C++ CBCentralManager和自动参考计数
我有两个应用程序,它们在BLE功能上非常相似,但是其中一个可以正确扫描并发现其他没有的外围设备 他们都报告说,初始化指向C++ CBCentralManager和自动参考计数,c++,ios,objective-c,bluetooth,core-bluetooth,C++,Ios,Objective C,Bluetooth,Core Bluetooth,我有两个应用程序,它们在BLE功能上非常相似,但是其中一个可以正确扫描并发现其他没有的外围设备 他们都报告说,初始化指向CBCentralManager的指针时没有问题&检查它是否打开并正常工作,但其中一个继续发现并能够连接到设备,而另一个没有 在不起作用的情况下,scanForPeripheralsWithServices的调用会顺利进行,但不会触发didiscoverperipheral回调。我认为最大的不同是一个项目使用纯 ObjultC[/Cuff> &弧,而另一个使用了 Objul-C
CBCentralManager
的指针时没有问题&检查它是否打开并正常工作,但其中一个继续发现并能够连接到设备,而另一个没有
在不起作用的情况下,scanForPeripheralsWithServices
的调用会顺利进行,但不会触发didiscoverperipheral
回调。我认为最大的不同是一个项目使用纯<代码> ObjultC[/Cuff> &弧,而另一个使用了<代码> Objul-C</Cuff> C++,不使用自动引用计数。
这可能是问题的根源吗?如果是这样的话,有办法解决吗?过去,我可以在没有ARC的情况下使用coreblutooth
,但如果我无法从文档中解析,它可能已经改变了
如果不是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 !
}
- (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;
@interface BLEDeviceManager : NSObject <CBCentralManagerDelegate>
{
}
+ (BLEDeviceManager *)sharedDeviceManager;
@property (strong, nonatomic) CBCentralManager *central;
非功能性
- (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 !
}
- (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;
@interface BLEDeviceManager : NSObject <CBCentralManagerDelegate>
{
}
+ (BLEDeviceManager *)sharedDeviceManager;
@property (strong, nonatomic) CBCentralManager *central;
以及两个头文件的相关部分:
功能性
- (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 !
}
- (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;
@interface BLEDeviceManager : NSObject <CBCentralManagerDelegate>
{
}
+ (BLEDeviceManager *)sharedDeviceManager;
@property (strong, nonatomic) CBCentralManager *central;
@interface-BLE:NSObject{
}
@属性(非原子,赋值)id委托;
@属性(强、非原子)NSMutableArray*外围设备;
@属性(强,非原子)NSMutableArray*外围设备SSI;
@属性(强、非原子)CBCentralManager*CM;
@性质(强,非原子)CBPeripheral*activePeripheral;
非功能性
- (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 !
}
- (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;
@interface BLEDeviceManager : NSObject <CBCentralManagerDelegate>
{
}
+ (BLEDeviceManager *)sharedDeviceManager;
@property (strong, nonatomic) CBCentralManager *central;
@接口BLEDeviceManager:NSObject
{
}
+(BLEDeviceManager*)共享副经理;
@属性(强、非原子)CBCentralManager*central;
非常感谢任何建议 这有点傻,但重启手机+清理/构建修复了所有问题。我怀疑这两个应用程序在释放CBCentralManager句柄时没有很好地配合,但这只是一个猜测。老实说,我对CoreBluetooth或Objective C不太熟悉,所以我的猜测也不是很灵通。嘿,Joshua。。。好名字:)愚蠢的问题,但是你在第二个版本中设置了代理吗。。。i、 e.
self.central.delegate=self
?在第二种情况下,您是否尝试了nil选项?我尝试了nil选项,是的,代理已设置,但这很愚蠢,重新启动手机+清理/构建修复了所有问题。坦白说,我有点尴尬,我想删除这个问题。你可以自己回答,而不是删除这个问题。这可能对有同样问题的人有所帮助。