Notificações do Android BLE para Glucose
Eu cansei de usar o Android BLE SDK para me comunicar com meu dispositivo de glicose. Eu preciso setCharacteristicNotification para UUID 2a18 e 2a34. Refiro-me ao SDK oficial do Android da seguinte maneira:
http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#notification
BluetoothGattCharacteristic charGM =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_GLUCOSE_MEASUREMENT_STRING));
mConnGatt.setCharacteristicNotification(charGM, enabled);
BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descGM);
MAS ele não pode nem inserir o retorno de chamada onCharacteristicChanged.
my onCharacteristicChanged da seguinte maneira:
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"onCharacteristicChanged",Toast.LENGTH_LONG).show();
setProgressBarIndeterminateVisibility(false);
};
});
}
se eu definir a notificação de nível de bateria da seguinte forma, ele funcionará.
BluetoothGattCharacteristic charBarrery =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_BATTERY))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_BATTERY_LEVEL_STRING));
mConnGatt.setCharacteristicNotification(charBarrery, enabled);
BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descBarrery);
Não sei o que há de diferente nas notificações de bateria e glicose.
Se alguém soube o que devo fazer, por favor, me ajude.
adicional:
quando usei o serviço de bateria, meu logcat da seguinte maneira:
07-29 10:28:17.924: D/BluetoothGatt(947): setCharacteristicNotification() - uuid: 00002a19-0000-1000-8000-00805f9b34fb enable: true
07-29 10:28:17.924: D/BluetoothGatt(947): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:28:18.484: D/BluetoothGatt(947): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
07-29 10:28:18.604: D/BluetoothGatt(947): onNotify() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
mas quando uso Glucose, meu logcat perde onNotify (), assim:
07-29 10:31:23.729: D/BluetoothGatt(1763): setCharacteristicNotification() - uuid: 00002a18-0000-1000-8000-00805f9b34fb enable: true
07-29 10:31:23.729: D/BluetoothGatt(1763): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:31:24.324: D/BluetoothGatt(1763): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a18-0000-1000-8000-00805f9b34fb
Eu não sei por que o logcat perde o onNotify () ...
adicional (8/4):
Obrigado pela sua resposta ! Tentei habilitar as indicações na característica do Ponto de Controle de Acesso ao Registro, mas falhei.
Ativar notificações na característica de Medição de Glicose e característica de Contexto de Medição de Glicose e Ativar indicações na característica de Ponto de Controle de Acesso a Registros
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
for (BluetoothGattService service : gatt.getServices()) {
if ((service == null) || (service.getUuid() == null)) {
continue;
}
if (BleUuid.SERVICE_GLUCOSE.equalsIgnoreCase(service
.getUuid().toString())) {
BluetoothGattCharacteristic charGM =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_GLUCOSE_MEASUREMENT_STRING));
mConnGatt.setCharacteristicNotification(charGM, enabled);
BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descGM);
BluetoothGattCharacteristic charGMC =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_GLUCOSE_MEASUREMENT_CONTEXT_STRING));
mConnGatt.setCharacteristicNotification(charGMC, enabled);
BluetoothGattDescriptor descGMC = charGMC.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descGMC.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descGMC);
BluetoothGattCharacteristic charRACP =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING));
mConnGatt.setCharacteristicNotification(charRACP, enabled);
BluetoothGattDescriptor descRACP = charRACP.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descRACP.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mConnGatt.writeDescriptor(descRACP);
BluetoothGattCharacteristic charBarrery =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_BATTERY))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_BATTERY_LEVEL_STRING));
mConnGatt.setCharacteristicNotification(charBarrery, enabled);
BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descBarrery);
runOnUiThread(new Runnable() {
public void run() {
btnUpdateData.setEnabled(true);
};
});
}
}
};
Envie 0x0101 para registrar o ponto de controle de acesso
case R.id.btnUpdateData:
try{
//***SEND 0x0101 TO RECORD ACCESS CONTROL POINT
BluetoothGattCharacteristic writeRACPchar =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING));
byte[] data = new byte[1];
data[0] = (byte)0x0101;
writeRACPchar.setValue(data);
mConnGatt.writeCharacteristic(writeRACPchar);
}catch(Exception e){
e.printStackTrace();
}
break;
minha função de retorno de chamada
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
};
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
&
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
if (BleUuid.CHAR_SERIAL_NUMBEAR_STRING
.equalsIgnoreCase(characteristic.getUuid().toString())) {
displayResult(characteristic.getStringValue(0));
}else if(BleUuid.CHAR_MANUFACTURER_NAME_STRING
.equalsIgnoreCase(characteristic.getUuid().toString())){
displayResult(characteristic.getStringValue(0));
} else if(BleUuid.CHAR_BATTERY_LEVEL_STRING
.equalsIgnoreCase(characteristic.getUuid().toString())){
final byte[] data = characteristic.getValue();
String dataStr = "";
dataStr = String.format("%02X", data[0]);
int a = Integer.parseInt(dataStr, 16);
String result = "battery level: " + Integer.toString(a)+ "%";
displayResult(result);
} else {
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
displayResult(stringBuilder.toString());
}
}
}
&
private void displayResult(String result){
adUpdateData.add(result);
runOnUiThread(new Runnable() {
public void run() {
lvUpdateData.setAdapter(adUpdateData);
};
});
}
Estou tentando entender o pdf "GLS_SPEC" ... Usei o aplicativo de exemplo Glicose Service da Nordic Semiconductor e ele pode funcionar. Eu tento aprender como alcançar essa função.
Observei que um log como "unregisterIRListener () é chamado") é exibido no LogCat, mas não tenho certeza de que seja sobre minha pergunta ou não ...
Obrigado pela leitura.