0
onCharacteristicChanged
は、周辺機器の特性を読み取ろうとしますが、setCharacteristicNotification
を設定しても呼び出されることはありません。トリガーされることはありませんsetCharacteristicNotificationはonCharacteristicChangedをトリガーしません
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
}
onCharacteristicChanged
方法:
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
Log.d(TAG, "characteristic changed");
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
同じコードが異なる周辺機器と正常に動作通知のセットアップ
private void getCharacteristic(List<BluetoothGattService> gattServices) {
if (gattServices == null) return;
for (BluetoothGattService gattService : gattServices) {
if (gattService.getUuid().toString().equals("000018f0-0000-1000-8000-00805f9b34fb")) {
for (BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
if (gattCharacteristic.getUuid().toString().equals("00002af0-0000-1000-8000-00805f9b34fb")) {
mBluetoothLeService.setCharacteristicNotification(gattService.getCharacteristic(gattCharacteristic.getUuid()), true);
}
}
}
}
}
:私の特性を取得し
方法。
Google Playストアから特性を表示するnRF Connectアプリもダウンロードしました.NRF Connectで通知を有効にすると、アプリ内のonCharacteristicChanged
が呼び出されました(logcatで「特性が変更されました」が表示されます)。