Android 4.3 Bluetooth ble no se llama onCharacteristicRead ()
He fijado la notificación en androide, no está llamando al método onCharacteristicRead()
???? No entra en la función. ¿Por qué está sucediendo así ??
Cualquier ayuda es apreciada
- Anula el método pero el parámetro es arg0
- Integración de ZbarScanner con la aplicación Android
- WiFi Direct en Android no funciona correctamente
- ContentResolver.requestSync en Sync Adapter no funciona en Android
- Android Studio, ProGuard no puede calcular hash de Classes.jar
Solicite las soluciones.
Este es mi código:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.i(TAG, "Disconnected from GATT server."); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { gattServices = mBluetoothGatt .getService(SampleGattAttributes.SERVICES_UUID); if (gattServices != null) { gattCharacteristics = gattServices .getCharacteristic(SampleGattAttributes.CHARACTERISTIC_UUID); System.out.println("character-->" + gattCharacteristics); } if (gattCharacteristics != null) { System.out.println("Characteristic not null"); System.out.println("Characteristic Properties-->" + gattCharacteristics.getProperties()); mBluetoothGatt.setCharacteristicNotification(gattCharacteristics, true); } } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { System.out.println("in read"); if (status == BluetoothGatt.GATT_SUCCESS) { byte[] data = characteristic.getValue(); System.out.println("reading"); System.out.println(new String(data)); } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { // System.out.println("change"); byte[] data = characteristic.getValue(); System.out.println(new String(data)); } };
¡¡Gracias de antemano!!
- Android: obtener error cerca de runOnUiThread cuando lo uso cerca de fragmento
- La aplicación para Android publica everyThing excepto el archivo publicado
- Biblioteca de gráficos 2D para Android
- Desconocido GoogleAuthException al intentar obtener un token de autorización
- El servicio de facturación no está disponible en el dispositivo. (Respuesta: 3: Facturación no disponible)
- Obtener el idioma del teclado o detectar el idioma de entrada del usuario en Android
- ¿Cómo realizar pruebas sin conexión para aplicaciones que dependen de JSON?
- Cómo crear un mapa interactivo en Android
En primer lugar, onCharacteristicRead
se disparará si ha leído una característica mediante:
mBluetoothGatt.readCharacteristic(characteristic);
Leer una característica y configurar notificaciones son dos cosas diferentes. ¿Cuál es el tipo de característica que desea obtener los datos?
Lo es:
- leer
- notificar
- indicar
Si se read
, puede leer la característica usando el mBluetoothGatt.readCharacteristic(characteristic);
pero si su notify
o indicate
primero tendrá que leer el descriptor
la característica llamando a:
mBluetoothGatt.readDescriptor(ccc);
Una vez que lo lea, debe devolver datos llamando a la onDescriptorRead
llamada onDescriptorRead
.
Aquí puede configurar (suscribirse) a la caracteres mediante una notificación o una indicación llamando a:
mBluetoothGatt.setCharacteristicNotification(characteristic, true)
una vez que devuelve true
, tendrá que volver a escribir en el descriptor (el valor de notificación o indicación)
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC); clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // or //clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(clientConfig);
Una vez hecho esto, obtendrá notificaciones a través de la onCharacteristicChanged
devolución de onCharacteristicChanged
cada vez que cambie la característica.
puedes leer más acerca de la conexión Bluetooth en Android aquí
y acerca de las características de Bluetooth aquí