diff options
Diffstat (limited to 'device/bluetooth/android/java/src/org/chromium/device')
4 files changed, 135 insertions, 67 deletions
diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java index 88e290f..b7fd6a1 100644 --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java @@ -255,6 +255,44 @@ final class ChromeBluetoothDevice { } }); } + + @Override + public void onDescriptorRead( + final Wrappers.BluetoothGattDescriptorWrapper descriptor, final int status) { + ThreadUtils.runOnUiThread(new Runnable() { + @Override + public void run() { + ChromeBluetoothRemoteGattDescriptor chromeDescriptor = + mWrapperToChromeDescriptorsMap.get(descriptor); + if (chromeDescriptor == null) { + // Android events arriving with no Chrome object is expected rarely: only + // when the event races object destruction. + Log.v(TAG, "onDescriptorRead when chromeDescriptor == null."); + } else { + chromeDescriptor.onDescriptorRead(status); + } + } + }); + } + + @Override + public void onDescriptorWrite( + final Wrappers.BluetoothGattDescriptorWrapper descriptor, final int status) { + ThreadUtils.runOnUiThread(new Runnable() { + @Override + public void run() { + ChromeBluetoothRemoteGattDescriptor chromeDescriptor = + mWrapperToChromeDescriptorsMap.get(descriptor); + if (chromeDescriptor == null) { + // Android events arriving with no Chrome object is expected rarely: only + // when the event races object destruction. + Log.v(TAG, "onDescriptorWrite when chromeDescriptor == null."); + } else { + chromeDescriptor.onDescriptorWrite(status); + } + } + }); + } } // --------------------------------------------------------------------------------------------- diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java index ea0a4dc..6d19c1f 100644 --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java @@ -5,8 +5,6 @@ package org.chromium.device.bluetooth; import android.annotation.TargetApi; -import android.bluetooth.BluetoothGattCharacteristic; -import android.bluetooth.BluetoothGattDescriptor; import android.os.Build; import org.chromium.base.Log; @@ -14,7 +12,6 @@ import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.JNINamespace; import java.util.List; -import java.util.UUID; /** * Exposes android.bluetooth.BluetoothGattCharacteristic as necessary @@ -115,51 +112,6 @@ final class ChromeBluetoothRemoteGattCharacteristic { return mCharacteristic.getProperties(); } - // Implements BluetoothRemoteGattCharacteristicAndroid::StartNotifySession. - @CalledByNative - private boolean startNotifySession() { - // Verify properties first, to provide clearest error log. - int properties = mCharacteristic.getProperties(); - boolean hasNotify = (properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0; - boolean hasIndicate = (properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0; - if (!hasNotify && !hasIndicate) { - Log.v(TAG, "startNotifySession failed! Characteristic needs NOTIFY or INDICATE."); - return false; - } - - // Find config descriptor. - Wrappers.BluetoothGattDescriptorWrapper clientCharacteristicConfigurationDescriptor = - mCharacteristic.getDescriptor(UUID.fromString( - "00002902-0000-1000-8000-00805F9B34FB" /* Config's standard UUID*/)); - if (clientCharacteristicConfigurationDescriptor == null) { - Log.v(TAG, "startNotifySession config descriptor failed!"); - return false; - } - - // Request Android route onCharacteristicChanged notifications for this characteristic. - if (!mChromeDevice.mBluetoothGatt.setCharacteristicNotification(mCharacteristic, true)) { - Log.i(TAG, "startNotifySession setCharacteristicNotification failed."); - return false; - } - - // Enable notification on remote device's characteristic: - if (!clientCharacteristicConfigurationDescriptor.setValue(hasNotify - ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE - : BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)) { - Log.v(TAG, "startNotifySession descriptor setValue failed!"); - return false; - } - Log.v(TAG, hasNotify ? "startNotifySession NOTIFY." : "startNotifySession INDICATE."); - - if (!mChromeDevice.mBluetoothGatt.writeDescriptor( - clientCharacteristicConfigurationDescriptor)) { - Log.i(TAG, "startNotifySession writeDescriptor failed!"); - return false; - } - - return true; - } - // Implements BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic. @CalledByNative private boolean readRemoteCharacteristic() { @@ -184,6 +136,12 @@ final class ChromeBluetoothRemoteGattCharacteristic { return true; } + // Enable or disable the notifications for this characteristic. + @CalledByNative + private boolean setCharacteristicNotification(boolean enabled) { + return mChromeDevice.mBluetoothGatt.setCharacteristicNotification(mCharacteristic, enabled); + } + // Creates objects for all descriptors. Designed only to be called by // BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated. @CalledByNative diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattDescriptor.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattDescriptor.java index e87b57b..ac7489d 100644 --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattDescriptor.java +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattDescriptor.java @@ -18,19 +18,14 @@ import org.chromium.base.annotations.JNINamespace; final class ChromeBluetoothRemoteGattDescriptor { private static final String TAG = "Bluetooth"; - // TODO(scheib): Will need c++ pointer eventually: - // private long mNativeBluetoothRemoteGattDescriptorAndroid; + private long mNativeBluetoothRemoteGattDescriptorAndroid; final Wrappers.BluetoothGattDescriptorWrapper mDescriptor; final ChromeBluetoothDevice mChromeDevice; - private ChromeBluetoothRemoteGattDescriptor( - // TODO(scheib): Will need c++ pointer eventually: - // long nativeBluetoothRemoteGattDescriptorAndroid, + private ChromeBluetoothRemoteGattDescriptor(long nativeBluetoothRemoteGattDescriptorAndroid, Wrappers.BluetoothGattDescriptorWrapper descriptorWrapper, ChromeBluetoothDevice chromeDevice) { - // TODO(scheib): Will need c++ pointer eventually: - // mNativeBluetoothRemoteGattDescriptorAndroid = - // nativeBluetoothRemoteGattDescriptorAndroid; + mNativeBluetoothRemoteGattDescriptorAndroid = nativeBluetoothRemoteGattDescriptorAndroid; mDescriptor = descriptorWrapper; mChromeDevice = chromeDevice; @@ -45,11 +40,27 @@ final class ChromeBluetoothRemoteGattDescriptor { @CalledByNative private void onBluetoothRemoteGattDescriptorAndroidDestruction() { Log.v(TAG, "ChromeBluetoothRemoteGattDescriptor Destroyed."); - // TODO(scheib): Will need c++ pointer eventually: - // mNativeBluetoothRemoteGattDescriptorAndroid = 0; + mNativeBluetoothRemoteGattDescriptorAndroid = 0; mChromeDevice.mWrapperToChromeDescriptorsMap.remove(mDescriptor); } + void onDescriptorRead(int status) { + Log.i(TAG, "onDescriptorRead status:%d==%s", status, + status == android.bluetooth.BluetoothGatt.GATT_SUCCESS ? "OK" : "Error"); + if (mNativeBluetoothRemoteGattDescriptorAndroid != 0) { + nativeOnRead( + mNativeBluetoothRemoteGattDescriptorAndroid, status, mDescriptor.getValue()); + } + } + + void onDescriptorWrite(int status) { + Log.i(TAG, "onDescriptorWrite status:%d==%s", status, + status == android.bluetooth.BluetoothGatt.GATT_SUCCESS ? "OK" : "Error"); + if (mNativeBluetoothRemoteGattDescriptorAndroid != 0) { + nativeOnWrite(mNativeBluetoothRemoteGattDescriptorAndroid, status); + } + } + // --------------------------------------------------------------------------------------------- // BluetoothRemoteGattDescriptorAndroid methods implemented in java: @@ -57,12 +68,9 @@ final class ChromeBluetoothRemoteGattDescriptor { // TODO(http://crbug.com/505554): Replace 'Object' with specific type when JNI fixed. @CalledByNative private static ChromeBluetoothRemoteGattDescriptor create( - // TODO(scheib): Will need c++ pointer eventually: - // long nativeBluetoothRemoteGattDescriptorAndroid, - Object bluetoothGattDescriptorWrapper, ChromeBluetoothDevice chromeDevice) { - return new ChromeBluetoothRemoteGattDescriptor( - // TODO(scheib): Will need c++ pointer eventually: - // nativeBluetoothRemoteGattDescriptorAndroid, + long nativeBluetoothRemoteGattDescriptorAndroid, Object bluetoothGattDescriptorWrapper, + ChromeBluetoothDevice chromeDevice) { + return new ChromeBluetoothRemoteGattDescriptor(nativeBluetoothRemoteGattDescriptorAndroid, (Wrappers.BluetoothGattDescriptorWrapper) bluetoothGattDescriptorWrapper, chromeDevice); } @@ -72,4 +80,38 @@ final class ChromeBluetoothRemoteGattDescriptor { private String getUUID() { return mDescriptor.getUuid().toString(); } + + // Implements BluetoothRemoteGattDescriptorAndroid::ReadRemoteDescriptor. + @CalledByNative + private boolean readRemoteDescriptor() { + if (!mChromeDevice.mBluetoothGatt.readDescriptor(mDescriptor)) { + Log.i(TAG, "readRemoteDescriptor readDescriptor failed."); + return false; + } + return true; + } + + // Implements BluetoothRemoteGattDescriptorAndroid::WriteRemoteDescriptor. + @CalledByNative + private boolean writeRemoteDescriptor(byte[] value) { + if (!mDescriptor.setValue(value)) { + Log.i(TAG, "writeRemoteDescriptor setValue failed."); + return false; + } + if (!mChromeDevice.mBluetoothGatt.writeDescriptor(mDescriptor)) { + Log.i(TAG, "writeRemoteDescriptor writeDescriptor failed."); + return false; + } + return true; + } + + // --------------------------------------------------------------------------------------------- + // BluetoothAdapterDevice C++ methods declared for access from java: + + // Binds to BluetoothRemoteGattDescriptorAndroid::OnRead. + native void nativeOnRead( + long nativeBluetoothRemoteGattDescriptorAndroid, int status, byte[] value); + + // Binds to BluetoothRemoteGattDescriptorAndroid::OnWrite. + native void nativeOnWrite(long nativeBluetoothRemoteGattDescriptorAndroid, int status); } diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java index 96634b3..b61c7cd 100644 --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java @@ -365,6 +365,10 @@ class Wrappers { return mGatt.writeCharacteristic(characteristic.mCharacteristic); } + boolean readDescriptor(BluetoothGattDescriptorWrapper descriptor) { + return mGatt.readDescriptor(descriptor.mDescriptor); + } + boolean writeDescriptor(BluetoothGattDescriptorWrapper descriptor) { return mGatt.writeDescriptor(descriptor.mDescriptor); } @@ -411,6 +415,20 @@ class Wrappers { } @Override + public void onDescriptorRead( + BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { + mWrapperCallback.onDescriptorRead( + mDeviceWrapper.mDescriptorsToWrappers.get(descriptor), status); + } + + @Override + public void onDescriptorWrite( + BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { + mWrapperCallback.onDescriptorWrite( + mDeviceWrapper.mDescriptorsToWrappers.get(descriptor), status); + } + + @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { mWrapperCallback.onConnectionStateChange(status, newState); } @@ -438,6 +456,10 @@ class Wrappers { BluetoothGattCharacteristicWrapper characteristic, int status); public abstract void onCharacteristicWrite( BluetoothGattCharacteristicWrapper characteristic, int status); + public abstract void onDescriptorRead( + BluetoothGattDescriptorWrapper descriptor, int status); + public abstract void onDescriptorWrite( + BluetoothGattDescriptorWrapper descriptor, int status); public abstract void onConnectionStateChange(int status, int newState); public abstract void onServicesDiscovered(int status); } @@ -505,7 +527,7 @@ class Wrappers { mDeviceWrapper.mDescriptorsToWrappers.get(descriptor); if (descriptorWrapper == null) { - descriptorWrapper = new BluetoothGattDescriptorWrapper(descriptor); + descriptorWrapper = new BluetoothGattDescriptorWrapper(descriptor, mDeviceWrapper); mDeviceWrapper.mDescriptorsToWrappers.put(descriptor, descriptorWrapper); } return descriptorWrapper; @@ -521,7 +543,8 @@ class Wrappers { BluetoothGattDescriptorWrapper descriptorWrapper = mDeviceWrapper.mDescriptorsToWrappers.get(descriptor); if (descriptorWrapper == null) { - descriptorWrapper = new BluetoothGattDescriptorWrapper(descriptor); + descriptorWrapper = + new BluetoothGattDescriptorWrapper(descriptor, mDeviceWrapper); mDeviceWrapper.mDescriptorsToWrappers.put(descriptor, descriptorWrapper); } descriptorsWrapped.add(descriptorWrapper); @@ -555,9 +578,16 @@ class Wrappers { */ static class BluetoothGattDescriptorWrapper { private final BluetoothGattDescriptor mDescriptor; + final BluetoothDeviceWrapper mDeviceWrapper; - public BluetoothGattDescriptorWrapper(BluetoothGattDescriptor descriptor) { + public BluetoothGattDescriptorWrapper( + BluetoothGattDescriptor descriptor, BluetoothDeviceWrapper deviceWrapper) { mDescriptor = descriptor; + mDeviceWrapper = deviceWrapper; + } + + public BluetoothGattCharacteristicWrapper getCharacteristic() { + return mDeviceWrapper.mCharacteristicsToWrappers.get(mDescriptor.getCharacteristic()); } public UUID getUuid() { |