我编写了一个iOS应用程序,其中iOS设备既是BTLE中央设备又是BTLE外围设备,并且多个设备可以交换数据。我试图将应用程序的外围部分带到OSX。问题是,一旦我尝试通过OS X上的updateValue:forCharacteristic:onSubscribedCentrals
BTLE将数据从OS X外围设备传输到iOS中心,OS X上的BTLE就会完全崩溃。
这意味着:
1)OS X计算机不再被任何iOS设备接收
2)外围设备管理器不再执行任何操作-不调用回调,什么也没有
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
是一个UUID的iOS设备所期待的。
执行此操作时,iOS设备会拿起OS X机器,发现服务,发现特征,然后OS X机器执行_sendInitialToCentral:
。在这里,didSend
变为,true
但之后BTLE基本上停止了在计算机上执行任何操作,并且新值永远不会到达iOS设备。
有任何想法吗?难道我做错了什么?我在这里看不到问题。运行OS X 10.9.3
只是想补充一点,我刚刚在另一台机器上进行了测试,并且发生了相同的事情。
发现原因:这个问题的原因是我将CBCentral交给了updateValue:forCharacteristic:onSubscribedCentrals:
。如果我将最后一个参数更改为要nil
发送的数据。问题是我需要将数据发送到特定的中央,我不想广播它们。任何人都知道这里发生了什么以及如何解决此问题?
只是为了回答这个问题:看来这是Mavericks中的错误。这个问题(以及许多其他有关BTLE的问题)已在优胜美地完全解决。
在Mavericks上,仅传递nil
到最后一个参数updateValue:forCharacteristic:onSubscribedCentrals
似乎可以使BTLE正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句