首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CBCentralManager与自动参考计数

CBCentralManager与自动参考计数
EN

Stack Overflow用户
提问于 2015-03-18 05:31:59
回答 1查看 260关注 0票数 0

我有两个应用程序,它们的BLE功能非常相似,但是一个正确地扫描&发现外围设备,而其他的没有。

他们都报告说,初始化指向CBCentralManager的指针没有任何问题&检查它是否打开&功能,但是一个接着发现并能够连接到一个设备,而另一个没有。

在不工作的情况下,对scanForPeripheralsWithServices的调用会顺利进行,但是didDiscoverPeripheral回调不会被触发。我能想到的最大区别是,一个项目使用纯Objective-C & ARC,而另一个项目使用Objective-C和C++的混合,并且不使用自动引用计数。

这可能是问题的根源吗?如果是的话,有没有办法解决这个问题?过去,在没有ARC的情况下,我能够使用CoreBluetooth,但如果不能从文档中解析它,它可能会发生变化。

如果不是ARC,则对scanForPeripheralsWithServices的两个调用如下所示:

泛函

代码语言:javascript
复制
- (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

代码语言:javascript
复制
- (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];
    }
}

以及两个头文件的相关部分:

泛函

代码语言:javascript
复制
@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

代码语言:javascript
复制
@interface BLEDeviceManager : NSObject <CBCentralManagerDelegate>
{

}

+ (BLEDeviceManager *)sharedDeviceManager;

@property (strong, nonatomic) CBCentralManager *central;

任何建议都非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-18 14:57:34

所以这有点傻,但是重新启动手机+干净/构建修复了一切。我怀疑这两个应用程序在发布CBCentralManager句柄时并不友好,但这只是猜测。老实说,我不太熟悉CoreBluetooth,也不太熟悉目标C,所以我的猜测并不那么灵通。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29114759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档