Bluetooth Low Energy Characteristics notification issue
Bonjour à tous,
je démarre avec BLE sous Android et je rencontre quelques problèmes.
J'ai 2 services différents avec chacun leur propre caractéristique. (Un GPS et un Accéléromètre)
lorsque je me connecte a mon périphérique bluetooth et que j'active les notifications pour chacune de ces caractéristiques (voir code enableTXNotification), j'ai régulièrement une des 2 caractéristique pour laquelle je ne reçois pas la notification (voir broadcastUpdate).
J'admet ne pas en comprendre la raison et je vois pas l'erreur qui m'y conduit.
Du coup, je m'interroge sur le CCCD, et ne suis pas certain d'avoir compris clairement comment cela fonctionne ... Peut être que la raison est autre ! Je compte sur un œil expert.
Merci pour vos conseils.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
public void enableTXNotification()
{
BluetoothGattService accService = mBluetoothGatt.getService(ACC_SERVICE_UUID);
if (accService == null) {
showMessage("Accelerometer service not found!");
return;
}
BluetoothGattCharacteristic TxChar = accService.getCharacteristic(ACC_TX_VALUE_CHAR_UUID);
if (TxChar == null) {
showMessage("Accelerometer value characteristic not found!");
return;
}
mBluetoothGatt.setCharacteristicNotification(TxChar, true);
BluetoothGattDescriptor descriptor2 = TxChar.getDescriptor(CCCD);
descriptor2.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor2);
BluetoothGattService gpsService = mBluetoothGatt.getService(GPS_SERVICE_UUID);
if (gpsService == null) {
showMessage("GPS service not found!");
return;
}
BluetoothGattCharacteristic gpsTxChar = gpsService.getCharacteristic(GPS_TX_VALUE_CHAR_UUID);
if (gpsTxChar == null) {
showMessage("GPS value charateristic not found!");
return;
}
mBluetoothGatt.setCharacteristicNotification(gpsTxChar,true);
BluetoothGattDescriptor descriptor = gpsTxChar.getDescriptor(CCCD);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
if (GPS_TX_VALUE_CHAR_UUID.equals(characteristic.getUuid())) {
Log.d(TAG, "Received GPS TX");
byte[] data = characteristic.getValue();
byte[] lat = Arrays.copyOfRange(data, 0, 4);
byte[] lng = Arrays.copyOfRange(data, 4, 8);
// la flemme de swapper par lot de 4 octet pour gerer tous les float en 1 fois ... !!!!
final FloatBuffer fb = ByteBuffer.wrap(SwapFloatEndian(lat)).asFloatBuffer();
final float[] dst = new float[fb.capacity()];
fb.get(dst); // Copy the contents of the FloatBuffer into dst
final FloatBuffer fb1 = ByteBuffer.wrap(SwapFloatEndian(lng) ).asFloatBuffer();
final float[] dst1 = new float[fb.capacity()];
fb1.get(dst1); // Copy the contents of the FloatBuffer into dst
intent.putExtra(EXTRA_DATA, String.format("Receipt GPS coordinates " + dst[0] + " - " + dst1[0] ).getBytes() );
}
else if (ACC_TX_VALUE_CHAR_UUID.equals( characteristic.getUuid()) )
{
Log.d(TAG, "Received Accelerometer TX");
intent.putExtra( EXTRA_DATA, String.format( "Receipt Giroscopic data " + byteArrayToByteString(characteristic.getValue() ) ).getBytes() );
}
else {
Log.d(TAG, "Unknow receipt");
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
} |