我编写了一个 iOS 应用程序,其中 iOS 设备既是 BTLE 中央设备又是外围设备,并且多个设备可以交换数据。我试图将应用程序的外围部分带到 OS X。问题是,一旦我尝试通过 updateValue:forCharacteristic:onSubscribedCentrals
将数据从 OS X 外围设备传输到 iOS 中心,BTLE在 OS X 上几乎完全崩溃。
这意味着:
1) OS X 计算机不再被任何 iOS 设备接收
2) peripheral manager 不再做任何事情——没有回调被调用,什么都没有
3) 当我在 OS X 上重新启动应用程序时,外设管理器永远不会将其状态更改为 PoweredOn 或任何其他状态
4) 在此之后让 BTLE 再次在 OS X 上工作的唯一方法是重新启动或使用 sudo killall blued
完全相同的代码在 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 中的一个错误。这个问题(以及很多其他与 BTLE 相关的问题)在 Yosemite 中得到了彻底解决。
在 Mavericks 上,只将 nil
传递给 updateValue:forCharacteristic:onSubscribedCentrals
的最后一个参数似乎可以使 BTLE 工作。
关于ios - 使用 updateValue :forCharacteristic:onSubscribedCentrals on OS X 时蓝牙崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24595953/