繁体   English   中英

在OS X上使用updateValue:forCharacteristic:onSubscribedCentrals时,蓝牙崩溃

[英]Bluetooth crashing when using updateValue:forCharacteristic:onSubscribedCentrals on OS X

我编写了一个iOS应用程序,其中iOS设备既是BTLE中央设备又是BTLE外围设备,并且多个设备可以交换数据。 我试图将应用程序的外围部分带到OSX。问题是,一旦我尝试通过updateValue:forCharacteristic:onSubscribedCentrals将数据从OS X外围设备传输到iOS中央updateValue:forCharacteristic:onSubscribedCentrals ,OS X上的BTLE就会完全崩溃。

这意味着:

1)OS X计算机不再被任何iOS设备接收

2)外围设备管理器不再执行任何操作-不调用回调,什么也没有

3)当我在OS X上重新启动应用程序时,外围设备管理器永远不会将其状态更改为PoweredOn或任何其他状态

4)重新启动或使用sudo killall blued后,使BTLE在OS X上再次运行的唯一方法

完全相同的代码在iOS上可以正常工作,我不知道发生了什么。 基本上,我正在这样做:

- (instancetype)init {
    self = [super init];
    dispatch_queue_t peripheralQueue = dispatch_queue_create("connichiwaperipheralqueue", DISPATCH_QUEUE_SERIAL);
    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:peripheralQueue];
    return self;
}

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheralManager {
    if (peripheralManager.state == CBCentralManagerStatePoweredOn) {
        [self.peripheralManager addService:self.advertisedService];
        [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:BLUETOOTH_SERVICE_UUID]] }];
    }
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
    if (characteristic == self.advertisedInitialCharacteristic) {
        [self _sendInitialToCentral:central];
    }
}

- (void)_sendInitialToCentral:(CBCentral *)central {   
    NSDictionary *sendDictionary = @{ /* some dictionary, but i've also tried with a short, simple string */ };
    NSData *initialData = [NSJSONSerialization dataWithJSONObject:sendDictionary options:NSJSONWritingPrettyPrinted error:nil];
    BOOL didSend = [self.peripheralManager updateValue:initialData forCharacteristic:self.advertisedInitialCharacteristic onSubscribedCentrals:@[central]];
}

,而self.advertisedService是包含self.advertisedInitialCharacteristic的服务,而BLUETOOTH_SERVICE_UUID是iOS设备正在寻找的UUID。

执行此操作后,iOS设备将拾取OS X机器,发现服务,发现特征,然后OS X机器执行_sendInitialToCentral: 在这里, didSend变为true但是之后BTLE基本上停止在计算机上执行任何操作,并且新值永远不会到达iOS设备。

有任何想法吗? 难道我做错了什么? 我在这里看不到问题。 运行OS X 10.9.3

更新

只是想补充一点,我刚刚在另一台机器上进行了测试,并且发生了同样的事情。

更新2

发现原因 :此问题的原因是我将CBCentral交给updateValue:forCharacteristic:onSubscribedCentrals: 如果我将最后一个参数更改为nil ,则发送数据。 问题是我需要将数据发送到特定的中央,我不想广播它们。 任何人都知道这里发生了什么以及如何解决此问题?

只是为了回答这个问题:看来这是Mavericks中的错误。 优胜美地(Yosemite)已完全解决了这个问题(以及许多其他有关BTLE的问题)。

在Mavericks上,仅将nil传递到updateValue:forCharacteristic:onSubscribedCentrals的最后一个参数似乎可以使BTLE正常工作。

暂无
暂无

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

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