コアBluetoothセントラル --> ペリフェラル
-
21-12-2019 - |
質問
私は Bluetooth 通信についてはまったくの初心者です。私の最初のプロジェクトは、iOS デバイスから BLEshield (小型チップ) にデータを転送することを目的としています。
中心となるコードをテストするために、iPhone をペリフェラル (チップを入手した後の役割) として設定し、iPad をセントラルとして設定することにしました。
デバイスを接続し、周辺機器から中央機器にデータを送信することもできます。ただし、それは非常に簡単です:
- (void)startService {
_readChar = [[CBMutableCharacteristic alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
_writeChar = [[CBMutableCharacteristics alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsWriteable];
_service = [[CBMutableService alloc] initWithType:[CBUUID ...] primary:YES];
[_service setCharacteristics:@[_readChar, _writeChar]];
_peripheral = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
[_peripheral addService:_service];
[_peripheral startAdvertising:@{CBAdvertisementDataServiceUUIDKey: @[[CBUUID ...]], CBAdvertisementDataLocalNameKey: @"ServiceName"}];
}
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
[_peripheral updateValue:[@"HELLO WORLD" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_readChar onSubscribedCentrals:nil];
}
しかし、他の方向を機能させることができません。中央側からデータを送信するには、次のコードがあります。
[_activePeripheral writeValue:[@"PONG" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_writeChar type:CBCharacteristicWriteWithoutResponse];
これらのメソッドのいずれかをペリフェラルで呼び出す必要があると仮定します。
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
しかし、実際には何も起こりません。残念ながら、私のハードウェア プロジェクトではペリフェラル モードでのみ動作するチップを使用することになり、最終的には制御信号の送信機であるペリフェラルにほぼ独占的に書き込むことになります。
誰かが私を助けてくれることを願っています!
解決
次のプロパティ:
_readChar
あるべきですCBCharacteristicPropertyRead
_writeChar
あるべきですCBCharacteristicPropertyWriteWithoutResponse
見る ここ 詳細については。 CBCharacteristicPropertyNotify
特性を書き込みまたは読み取り可能にすることはできません。通知 (サブスクライブ/サブスクライブ解除) のみが可能です。
他のヒント
https://github.com/lgbluetooth/lgbluetooth ライフを楽にするだろう
所属していません StackOverflow