カスタムサービスと特性を備えたBLEセントラルとして機能するiOSアプリケーションを開発しています。
また、周辺機器は某ベンダが開発したものです。
システムは、特性値を書き込むためにセントラルとペリフェラル間のペアリングを必要とします。そして今、ペアリングに関して問題があります。
[設定] の [Bluetooth] の [このデバイスを忘れる] で中央 (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);
}
}
}
}
どちらの場合も(セントラル/ペリフェラル側のペアリング情報を削除した後)、まったく同じ特性を発見し、同じデータを書き込みます。
ただし、後者(ペリフェラル側で削除)の場合のみ、ペアリングシーケンスは開始されません。