我有两个应用程序,它们的BLE功能非常相似,但是一个正确地扫描&发现外围设备,而其他的没有。
他们都报告说,初始化指向CBCentralManager的指针没有任何问题&检查它是否打开&功能,但是一个接着发现并能够连接到一个设备,而另一个没有。
在不工作的情况下,对scanForPeripheralsWithServices的调用会顺利进行,但是didDiscoverPeripheral回调不会被触发。我能想到的最大区别是,一个项目使用纯Objective-C & ARC,而另一个项目使用Objective-C和C++的混合,并且不使用自动引用计数。
这可能是问题的根源吗?如果是的话,有没有办法解决这个问题?过去,在没有ARC的情况下,我能够使用CoreBluetooth,但如果不能从文档中解析它,它可能会发生变化。
如果不是ARC,则对scanForPeripheralsWithServices的两个调用如下所示:
泛函
- (int) findBLEPeripherals:(int) timeout
{
NSLog(@"start finding");
if (self.CM.state != CBCentralManagerStatePoweredOn)
{
NSLog(@"CoreBluetooth not correctly initialized !");
NSLog(@"State = %d (%s)\r\n", self.CM.state, [self centralManagerStateToString:self.CM.state]);
return -1;
}
[NSTimer scheduledTimerWithTimeInterval:(float)timeout target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];
[self.CM scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@RBL_SERVICE_UUID]] options:nil];
NSLog(@"scanForPeripheralsWithServices");
return 0; // Started scanning OK !
}non-functional
- (void)startScan
{
NSLog(@"startScan");
isScanning = true;
NSDictionary *options = nil;
didUpdateDiscoveredDeviceFlag = [delegate respondsToSelector:@selector(didUpdateDiscoveredRFduino:)];
if (didUpdateDiscoveredDeviceFlag) {
options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
}
[devices removeAllObjects];
if (self.central.state != CBCentralManagerStatePoweredOn)
{
NSLog(@"CoreBluetooth not correctly initialized !");
// NSLog(@"State = %d (%s)\r\n", self.central.state, [self centralManagerStateToString:self.central.state]);
}
[NSTimer scheduledTimerWithTimeInterval:(float)60 target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];
[self.central scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@RBL_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
if (didUpdateDiscoveredDeviceFlag) {
[self startRangeTimer];
}
}以及两个头文件的相关部分:
泛函
@interface BLE : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate> {
}
@property (nonatomic,assign) id <BLEDelegate> delegate;
@property (strong, nonatomic) NSMutableArray *peripherals;
@property (strong, nonatomic) NSMutableArray *peripheralsRssi;
@property (strong, nonatomic) CBCentralManager *CM;
@property (strong, nonatomic) CBPeripheral *activePeripheral;non-functional
@interface BLEDeviceManager : NSObject <CBCentralManagerDelegate>
{
}
+ (BLEDeviceManager *)sharedDeviceManager;
@property (strong, nonatomic) CBCentralManager *central;任何建议都非常感谢!
发布于 2015-03-18 14:57:34
所以这有点傻,但是重新启动手机+干净/构建修复了一切。我怀疑这两个应用程序在发布CBCentralManager句柄时并不友好,但这只是猜测。老实说,我不太熟悉CoreBluetooth,也不太熟悉目标C,所以我的猜测并不那么灵通。
https://stackoverflow.com/questions/29114759
复制相似问题