繁体   English   中英

CBCentralManager和自动引用计数

[英]CBCentralManager and Automatic Reference Counting

我有两个在BLE功能方面非常相似的应用程序,但是其中一个正确扫描并发现了外围设备,而其他外围设备却没有。

他们俩都认为初始化指向CBCentralManager的指针并检查其功能是否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 !
}

无功能

- (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;

任何建议,不胜感激!

因此,这有点愚蠢,但是重新启动电话+清理/构建可以修复所有问题。 我怀疑这两个应用程序在释放CBCentralManager句柄时不能很好地协同工作,但这只是一个猜测。 老实说,我对CoreBluetooth或Objective C并不很熟悉,所以我的猜测不是很清楚。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM