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 #include "device/bluetooth/bluetooth_device_android.h" | 5 #include "device/bluetooth/bluetooth_device_android.h" |
6 | 6 |
7 #include "base/android/context_utils.h" | 7 #include "base/android/context_utils.h" |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/android/jni_array.h" | 9 #include "base/android/jni_array.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/metrics/sparse_histogram.h" | |
11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
12 #include "device/bluetooth/bluetooth_adapter_android.h" | 13 #include "device/bluetooth/bluetooth_adapter_android.h" |
13 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" | 14 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" |
14 #include "jni/ChromeBluetoothDevice_jni.h" | 15 #include "jni/ChromeBluetoothDevice_jni.h" |
15 | 16 |
16 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
17 using base::android::AppendJavaStringArrayToStringVector; | 18 using base::android::AppendJavaStringArrayToStringVector; |
18 | 19 |
19 namespace device { | 20 namespace device { |
21 namespace { | |
22 void RecordConnectionSuccessResult(int32_t status) { | |
23 UMA_HISTOGRAM_SPARSE_SLOWLY("Bluetooth.Android.GATTConnection.Success.Result", | |
24 status); | |
25 } | |
26 void RecordConnectionFailureResult(int32_t status) { | |
27 UMA_HISTOGRAM_SPARSE_SLOWLY("Bluetooth.Android.GATTConnection.Failure.Result", | |
28 status); | |
29 } | |
30 void RecordConnectionTerminatedResult(int32_t status) { | |
31 UMA_HISTOGRAM_SPARSE_SLOWLY( | |
32 "Bluetooth.Android.GATTConnection.Disconnected.Result", status); | |
33 } | |
34 } // namespace | |
20 | 35 |
21 BluetoothDeviceAndroid* BluetoothDeviceAndroid::Create( | 36 BluetoothDeviceAndroid* BluetoothDeviceAndroid::Create( |
22 BluetoothAdapterAndroid* adapter, | 37 BluetoothAdapterAndroid* adapter, |
23 jobject bluetooth_device_wrapper) { // Java Type: bluetoothDeviceWrapper | 38 jobject bluetooth_device_wrapper) { // Java Type: bluetoothDeviceWrapper |
24 BluetoothDeviceAndroid* device = new BluetoothDeviceAndroid(adapter); | 39 BluetoothDeviceAndroid* device = new BluetoothDeviceAndroid(adapter); |
25 | 40 |
26 device->j_device_.Reset(Java_ChromeBluetoothDevice_create( | 41 device->j_device_.Reset(Java_ChromeBluetoothDevice_create( |
27 AttachCurrentThread(), reinterpret_cast<intptr_t>(device), | 42 AttachCurrentThread(), reinterpret_cast<intptr_t>(device), |
28 bluetooth_device_wrapper)); | 43 bluetooth_device_wrapper)); |
29 | 44 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 NOTIMPLEMENTED(); | 216 NOTIMPLEMENTED(); |
202 } | 217 } |
203 | 218 |
204 void BluetoothDeviceAndroid::OnConnectionStateChange( | 219 void BluetoothDeviceAndroid::OnConnectionStateChange( |
205 JNIEnv* env, | 220 JNIEnv* env, |
206 const JavaParamRef<jobject>& jcaller, | 221 const JavaParamRef<jobject>& jcaller, |
207 int32_t status, | 222 int32_t status, |
208 bool connected) { | 223 bool connected) { |
209 gatt_connected_ = connected; | 224 gatt_connected_ = connected; |
210 if (gatt_connected_) { | 225 if (gatt_connected_) { |
226 RecordConnectionSuccessResult(status); | |
211 DidConnectGatt(); | 227 DidConnectGatt(); |
228 } else if (!create_gatt_connection_error_callbacks_.empty()) { | |
229 // We assume that if there are any pending connection callbacks there | |
230 // was a failed connection attempt. | |
231 RecordConnectionFailureResult(status); | |
232 // TODO(ortuno): Return an error code based on |status| | |
233 // http://crbug.com/578191 | |
234 DidFailToConnectGatt(ERROR_FAILED); | |
scheib
2016/01/16 23:59:52
Add a LOG for now to help us understand codes in b
ortuno
2016/01/19 20:40:08
I didn't add log because Android already logs it.
scheib
2016/01/19 20:58:22
No, the BluetoothGattCallbackImpl log is sufficien
| |
212 } else { | 235 } else { |
236 // Otherwise an existing connection was terminated. | |
237 RecordConnectionTerminatedResult(status); | |
213 gatt_services_.clear(); | 238 gatt_services_.clear(); |
214 SetGattServicesDiscoveryComplete(false); | 239 SetGattServicesDiscoveryComplete(false); |
215 | 240 DidDisconnectGatt(); |
216 switch (status) { // Constants are from android.bluetooth.BluetoothGatt. | |
217 case 0x0000008f: // GATT_CONNECTION_CONGESTED | |
218 return DidFailToConnectGatt(ERROR_CONNECTION_CONGESTED); | |
219 case 0x00000101: // GATT_FAILURE | |
220 return DidFailToConnectGatt(ERROR_FAILED); | |
221 case 0x00000005: // GATT_INSUFFICIENT_AUTHENTICATION | |
222 return DidFailToConnectGatt(ERROR_AUTH_FAILED); | |
223 case 0x0000000f: // GATT_INSUFFICIENT_ENCRYPTION | |
224 return DidFailToConnectGatt(ERROR_INSUFFICIENT_ENCRYPTION); | |
225 case 0x0000000d: // GATT_INVALID_ATTRIBUTE_LENGTH | |
226 return DidFailToConnectGatt(ERROR_ATTRIBUTE_LENGTH_INVALID); | |
227 case 0x00000007: // GATT_INVALID_OFFSET | |
228 return DidFailToConnectGatt(ERROR_OFFSET_INVALID); | |
229 case 0x00000002: // GATT_READ_NOT_PERMITTED | |
230 return DidFailToConnectGatt(ERROR_READ_NOT_PERMITTED); | |
231 case 0x00000006: // GATT_REQUEST_NOT_SUPPORTED | |
232 return DidFailToConnectGatt(ERROR_REQUEST_NOT_SUPPORTED); | |
233 case 0x00000000: // GATT_SUCCESS | |
234 return DidDisconnectGatt(); | |
235 case 0x00000003: // GATT_WRITE_NOT_PERMITTED | |
236 return DidFailToConnectGatt(ERROR_WRITE_NOT_PERMITTED); | |
237 default: | |
238 VLOG(1) << "Unhandled status: " << status; | |
239 return DidFailToConnectGatt(ERROR_UNKNOWN); | |
240 } | |
241 } | 241 } |
242 } | 242 } |
243 | 243 |
244 void BluetoothDeviceAndroid::OnGattServicesDiscovered( | 244 void BluetoothDeviceAndroid::OnGattServicesDiscovered( |
245 JNIEnv* env, | 245 JNIEnv* env, |
246 const JavaParamRef<jobject>& jcaller) { | 246 const JavaParamRef<jobject>& jcaller) { |
247 SetGattServicesDiscoveryComplete(true); | 247 SetGattServicesDiscoveryComplete(true); |
248 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, GetAdapter()->GetObservers(), | 248 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, GetAdapter()->GetObservers(), |
249 GattServicesDiscovered(GetAdapter(), this)); | 249 GattServicesDiscovered(GetAdapter(), this)); |
250 } | 250 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 AttachCurrentThread(), j_device_.obj(), | 284 AttachCurrentThread(), j_device_.obj(), |
285 base::android::GetApplicationContext()); | 285 base::android::GetApplicationContext()); |
286 } | 286 } |
287 | 287 |
288 void BluetoothDeviceAndroid::DisconnectGatt() { | 288 void BluetoothDeviceAndroid::DisconnectGatt() { |
289 Java_ChromeBluetoothDevice_disconnectGatt(AttachCurrentThread(), | 289 Java_ChromeBluetoothDevice_disconnectGatt(AttachCurrentThread(), |
290 j_device_.obj()); | 290 j_device_.obj()); |
291 } | 291 } |
292 | 292 |
293 } // namespace device | 293 } // namespace device |
OLD | NEW |