Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(960)

Unified Diff: device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java

Issue 1502833002: bluetooth: android: Enable characteristic change notification events. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bta-check-null-
Patch Set: GattCharacteristicValueChanged_AfterDeleted Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java
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 696d8f71f0a25b3702caef586a928cfabc7d21b7..0fb460f66e534d037f7a7772a5b2762b4d28119b 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
@@ -4,11 +4,15 @@
package org.chromium.device.bluetooth;
+import android.bluetooth.BluetoothGattCharacteristic;
+import android.bluetooth.BluetoothGattDescriptor;
+
import org.chromium.base.Log;
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
@@ -47,10 +51,19 @@ final class ChromeBluetoothRemoteGattCharacteristic {
@CalledByNative
private void onBluetoothRemoteGattCharacteristicAndroidDestruction() {
Log.v(TAG, "ChromeBluetoothRemoteGattCharacteristic Destroyed.");
+ mChromeDevice.mBluetoothGatt.setCharacteristicNotification(mCharacteristic, false);
mNativeBluetoothRemoteGattCharacteristicAndroid = 0;
mChromeDevice.mWrapperToChromeCharacteristicsMap.remove(mCharacteristic);
}
+ void onCharacteristicChanged() {
+ Log.i(TAG, "onCharacteristicChanged");
+ if (mNativeBluetoothRemoteGattCharacteristicAndroid != 0) {
+ nativeOnChanged(
+ mNativeBluetoothRemoteGattCharacteristicAndroid, mCharacteristic.getValue());
+ }
+ }
+
void onCharacteristicRead(int status) {
Log.i(TAG, "onCharacteristicRead status:%d==%s", status,
status == android.bluetooth.BluetoothGatt.GATT_SUCCESS ? "OK" : "Error");
@@ -100,11 +113,45 @@ final class ChromeBluetoothRemoteGattCharacteristic {
// 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;
}
- Log.i(TAG, "startNotifySession.");
+
+ // 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;
}
@@ -150,6 +197,9 @@ final class ChromeBluetoothRemoteGattCharacteristic {
// ---------------------------------------------------------------------------------------------
// BluetoothAdapterDevice C++ methods declared for access from java:
+ // Binds to BluetoothRemoteGattCharacteristicAndroid::OnChanged.
+ native void nativeOnChanged(long nativeBluetoothRemoteGattCharacteristicAndroid, byte[] value);
+
// Binds to BluetoothRemoteGattCharacteristicAndroid::OnRead.
native void nativeOnRead(
long nativeBluetoothRemoteGattCharacteristicAndroid, int status, byte[] value);

Powered by Google App Engine
This is Rietveld 408576698