前提・実現したいこと
Javaで特定のBluetoothデバイスとペアリングしデータの受信を行うAndroidアプリを作成しています。
該当のデバイスと接続し、接続完了後に画面上のボタンからCCCDの設定とcharacteristicの書き込みを行います。
そのデバイスでは、indicateとwriteでデータの受信と確認を行います。
問題は、デバイスとの接続完了後にindicateを設定した後、writeしてもonCharacteristicWriteが呼ばれないことです。
writeしたい値自体は書き込まれていました
また、indicateの設定をする前であれば、writeしてもonCharacteristicWriteが呼ばれます。
尚、writeの値が悪いのか、indicateの設定が間違えているのか、onCharacteristicChangedも呼ばれておりません。
接続が完了するところまでは確認できています。
また、indicateの設定後に、onDescriptorWriteも呼ばれておりません。
考えられる原因や解決策について教えていただければ幸いです。
よろしくお願いいたします。
該当のソースコード
// キャラクタリスティックの書き込み public void writeCharacteristic( UUID uuid_service, UUID uuid_characteristic, byte[] bytes ) { if( null == mBluetoothGatt ) { String TAG = ""; Log.e(TAG, "writeCharacteristic GATT is null"); return; } try { //対象のサービスを取得mBluetoothGattService BluetoothGattService bluetoothGattService = mBluetoothGatt.getService( uuid_service ); //対象のキャラクタリスティックを取得 BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGattService.getCharacteristic(uuid_characteristic); //キャラクタリスティックへの書き込み bluetoothGattCharacteristic.setValue(bytes); //ガットへの書き込み mBluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic); } catch (Exception e) { String TAG = ""; Log.e(TAG, "writeCharacteristic error. ", e); } } // キャラクタリスティック通知の設定(Indication) private void setCharacteristicIndication( UUID uuid_service, UUID uuid_characteristic ) { if( null == mBluetoothGatt ) { return; } //対象のサービスを取得 BluetoothGattService bluetoothGattService = mBluetoothGatt.getService( uuid_service ); //対象のキャラクタリスティックを取得 BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGattService.getCharacteristic( uuid_characteristic ); //trueで許可を出すのは、notify,indicateで共通 boolean chaFlag = mBluetoothGatt.setCharacteristicNotification( bluetoothGattCharacteristic, true); //通知なので、descriptorを取得し、そこにvalueをセット(正しくは、設定を呼び出す) BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor( UUID_NOTIFY ); //ここで、notify,indicateの定義 descriptor.setValue( BluetoothGattDescriptor.ENABLE_INDICATION_VALUE ); mBluetoothGatt.writeDescriptor( descriptor ); }
補足情報(FW/ツールのバージョンなど)
AndroidStudio 4.0.1
あなたの回答
tips
プレビュー