2016-09-14 15 views
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で「特性が変更されました」が表示されます)。

答えて

0

私はこの問題を解決しました。私は私のBluetoothGattwriteDescriptorに持っていたので、私のsetCharacteristicNotificiationは次のようになります。

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, 
               boolean enabled) { 
    if (mBluetoothAdapter == null || mBluetoothGatt == null) { 
     Log.w(TAG, "BluetoothAdapter not initialized"); 
     return; 
    } 
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); 

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
     UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); 
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
     mBluetoothGatt.writeDescriptor(descriptor); 
} 
関連する問題