| 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" | |
| 10 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 11 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
| 12 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_android.h" | 12 #include "device/bluetooth/bluetooth_adapter_android.h" |
| 14 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" | 13 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" |
| 15 #include "jni/ChromeBluetoothDevice_jni.h" | 14 #include "jni/ChromeBluetoothDevice_jni.h" |
| 16 | 15 |
| 17 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
| 18 using base::android::AppendJavaStringArrayToStringVector; | |
| 19 using base::android::JavaParamRef; | 17 using base::android::JavaParamRef; |
| 20 | 18 |
| 21 namespace device { | 19 namespace device { |
| 22 namespace { | 20 namespace { |
| 23 void RecordConnectionSuccessResult(int32_t status) { | 21 void RecordConnectionSuccessResult(int32_t status) { |
| 24 UMA_HISTOGRAM_SPARSE_SLOWLY("Bluetooth.Android.GATTConnection.Success.Result", | 22 UMA_HISTOGRAM_SPARSE_SLOWLY("Bluetooth.Android.GATTConnection.Success.Result", |
| 25 status); | 23 status); |
| 26 } | 24 } |
| 27 void RecordConnectionFailureResult(int32_t status) { | 25 void RecordConnectionFailureResult(int32_t status) { |
| 28 UMA_HISTOGRAM_SPARSE_SLOWLY("Bluetooth.Android.GATTConnection.Failure.Result", | 26 UMA_HISTOGRAM_SPARSE_SLOWLY("Bluetooth.Android.GATTConnection.Failure.Result", |
| (...skipping 15 matching lines...) Expand all Loading... |
| 44 bluetooth_device_wrapper)); | 42 bluetooth_device_wrapper)); |
| 45 | 43 |
| 46 return device; | 44 return device; |
| 47 } | 45 } |
| 48 | 46 |
| 49 BluetoothDeviceAndroid::~BluetoothDeviceAndroid() { | 47 BluetoothDeviceAndroid::~BluetoothDeviceAndroid() { |
| 50 Java_ChromeBluetoothDevice_onBluetoothDeviceAndroidDestruction( | 48 Java_ChromeBluetoothDevice_onBluetoothDeviceAndroidDestruction( |
| 51 AttachCurrentThread(), j_device_); | 49 AttachCurrentThread(), j_device_); |
| 52 } | 50 } |
| 53 | 51 |
| 54 void BluetoothDeviceAndroid::UpdateAdvertisedUUIDs( | |
| 55 jobjectArray advertised_uuids) { | |
| 56 JNIEnv* env = AttachCurrentThread(); | |
| 57 std::vector<std::string> uuid_strings; | |
| 58 AppendJavaStringArrayToStringVector(env, advertised_uuids, &uuid_strings); | |
| 59 | |
| 60 advertised_uuids_ = UUIDList(uuid_strings.begin(), uuid_strings.end()); | |
| 61 } | |
| 62 | |
| 63 // static | 52 // static |
| 64 bool BluetoothDeviceAndroid::RegisterJNI(JNIEnv* env) { | 53 bool BluetoothDeviceAndroid::RegisterJNI(JNIEnv* env) { |
| 65 return RegisterNativesImpl(env); // Generated in ChromeBluetoothDevice_jni.h | 54 return RegisterNativesImpl(env); // Generated in ChromeBluetoothDevice_jni.h |
| 66 } | 55 } |
| 67 | 56 |
| 68 base::android::ScopedJavaLocalRef<jobject> | 57 base::android::ScopedJavaLocalRef<jobject> |
| 69 BluetoothDeviceAndroid::GetJavaObject() { | 58 BluetoothDeviceAndroid::GetJavaObject() { |
| 70 return base::android::ScopedJavaLocalRef<jobject>(j_device_); | 59 return base::android::ScopedJavaLocalRef<jobject>(j_device_); |
| 71 } | 60 } |
| 72 | 61 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 // We assume that if there are any pending connection callbacks there | 223 // We assume that if there are any pending connection callbacks there |
| 235 // was a failed connection attempt. | 224 // was a failed connection attempt. |
| 236 RecordConnectionFailureResult(status); | 225 RecordConnectionFailureResult(status); |
| 237 // TODO(ortuno): Return an error code based on |status| | 226 // TODO(ortuno): Return an error code based on |status| |
| 238 // http://crbug.com/578191 | 227 // http://crbug.com/578191 |
| 239 DidFailToConnectGatt(ERROR_FAILED); | 228 DidFailToConnectGatt(ERROR_FAILED); |
| 240 } else { | 229 } else { |
| 241 // Otherwise an existing connection was terminated. | 230 // Otherwise an existing connection was terminated. |
| 242 RecordConnectionTerminatedResult(status); | 231 RecordConnectionTerminatedResult(status); |
| 243 gatt_services_.clear(); | 232 gatt_services_.clear(); |
| 244 service_uuids_.clear(); | 233 device_uuids_.ClearServiceUUIDs(); |
| 245 SetGattServicesDiscoveryComplete(false); | 234 SetGattServicesDiscoveryComplete(false); |
| 246 DidDisconnectGatt(); | 235 DidDisconnectGatt(); |
| 247 } | 236 } |
| 248 } | 237 } |
| 249 | 238 |
| 250 void BluetoothDeviceAndroid::OnGattServicesDiscovered( | 239 void BluetoothDeviceAndroid::OnGattServicesDiscovered( |
| 251 JNIEnv* env, | 240 JNIEnv* env, |
| 252 const JavaParamRef<jobject>& jcaller) { | 241 const JavaParamRef<jobject>& jcaller) { |
| 253 UpdateServiceUUIDs(); | 242 device_uuids_.ReplaceServiceUUIDs(gatt_services_); |
| 254 SetGattServicesDiscoveryComplete(true); | 243 SetGattServicesDiscoveryComplete(true); |
| 255 adapter_->NotifyGattServicesDiscovered(this); | 244 adapter_->NotifyGattServicesDiscovered(this); |
| 245 adapter_->NotifyDeviceChanged(this); |
| 256 } | 246 } |
| 257 | 247 |
| 258 void BluetoothDeviceAndroid::CreateGattRemoteService( | 248 void BluetoothDeviceAndroid::CreateGattRemoteService( |
| 259 JNIEnv* env, | 249 JNIEnv* env, |
| 260 const JavaParamRef<jobject>& caller, | 250 const JavaParamRef<jobject>& caller, |
| 261 const JavaParamRef<jstring>& instance_id, | 251 const JavaParamRef<jstring>& instance_id, |
| 262 const JavaParamRef<jobject>& | 252 const JavaParamRef<jobject>& |
| 263 bluetooth_gatt_service_wrapper) { // BluetoothGattServiceWrapper | 253 bluetooth_gatt_service_wrapper) { // BluetoothGattServiceWrapper |
| 264 std::string instance_id_string = | 254 std::string instance_id_string = |
| 265 base::android::ConvertJavaStringToUTF8(env, instance_id); | 255 base::android::ConvertJavaStringToUTF8(env, instance_id); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 283 void BluetoothDeviceAndroid::CreateGattConnectionImpl() { | 273 void BluetoothDeviceAndroid::CreateGattConnectionImpl() { |
| 284 Java_ChromeBluetoothDevice_createGattConnectionImpl( | 274 Java_ChromeBluetoothDevice_createGattConnectionImpl( |
| 285 AttachCurrentThread(), j_device_, base::android::GetApplicationContext()); | 275 AttachCurrentThread(), j_device_, base::android::GetApplicationContext()); |
| 286 } | 276 } |
| 287 | 277 |
| 288 void BluetoothDeviceAndroid::DisconnectGatt() { | 278 void BluetoothDeviceAndroid::DisconnectGatt() { |
| 289 Java_ChromeBluetoothDevice_disconnectGatt(AttachCurrentThread(), j_device_); | 279 Java_ChromeBluetoothDevice_disconnectGatt(AttachCurrentThread(), j_device_); |
| 290 } | 280 } |
| 291 | 281 |
| 292 } // namespace device | 282 } // namespace device |
| OLD | NEW |