OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.bluetooth; | 5 package org.chromium.device.bluetooth; |
6 | 6 |
7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
8 import android.bluetooth.BluetoothGattCharacteristic; | |
9 import android.bluetooth.BluetoothGattDescriptor; | |
10 import android.os.Build; | 8 import android.os.Build; |
11 | 9 |
12 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
13 import org.chromium.base.annotations.CalledByNative; | 11 import org.chromium.base.annotations.CalledByNative; |
14 import org.chromium.base.annotations.JNINamespace; | 12 import org.chromium.base.annotations.JNINamespace; |
15 | 13 |
16 import java.util.List; | 14 import java.util.List; |
17 import java.util.UUID; | |
18 | 15 |
19 /** | 16 /** |
20 * Exposes android.bluetooth.BluetoothGattCharacteristic as necessary | 17 * Exposes android.bluetooth.BluetoothGattCharacteristic as necessary |
21 * for C++ device::BluetoothRemoteGattCharacteristicAndroid. | 18 * for C++ device::BluetoothRemoteGattCharacteristicAndroid. |
22 * | 19 * |
23 * Lifetime is controlled by | 20 * Lifetime is controlled by |
24 * device::BluetoothRemoteGattCharacteristicAndroid. | 21 * device::BluetoothRemoteGattCharacteristicAndroid. |
25 */ | 22 */ |
26 @JNINamespace("device") | 23 @JNINamespace("device") |
27 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 24 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 return mCharacteristic.getUuid().toString(); | 105 return mCharacteristic.getUuid().toString(); |
109 } | 106 } |
110 | 107 |
111 // Implements BluetoothRemoteGattCharacteristicAndroid::GetProperties. | 108 // Implements BluetoothRemoteGattCharacteristicAndroid::GetProperties. |
112 @CalledByNative | 109 @CalledByNative |
113 private int getProperties() { | 110 private int getProperties() { |
114 // TODO(scheib): Must read Extended Properties Descriptor. crbug.com/548
449 | 111 // TODO(scheib): Must read Extended Properties Descriptor. crbug.com/548
449 |
115 return mCharacteristic.getProperties(); | 112 return mCharacteristic.getProperties(); |
116 } | 113 } |
117 | 114 |
118 // Implements BluetoothRemoteGattCharacteristicAndroid::StartNotifySession. | |
119 @CalledByNative | |
120 private boolean startNotifySession() { | |
121 // Verify properties first, to provide clearest error log. | |
122 int properties = mCharacteristic.getProperties(); | |
123 boolean hasNotify = (properties & BluetoothGattCharacteristic.PROPERTY_N
OTIFY) != 0; | |
124 boolean hasIndicate = (properties & BluetoothGattCharacteristic.PROPERTY
_INDICATE) != 0; | |
125 if (!hasNotify && !hasIndicate) { | |
126 Log.v(TAG, "startNotifySession failed! Characteristic needs NOTIFY o
r INDICATE."); | |
127 return false; | |
128 } | |
129 | |
130 // Find config descriptor. | |
131 Wrappers.BluetoothGattDescriptorWrapper clientCharacteristicConfiguratio
nDescriptor = | |
132 mCharacteristic.getDescriptor(UUID.fromString( | |
133 "00002902-0000-1000-8000-00805F9B34FB" /* Config's stand
ard UUID*/)); | |
134 if (clientCharacteristicConfigurationDescriptor == null) { | |
135 Log.v(TAG, "startNotifySession config descriptor failed!"); | |
136 return false; | |
137 } | |
138 | |
139 // Request Android route onCharacteristicChanged notifications for this
characteristic. | |
140 if (!mChromeDevice.mBluetoothGatt.setCharacteristicNotification(mCharact
eristic, true)) { | |
141 Log.i(TAG, "startNotifySession setCharacteristicNotification failed.
"); | |
142 return false; | |
143 } | |
144 | |
145 // Enable notification on remote device's characteristic: | |
146 if (!clientCharacteristicConfigurationDescriptor.setValue(hasNotify | |
147 ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE | |
148 : BluetoothGattDescriptor.ENABLE_INDICATION_VALUE))
{ | |
149 Log.v(TAG, "startNotifySession descriptor setValue failed!"); | |
150 return false; | |
151 } | |
152 Log.v(TAG, hasNotify ? "startNotifySession NOTIFY." : "startNotifySessio
n INDICATE."); | |
153 | |
154 if (!mChromeDevice.mBluetoothGatt.writeDescriptor( | |
155 clientCharacteristicConfigurationDescriptor)) { | |
156 Log.i(TAG, "startNotifySession writeDescriptor failed!"); | |
157 return false; | |
158 } | |
159 | |
160 return true; | |
161 } | |
162 | |
163 // Implements BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteri
stic. | 115 // Implements BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteri
stic. |
164 @CalledByNative | 116 @CalledByNative |
165 private boolean readRemoteCharacteristic() { | 117 private boolean readRemoteCharacteristic() { |
166 if (!mChromeDevice.mBluetoothGatt.readCharacteristic(mCharacteristic)) { | 118 if (!mChromeDevice.mBluetoothGatt.readCharacteristic(mCharacteristic)) { |
167 Log.i(TAG, "readRemoteCharacteristic readCharacteristic failed."); | 119 Log.i(TAG, "readRemoteCharacteristic readCharacteristic failed."); |
168 return false; | 120 return false; |
169 } | 121 } |
170 return true; | 122 return true; |
171 } | 123 } |
172 | 124 |
173 // Implements BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacter
istic. | 125 // Implements BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacter
istic. |
174 @CalledByNative | 126 @CalledByNative |
175 private boolean writeRemoteCharacteristic(byte[] value) { | 127 private boolean writeRemoteCharacteristic(byte[] value) { |
176 if (!mCharacteristic.setValue(value)) { | 128 if (!mCharacteristic.setValue(value)) { |
177 Log.i(TAG, "writeRemoteCharacteristic setValue failed."); | 129 Log.i(TAG, "writeRemoteCharacteristic setValue failed."); |
178 return false; | 130 return false; |
179 } | 131 } |
180 if (!mChromeDevice.mBluetoothGatt.writeCharacteristic(mCharacteristic))
{ | 132 if (!mChromeDevice.mBluetoothGatt.writeCharacteristic(mCharacteristic))
{ |
181 Log.i(TAG, "writeRemoteCharacteristic writeCharacteristic failed."); | 133 Log.i(TAG, "writeRemoteCharacteristic writeCharacteristic failed."); |
182 return false; | 134 return false; |
183 } | 135 } |
184 return true; | 136 return true; |
185 } | 137 } |
186 | 138 |
| 139 // Enable or disable the notifications for this characteristic. |
| 140 @CalledByNative |
| 141 private boolean setCharacteristicNotification(boolean enabled) { |
| 142 return mChromeDevice.mBluetoothGatt.setCharacteristicNotification(mChara
cteristic, enabled); |
| 143 } |
| 144 |
187 // Creates objects for all descriptors. Designed only to be called by | 145 // Creates objects for all descriptors. Designed only to be called by |
188 // BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated. | 146 // BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated. |
189 @CalledByNative | 147 @CalledByNative |
190 private void createDescriptors() { | 148 private void createDescriptors() { |
191 List<Wrappers.BluetoothGattDescriptorWrapper> descriptors = | 149 List<Wrappers.BluetoothGattDescriptorWrapper> descriptors = |
192 mCharacteristic.getDescriptors(); | 150 mCharacteristic.getDescriptors(); |
193 for (Wrappers.BluetoothGattDescriptorWrapper descriptor : descriptors) { | 151 for (Wrappers.BluetoothGattDescriptorWrapper descriptor : descriptors) { |
194 // Create an adapter unique descriptor ID. | 152 // Create an adapter unique descriptor ID. |
195 // TODO(crbug.com/576900) Unique descriptorInstanceId duplicate UUID
values. | 153 // TODO(crbug.com/576900) Unique descriptorInstanceId duplicate UUID
values. |
196 String descriptorInstanceId = mInstanceId + "/" + descriptor.getUuid
().toString(); | 154 String descriptorInstanceId = mInstanceId + "/" + descriptor.getUuid
().toString(); |
(...skipping 14 matching lines...) Expand all Loading... |
211 | 169 |
212 // Binds to BluetoothRemoteGattCharacteristicAndroid::OnWrite. | 170 // Binds to BluetoothRemoteGattCharacteristicAndroid::OnWrite. |
213 native void nativeOnWrite(long nativeBluetoothRemoteGattCharacteristicAndroi
d, int status); | 171 native void nativeOnWrite(long nativeBluetoothRemoteGattCharacteristicAndroi
d, int status); |
214 | 172 |
215 // Binds to BluetoothRemoteGattCharacteristicAndroid::CreateGattRemoteDescri
ptor. | 173 // Binds to BluetoothRemoteGattCharacteristicAndroid::CreateGattRemoteDescri
ptor. |
216 // TODO(http://crbug.com/505554): Replace 'Object' with specific type when J
NI fixed. | 174 // TODO(http://crbug.com/505554): Replace 'Object' with specific type when J
NI fixed. |
217 private native void nativeCreateGattRemoteDescriptor( | 175 private native void nativeCreateGattRemoteDescriptor( |
218 long nativeBluetoothRemoteGattCharacteristicAndroid, String instance
Id, | 176 long nativeBluetoothRemoteGattCharacteristicAndroid, String instance
Id, |
219 Object bluetoothGattDescriptorWrapper, Object chromeBluetoothDevice)
; | 177 Object bluetoothGattDescriptorWrapper, Object chromeBluetoothDevice)
; |
220 } | 178 } |
OLD | NEW |