我正在开发 iOS 应用程序,它作为具有自定义服务和特性的 BLE 中心。
并且外围设备是由某个供应商开发的。
系统需要中央和外围配对才能写入特征值。现在我有一个关于配对的问题。
如果我通过[设置]的[蓝牙]中的[忘记此设备]删除中央(iPhone)端的配对信息,
当我的应用程序在连接后尝试写入特征值并发现服务和特征时,配对序列再次开始。(配对对话显示在我的应用程序上)
顺序如下,
1. Complete connection
[_centralMgr centralManager:didConnectPeripheral:]
2. Discover service
[_centralMgr peripheral:didDiscoverServices:]
3. Discover characteristic
[_centralMgr peripheral:didDiscoverCharacteristicsForService:error:]
4. Try to write characteristic value
[_centralMgr peripheral:writeValue:forCharacteristic:tyep:]
5. At this timing, paring dialog is shown and user tap [Paring] button
6. Complete writing of characteristic value without error
[_centralMgr peripheral:didWriteValueForCharacteristic:error:]
但是,如果我删除外围设备端的配对信息(通过重置设备), 当我的应用程序在连接后尝试写入特征值并发现服务和特征时,配对序列不会开始。
配对对话永远不会显示,并且写入请求的响应不会返回。
顺序如下,
1. Complete connectiton
[_centralMgr centralManager:didConnectPeripheral:]
2. Discover service
[_centralMgr peripheral:didDiscoverServices:]
3. Discover characteristic
[_centralMgr peripheral:didDiscoverCharacteristicsForService:error:]
4. Try to write characteristic value
[_centralMgr peripheral:writeValue:forCharacteristic:tyep:]
5. At this timing, paring dialog is never shown
6. Response of writing request(in STEP 4.) doesn't return
外围设备的供应商说,即使外围设备返回写入错误,iPhone 也不会请求配对。
但是我的应用程序(至少 iOS 中的应用程序层)没有收到写入错误的委托 API。
有没有人有同样的问题?如果您能提供一些提示或信息,我将不胜感激。
添加写特征值的代码
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error
{
NSArray* characteristics = service.characteristics;
for (CBCharacteristic* characteristic in characteristics) {
if (characteristic.properties & CBCharacteristicPropertyWrite) {
if ([[characteristic.UUID UUIDString] isEqualToString:MY_CUSTOM_CHARACTERISTIC]) {
NSData* data = MY_CUSTOM_DATA;
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
NSLog(@"writeValue:%@", data);
}
}
}
}
在这两种情况下(在中央/外围端删除配对信息后),它发现完全相同的特征并写入相同的数据。
但仅在后一种情况下(在外围端删除),配对序列不会开始。