Я новичок в Xcode и Objective C, но быстро учился. Я пишу приложение Bluetooth LE для сбора данных с нескольких устройств BLE. Счастлив с CoreBluetooth и могу получить то, что я хочу использовать и собирать данные.Класс не работает
Однако я сделал все это в AppDelegate и теперь хочу выделить различные разделы кода в аккуратные классы.
Код компилируется в порядке, но ничего не запускается, кроме AppDelegate.
Пример класса - SensorDev.m:
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@class SensorDev;
@protocol SensorDevDelegate<NSObject>
- (void) sensorDevDidChangeStatus:(SensorDev*)dev;
@end
@interface SensorDev : NSObject
@property (nonatomic, assign, readonly) id<SensorDevDelegate> delegate;
@property (nonatomic, readonly) CBPeripheral *peripheral;
- (id)initWithPeripheral:(CBPeripheral *)peripheral controller:(id<SensorDevDelegate>)controller;
- (void)start;
@end
Пример класса - SensorDev.h
#import "SensorDev.h"
NSString *SR1Device9DOFServiceUUIDString = @"346D0000";
NSString *SR1Device9DOFCharacteristicUUIDString = @"346D0001-12A9-11CF-1279-81F2B7A91332";
@interface SensorDev() <CBPeripheralDelegate> {
CBService *_temperatureService;
CBCharacteristic *_temperatureCharacteristic;
}
@end
@implementation SensorDev
#pragma mark - Setup
- (id)initWithPeripheral:(CBPeripheral *)peripheral controller:(id<SensorDevDelegate>)controller
{
self = [super init];
if (self) {
_peripheral = peripheral;
_peripheral.delegate = self;
_delegate = controller;
}
return self;
}
#pragma mark - Start
// -------------------------------------------------------------------------------
// Startup
// -------------------------------------------------------------------------------
- (void)start
{
NSLog(@"- (void) start"); //--Debug
CBUUID *serviceUUID = [CBUUID UUIDWithString:SR1Device9DOFServiceUUIDString];
NSArray *serviceArray = [NSArray arrayWithObjects:serviceUUID, nil];
[_peripheral discoverServices:serviceArray];
}
@end
Я не получаю строку отладки в журнале:
NSLog(@"- (void) start"); //--Debug
Ищет помощь, ребята .... что мне не хватает ... спасибо заранее.
UPDATE
Так у меня есть второй класс, который делает все настройки CoreBluetooth и обнаружения
Discovery.h
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "SensorDev.h"
// -------------------------------------------------------------------------------
//UI Setup/Protocols
// -------------------------------------------------------------------------------
@protocol DiscoveryDelegate <NSObject>
- (void) discoveryDidRefresh;
- (void) discoveryStatePoweredOff;
@end
@interface Discovery : NSObject
+(Discovery*) sharedInstance;
@property (nonatomic, assign) id<DiscoveryDelegate> discoveryDelegate;
@property (nonatomic, assign) id<SensorTagDelegate> peripheralDelegate;
// -------------------------------------------------------------------------------
// Actions
// -------------------------------------------------------------------------------
- (void) startScanningForUUIDString:(NSString *)uuidString;
- (void) stopScanning;
- (void) connectPeripheral:(CBPeripheral*)peripheral;
- (void) disconnectPeripheral:(CBPeripheral*)peripheral;
// -------------------------------------------------------------------------------
// Access to the devices
// -------------------------------------------------------------------------------
@property (readonly, nonatomic) NSMutableArray *foundPeripherals;
@property (retain, nonatomic) NSMutableArray *connectedPeripherals;
@end
Discover.m (экстракт)
#import "Discovery.h"
extern NSString *SR1Device9DOFServiceUUIDString; //346D0000
extern NSString *SR1Device9DOFCharacteristicUUIDString; //346D0001-12A9-11CF-1279-81F2B7A91332
@interface Discovery() <CBCentralManagerDelegate, CBPeripheralDelegate> {
CBCentralManager *_centralManager;
BOOL _pendingInit;
}
@end
@implementation Discovery
#pragma mark - Setup
+ (Discovery*) sharedInstance
{
static Discovery *this = nil;
if (!this)
this = [[Discovery alloc] init];
return this;
}
- (id) init
{
self = [super init];
if (self) {
_pendingInit = YES;
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
_foundPeripherals = [[NSMutableArray alloc] init];
_connectedPeripherals = [[NSMutableArray alloc] init];
}
return self;
}
#pragma mark - CoreBluetooth Services
// -------------------------------------------------------------------------------
// CoreBluetooth Start/Stop Scanning
// -------------------------------------------------------------------------------
- (void)startScanningForUUIDString:(NSString *)uuidString
{
NSLog(@"- (void) startScanningForUUIDString"); //--Debug
[_centralManager scanForPeripheralsWithServices:
[NSArray arrayWithObjects:SR1Device9DOFServiceUUIDString, nil] options:nil];
}
- (void)stopScanning
{
NSLog(@"- (void) stopScanning"); //--Debug
}
// -------------------------------------------------------------------------------
// CoreBluetooth Connect/Disconnect
// -------------------------------------------------------------------------------
- (void) connectPeripheral:(CBPeripheral*)peripheral
{
NSLog(@"- (void) connectPeripheral"); //--Debug
if (peripheral.state == CBPeripheralStateDisconnected) {
[_centralManager connectPeripheral:peripheral options:nil];
}
}
- (void) disconnectPeripheral:(CBPeripheral*)peripheral
{
NSLog(@"- (void) disconnectPeripheral"); //--Debug
[_centralManager cancelPeripheralConnection:peripheral];
}
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"- (void) didConnectPeripheral"); //--Debug
SensorDev *tag = nil;
// Create a service instance.
tag = [[SensorDev alloc] initWithPeripheral:peripheral controller:_peripheralDelegate];
[tag start];
if (![_connectedPeripherals containsObject:tag])
[_connectedPeripherals addObject:tag];
[_peripheralDelegate sensorTagDidChangeStatus:tag];
[_discoveryDelegate discoveryDidRefresh];
}
Этот не работает ...
Где и как создается и запускается «SensorDev»? –
Привет, я не уверен, я думаю, что ответ - это не так, и вот где код и мои знания не срабатывают ... – duck1970
Вы на самом деле ЗВОНИТЕ начало? –