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_remote_gatt_characteristic_android.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 jobject /* ChromeBluetoothDevice */ chrome_bluetooth_device) { | 36 jobject /* ChromeBluetoothDevice */ chrome_bluetooth_device) { |
37 std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid> characteristic( | 37 std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid> characteristic( |
38 new BluetoothRemoteGattCharacteristicAndroid(adapter, service, | 38 new BluetoothRemoteGattCharacteristicAndroid(adapter, service, |
39 instance_id)); | 39 instance_id)); |
40 | 40 |
41 JNIEnv* env = AttachCurrentThread(); | 41 JNIEnv* env = AttachCurrentThread(); |
42 characteristic->j_characteristic_.Reset( | 42 characteristic->j_characteristic_.Reset( |
43 Java_ChromeBluetoothRemoteGattCharacteristic_create( | 43 Java_ChromeBluetoothRemoteGattCharacteristic_create( |
44 env, reinterpret_cast<intptr_t>(characteristic.get()), | 44 env, reinterpret_cast<intptr_t>(characteristic.get()), |
45 bluetooth_gatt_characteristic_wrapper, | 45 bluetooth_gatt_characteristic_wrapper, |
46 base::android::ConvertUTF8ToJavaString(env, instance_id).obj(), | 46 base::android::ConvertUTF8ToJavaString(env, instance_id), |
47 chrome_bluetooth_device)); | 47 chrome_bluetooth_device)); |
48 | 48 |
49 return characteristic; | 49 return characteristic; |
50 } | 50 } |
51 | 51 |
52 BluetoothRemoteGattCharacteristicAndroid:: | 52 BluetoothRemoteGattCharacteristicAndroid:: |
53 ~BluetoothRemoteGattCharacteristicAndroid() { | 53 ~BluetoothRemoteGattCharacteristicAndroid() { |
54 Java_ChromeBluetoothRemoteGattCharacteristic_onBluetoothRemoteGattCharacterist
icAndroidDestruction( | 54 Java_ChromeBluetoothRemoteGattCharacteristic_onBluetoothRemoteGattCharacterist
icAndroidDestruction( |
55 AttachCurrentThread(), j_characteristic_.obj()); | 55 AttachCurrentThread(), j_characteristic_); |
56 | 56 |
57 if (pending_start_notify_calls_.size()) { | 57 if (pending_start_notify_calls_.size()) { |
58 OnStartNotifySessionError( | 58 OnStartNotifySessionError( |
59 device::BluetoothRemoteGattService::GATT_ERROR_FAILED); | 59 device::BluetoothRemoteGattService::GATT_ERROR_FAILED); |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 // static | 63 // static |
64 bool BluetoothRemoteGattCharacteristicAndroid::RegisterJNI(JNIEnv* env) { | 64 bool BluetoothRemoteGattCharacteristicAndroid::RegisterJNI(JNIEnv* env) { |
65 return RegisterNativesImpl( | 65 return RegisterNativesImpl( |
66 env); // Generated in ChromeBluetoothRemoteGattCharacteristic_jni.h | 66 env); // Generated in ChromeBluetoothRemoteGattCharacteristic_jni.h |
67 } | 67 } |
68 | 68 |
69 base::android::ScopedJavaLocalRef<jobject> | 69 base::android::ScopedJavaLocalRef<jobject> |
70 BluetoothRemoteGattCharacteristicAndroid::GetJavaObject() { | 70 BluetoothRemoteGattCharacteristicAndroid::GetJavaObject() { |
71 return base::android::ScopedJavaLocalRef<jobject>(j_characteristic_); | 71 return base::android::ScopedJavaLocalRef<jobject>(j_characteristic_); |
72 } | 72 } |
73 | 73 |
74 std::string BluetoothRemoteGattCharacteristicAndroid::GetIdentifier() const { | 74 std::string BluetoothRemoteGattCharacteristicAndroid::GetIdentifier() const { |
75 return instance_id_; | 75 return instance_id_; |
76 } | 76 } |
77 | 77 |
78 BluetoothUUID BluetoothRemoteGattCharacteristicAndroid::GetUUID() const { | 78 BluetoothUUID BluetoothRemoteGattCharacteristicAndroid::GetUUID() const { |
79 return device::BluetoothUUID(ConvertJavaStringToUTF8( | 79 return device::BluetoothUUID(ConvertJavaStringToUTF8( |
80 Java_ChromeBluetoothRemoteGattCharacteristic_getUUID( | 80 Java_ChromeBluetoothRemoteGattCharacteristic_getUUID( |
81 AttachCurrentThread(), j_characteristic_.obj()))); | 81 AttachCurrentThread(), j_characteristic_))); |
82 } | 82 } |
83 | 83 |
84 const std::vector<uint8_t>& BluetoothRemoteGattCharacteristicAndroid::GetValue() | 84 const std::vector<uint8_t>& BluetoothRemoteGattCharacteristicAndroid::GetValue() |
85 const { | 85 const { |
86 return value_; | 86 return value_; |
87 } | 87 } |
88 | 88 |
89 BluetoothRemoteGattService* | 89 BluetoothRemoteGattService* |
90 BluetoothRemoteGattCharacteristicAndroid::GetService() const { | 90 BluetoothRemoteGattCharacteristicAndroid::GetService() const { |
91 return service_; | 91 return service_; |
92 } | 92 } |
93 | 93 |
94 BluetoothRemoteGattCharacteristic::Properties | 94 BluetoothRemoteGattCharacteristic::Properties |
95 BluetoothRemoteGattCharacteristicAndroid::GetProperties() const { | 95 BluetoothRemoteGattCharacteristicAndroid::GetProperties() const { |
96 return Java_ChromeBluetoothRemoteGattCharacteristic_getProperties( | 96 return Java_ChromeBluetoothRemoteGattCharacteristic_getProperties( |
97 AttachCurrentThread(), j_characteristic_.obj()); | 97 AttachCurrentThread(), j_characteristic_); |
98 } | 98 } |
99 | 99 |
100 BluetoothRemoteGattCharacteristic::Permissions | 100 BluetoothRemoteGattCharacteristic::Permissions |
101 BluetoothRemoteGattCharacteristicAndroid::GetPermissions() const { | 101 BluetoothRemoteGattCharacteristicAndroid::GetPermissions() const { |
102 NOTIMPLEMENTED(); | 102 NOTIMPLEMENTED(); |
103 return 0; | 103 return 0; |
104 } | 104 } |
105 | 105 |
106 bool BluetoothRemoteGattCharacteristicAndroid::IsNotifying() const { | 106 bool BluetoothRemoteGattCharacteristicAndroid::IsNotifying() const { |
107 NOTIMPLEMENTED(); | 107 NOTIMPLEMENTED(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 base::ThreadTaskRunnerHandle::Get()->PostTask( | 160 base::ThreadTaskRunnerHandle::Get()->PostTask( |
161 FROM_HERE, | 161 FROM_HERE, |
162 base::Bind(error_callback, | 162 base::Bind(error_callback, |
163 (ccc_descriptor.size() == 0) | 163 (ccc_descriptor.size() == 0) |
164 ? BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED | 164 ? BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED |
165 : BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 165 : BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
166 return; | 166 return; |
167 } | 167 } |
168 | 168 |
169 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( | 169 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( |
170 AttachCurrentThread(), j_characteristic_.obj(), true)) { | 170 AttachCurrentThread(), j_characteristic_, true)) { |
171 LOG(ERROR) << "Error enabling characteristic notification"; | 171 LOG(ERROR) << "Error enabling characteristic notification"; |
172 base::ThreadTaskRunnerHandle::Get()->PostTask( | 172 base::ThreadTaskRunnerHandle::Get()->PostTask( |
173 FROM_HERE, base::Bind(error_callback, | 173 FROM_HERE, base::Bind(error_callback, |
174 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 174 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
175 return; | 175 return; |
176 } | 176 } |
177 | 177 |
178 std::vector<uint8_t> value(2); | 178 std::vector<uint8_t> value(2); |
179 value[0] = hasNotify ? 1 : 2; | 179 value[0] = hasNotify ? 1 : 2; |
180 | 180 |
(...skipping 13 matching lines...) Expand all Loading... |
194 const ErrorCallback& error_callback) { | 194 const ErrorCallback& error_callback) { |
195 if (read_pending_ || write_pending_) { | 195 if (read_pending_ || write_pending_) { |
196 base::ThreadTaskRunnerHandle::Get()->PostTask( | 196 base::ThreadTaskRunnerHandle::Get()->PostTask( |
197 FROM_HERE, | 197 FROM_HERE, |
198 base::Bind(error_callback, | 198 base::Bind(error_callback, |
199 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | 199 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
200 return; | 200 return; |
201 } | 201 } |
202 | 202 |
203 if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic( | 203 if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic( |
204 AttachCurrentThread(), j_characteristic_.obj())) { | 204 AttachCurrentThread(), j_characteristic_)) { |
205 base::ThreadTaskRunnerHandle::Get()->PostTask( | 205 base::ThreadTaskRunnerHandle::Get()->PostTask( |
206 FROM_HERE, base::Bind(error_callback, | 206 FROM_HERE, base::Bind(error_callback, |
207 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 207 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
208 return; | 208 return; |
209 } | 209 } |
210 | 210 |
211 read_pending_ = true; | 211 read_pending_ = true; |
212 read_callback_ = callback; | 212 read_callback_ = callback; |
213 read_error_callback_ = error_callback; | 213 read_error_callback_ = error_callback; |
214 } | 214 } |
215 | 215 |
216 void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic( | 216 void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic( |
217 const std::vector<uint8_t>& new_value, | 217 const std::vector<uint8_t>& new_value, |
218 const base::Closure& callback, | 218 const base::Closure& callback, |
219 const ErrorCallback& error_callback) { | 219 const ErrorCallback& error_callback) { |
220 if (read_pending_ || write_pending_) { | 220 if (read_pending_ || write_pending_) { |
221 base::ThreadTaskRunnerHandle::Get()->PostTask( | 221 base::ThreadTaskRunnerHandle::Get()->PostTask( |
222 FROM_HERE, | 222 FROM_HERE, |
223 base::Bind(error_callback, | 223 base::Bind(error_callback, |
224 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | 224 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
225 return; | 225 return; |
226 } | 226 } |
227 | 227 |
228 JNIEnv* env = AttachCurrentThread(); | 228 JNIEnv* env = AttachCurrentThread(); |
229 if (!Java_ChromeBluetoothRemoteGattCharacteristic_writeRemoteCharacteristic( | 229 if (!Java_ChromeBluetoothRemoteGattCharacteristic_writeRemoteCharacteristic( |
230 env, j_characteristic_.obj(), | 230 env, j_characteristic_, |
231 base::android::ToJavaByteArray(env, new_value).obj())) { | 231 base::android::ToJavaByteArray(env, new_value))) { |
232 base::ThreadTaskRunnerHandle::Get()->PostTask( | 232 base::ThreadTaskRunnerHandle::Get()->PostTask( |
233 FROM_HERE, base::Bind(error_callback, | 233 FROM_HERE, base::Bind(error_callback, |
234 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 234 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
235 return; | 235 return; |
236 } | 236 } |
237 | 237 |
238 write_pending_ = true; | 238 write_pending_ = true; |
239 write_callback_ = callback; | 239 write_callback_ = callback; |
240 write_error_callback_ = error_callback; | 240 write_error_callback_ = error_callback; |
241 } | 241 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 BluetoothRemoteGattServiceAndroid* service, | 340 BluetoothRemoteGattServiceAndroid* service, |
341 const std::string& instance_id) | 341 const std::string& instance_id) |
342 : adapter_(adapter), service_(service), instance_id_(instance_id) {} | 342 : adapter_(adapter), service_(service), instance_id_(instance_id) {} |
343 | 343 |
344 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 344 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
345 const { | 345 const { |
346 if (!descriptors_.empty()) | 346 if (!descriptors_.empty()) |
347 return; | 347 return; |
348 | 348 |
349 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 349 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
350 AttachCurrentThread(), j_characteristic_.obj()); | 350 AttachCurrentThread(), j_characteristic_); |
351 } | 351 } |
352 | 352 |
353 } // namespace device | 353 } // namespace device |
OLD | NEW |