diff options
author | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-11-18 08:50:54 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-11-24 15:08:57 +0100 |
commit | 6ed49688149fd43b3d4c5aa0415191a8c1cdc1a5 (patch) | |
tree | ae2a3ef1c4d5c5bcb5f6912ed874bcebf8977eed /src/android | |
parent | 5de53cc79ecb83e3bce97567b1462ce432cc5759 (diff) |
Android: characteristic changed notification support
Change-Id: I4c50df7d758390989c2e2127f7646e5d2dc34712
Reviewed-by: Timur Pocheptsov <Timur.Pocheptsov@digia.com>
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/android')
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java index 988f3c3d..618be1ea 100644 --- a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java +++ b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java @@ -45,6 +45,7 @@ import android.bluetooth.BluetoothProfile; import android.util.Log; import java.util.ArrayList; +import java.util.Arrays; import java.util.Hashtable; import java.util.LinkedList; import java.util.List; @@ -57,6 +58,7 @@ public class QtBluetoothLE { private BluetoothGatt mBluetoothGatt = null; private String mRemoteGattAddress; + final UUID clientCharacteristicUuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); /* Pointer to the Qt object that "owns" the Java object */ @@ -271,7 +273,13 @@ public class QtBluetoothLE { public void onCharacteristicChanged(android.bluetooth.BluetoothGatt gatt, android.bluetooth.BluetoothGattCharacteristic characteristic) { - System.out.println("onCharacteristicChanged"); + int handle = handleForCharacteristic(characteristic); + if (handle == -1) { + Log.w(TAG,"onCharacteristicChanged: cannot find handle"); + return; + } + + leCharacteristicChanged(qtObject, handle+1, characteristic.getValue()); } public void onDescriptorRead(android.bluetooth.BluetoothGatt gatt, @@ -839,6 +847,31 @@ public class QtBluetoothLE { skip = true; break; case Descriptor: + if (nextJob.entry.descriptor.getUuid().compareTo(clientCharacteristicUuid) == 0) { + /* + For some reason, Android splits characteristic notifications + into two operations. BluetoothGatt.enableCharacteristicNotification + ensures the local Blueooth stack forwards the notifications. In addition, + BluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) + must be written to the peripheral + */ + + boolean enableNotifications = true; + if (Arrays.equals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE, + nextJob.newValue)) + enableNotifications = false; + + result = mBluetoothGatt.setCharacteristicNotification( + nextJob.entry.descriptor.getCharacteristic(), enableNotifications); + if (!result) { + Log.w(TAG, "Cannot set characteristic notification"); + //we continue anyway to ensure that we write the requested value + //to the device + } + + Log.d(TAG, "Enable notifications: " + enableNotifications); + } + result = nextJob.entry.descriptor.setValue(nextJob.newValue); if (!result || !mBluetoothGatt.writeDescriptor(nextJob.entry.descriptor)) skip = true; @@ -847,7 +880,6 @@ public class QtBluetoothLE { case CharacteristicValue: skip = true; break; - } if (!skip) @@ -860,7 +892,6 @@ public class QtBluetoothLE { } } - public native void leConnectionStateChange(long qtObject, int wasErrorTransition, int newState); public native void leServicesDiscovered(long qtObject, int errorCode, String uuidList); public native void leServiceDetailDiscoveryFinished(long qtObject, final String serviceUuid, @@ -874,6 +905,6 @@ public class QtBluetoothLE { int errorCode); public native void leDescriptorWritten(long qtObject, int charHandle, byte[] newData, int errorCode); - + public native void leCharacteristicChanged(long qtObject, int charHandle, byte[] newData); } |