ゴール
Linux を実行する単純なデバイスを開発しています。BLE 対応で、現在 bluez 5.8 を使用しています。
iPhone を使用して、このデバイスでアクションをトリガーしたいと考えています。
すでに機能しているもの:
- iPhoneにデバイスを「見せる」ことができます。
- iPhone もデバイスに接続します。
Linuxで次のようにBluetoothデバイスをセットアップしました(この質問に感謝します):
# activate bluetooth
hciconfig hci0 up
# set advertise data: "hello world"
hcitool -i hci0 cmd 0x08 0x0008 48 45 4c 4c 4f 57 4f 52 4c 44
# start advertising as connectable
hciconfig hci0 leadv 0
iOS コードは単純です。
- (int) scanForPeripherals
{
if (self->centralManager.state != CBCentralManagerStatePoweredOn) {
return -1;
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[self.centralManager scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if (central.state == CBCentralManagerStatePoweredOn) {
NSLog(@"Starting scan");
[self scanForPeripherals];
}
}
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"didDiscoverPeripheral");
/*
* Retain the peripheral to avoid the error:
* CoreBluetooth[WARNING]: state = connecting> is being dealloc'ed while connecting
*/
self.activePeripheral = peripheral;
[centralManager connectPeripheral:peripheral options:nil];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Connected to peripheral");
/* discover all services */
[peripheral discoverServices:nil];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSLog(@"Discovered services");
for (CBService *service in peripheral.services) {
NSLog(@"Discovered service %@", service);
}
}
このコードを iPhone で実行すると、次のログが表示されます。
2013-12-19 12:53:22.609 Test2[18518:60b] Starting scan
2013-12-19 12:53:29.945 Test2[18518:60b] didDiscoverPeripheral
2013-12-19 12:53:31.230 Test2[18518:60b] Connected to peripheral
そのため、iPhone は問題なく接続しているように見えますが、サービスは表示されません。
私が欠けているもの
- シンプルな BLEサービスを宣伝する必要がありますが、bluez でこれを行う方法に関するドキュメントが見つかりません。
- 私が宣伝するサービスの読み取り/書き込み特性を受け取るには、gatt-server のようなものが必要だと思います。bluez で plugins/gatt-example.c ファイルを見ましたが、使い方がまったくわかりません。ドキュメントがありません。
私はおそらく、次の質問を見たことに言及する必要があります:ガット サーバーの作成、しかし、答えはあまりにも多くの質問を提起します (たとえば、bluez の GATT API はどこにありますか? GATT データベースを設定する方法は? 読み取り/書き込みイベントに登録する方法は? )
編集: 私が使用するコマンドは、一部のデータをアドバタイズするために BLE デバイスをセットアップするだけですが、iOS は接続が受け入れられたと報告します。着信接続を受け入れる bluez の部分は?