| 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 "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 143 |
| 144 Properties properties = GetProperties(); | 144 Properties properties = GetProperties(); |
| 145 | 145 |
| 146 bool hasNotify = properties & PROPERTY_NOTIFY; | 146 bool hasNotify = properties & PROPERTY_NOTIFY; |
| 147 bool hasIndicate = properties & PROPERTY_INDICATE; | 147 bool hasIndicate = properties & PROPERTY_INDICATE; |
| 148 | 148 |
| 149 if (!hasNotify && !hasIndicate) { | 149 if (!hasNotify && !hasIndicate) { |
| 150 LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; | 150 LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; |
| 151 base::MessageLoop::current()->PostTask( | 151 base::MessageLoop::current()->PostTask( |
| 152 FROM_HERE, | 152 FROM_HERE, |
| 153 base::Bind(error_callback, | 153 base::Bind(error_callback, BluetoothGattService::GATT_ERROR_FAILED)); |
| 154 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | |
| 155 return; | 154 return; |
| 156 } | 155 } |
| 157 | 156 |
| 158 BluetoothGattDescriptor* descriptor = GetDescriptorForUUID( | 157 std::vector<BluetoothGattDescriptor*> ccc_descriptor = GetDescriptorsByUUID( |
| 159 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()); | 158 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()); |
| 160 | 159 |
| 161 if (!descriptor) { | 160 if (ccc_descriptor.size() != 1u) { |
| 162 LOG(ERROR) | 161 LOG(ERROR) << "Found " << ccc_descriptor.size() |
| 163 << "Could not find client characteristic configuration descriptor"; | 162 << " client characteristic configuration descriptors."; |
| 164 base::MessageLoop::current()->PostTask( | 163 base::MessageLoop::current()->PostTask( |
| 165 FROM_HERE, | 164 FROM_HERE, |
| 166 base::Bind(error_callback, | 165 base::Bind(error_callback, |
| 167 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | 166 (ccc_descriptor.size() == 0) |
| 167 ? BluetoothGattService::GATT_ERROR_NOT_SUPPORTED |
| 168 : BluetoothGattService::GATT_ERROR_FAILED)); |
| 168 return; | 169 return; |
| 169 } | 170 } |
| 170 | 171 |
| 171 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( | 172 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( |
| 172 AttachCurrentThread(), j_characteristic_.obj(), true)) { | 173 AttachCurrentThread(), j_characteristic_.obj(), true)) { |
| 173 LOG(ERROR) << "Error enabling characteristic notification"; | 174 LOG(ERROR) << "Error enabling characteristic notification"; |
| 174 base::MessageLoop::current()->PostTask( | 175 base::MessageLoop::current()->PostTask( |
| 175 FROM_HERE, | 176 FROM_HERE, |
| 176 base::Bind(error_callback, | 177 base::Bind(error_callback, BluetoothGattService::GATT_ERROR_FAILED)); |
| 177 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | |
| 178 return; | 178 return; |
| 179 } | 179 } |
| 180 | 180 |
| 181 std::vector<uint8_t> value; | 181 std::vector<uint8_t> value; |
| 182 value.push_back(hasNotify ? 1 : 2); | 182 value.push_back(hasNotify ? 1 : 2); |
| 183 value.push_back(0); | 183 value.push_back(0); |
| 184 | 184 |
| 185 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); | 185 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); |
| 186 descriptor->WriteRemoteDescriptor( | 186 ccc_descriptor[0]->WriteRemoteDescriptor( |
| 187 value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: | 187 value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: |
| 188 OnStartNotifySessionSuccess, | 188 OnStartNotifySessionSuccess, |
| 189 base::Unretained(this)), | 189 base::Unretained(this)), |
| 190 base::Bind( | 190 base::Bind( |
| 191 &BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError, | 191 &BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError, |
| 192 base::Unretained(this))); | 192 base::Unretained(this))); |
| 193 } | 193 } |
| 194 | 194 |
| 195 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( | 195 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( |
| 196 const ValueCallback& callback, | 196 const ValueCallback& callback, |
| 197 const ErrorCallback& error_callback) { | 197 const ErrorCallback& error_callback) { |
| 198 if (read_pending_ || write_pending_) { | 198 if (read_pending_ || write_pending_) { |
| 199 base::MessageLoop::current()->PostTask( | 199 base::MessageLoop::current()->PostTask( |
| 200 FROM_HERE, base::Bind(error_callback, | 200 FROM_HERE, base::Bind(error_callback, |
| 201 BluetoothGattService::GATT_ERROR_IN_PROGRESS)); | 201 BluetoothGattService::GATT_ERROR_IN_PROGRESS)); |
| 202 return; | 202 return; |
| 203 } | 203 } |
| 204 | 204 |
| 205 if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic( | 205 if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic( |
| 206 AttachCurrentThread(), j_characteristic_.obj())) { | 206 AttachCurrentThread(), j_characteristic_.obj())) { |
| 207 base::MessageLoop::current()->PostTask( | 207 base::MessageLoop::current()->PostTask( |
| 208 FROM_HERE, | 208 FROM_HERE, |
| 209 base::Bind(error_callback, | 209 base::Bind(error_callback, BluetoothGattService::GATT_ERROR_FAILED)); |
| 210 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | |
| 211 return; | 210 return; |
| 212 } | 211 } |
| 213 | 212 |
| 214 read_pending_ = true; | 213 read_pending_ = true; |
| 215 read_callback_ = callback; | 214 read_callback_ = callback; |
| 216 read_error_callback_ = error_callback; | 215 read_error_callback_ = error_callback; |
| 217 } | 216 } |
| 218 | 217 |
| 219 void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic( | 218 void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic( |
| 220 const std::vector<uint8_t>& new_value, | 219 const std::vector<uint8_t>& new_value, |
| 221 const base::Closure& callback, | 220 const base::Closure& callback, |
| 222 const ErrorCallback& error_callback) { | 221 const ErrorCallback& error_callback) { |
| 223 if (read_pending_ || write_pending_) { | 222 if (read_pending_ || write_pending_) { |
| 224 base::MessageLoop::current()->PostTask( | 223 base::MessageLoop::current()->PostTask( |
| 225 FROM_HERE, base::Bind(error_callback, | 224 FROM_HERE, base::Bind(error_callback, |
| 226 BluetoothGattService::GATT_ERROR_IN_PROGRESS)); | 225 BluetoothGattService::GATT_ERROR_IN_PROGRESS)); |
| 227 return; | 226 return; |
| 228 } | 227 } |
| 229 | 228 |
| 230 JNIEnv* env = AttachCurrentThread(); | 229 JNIEnv* env = AttachCurrentThread(); |
| 231 if (!Java_ChromeBluetoothRemoteGattCharacteristic_writeRemoteCharacteristic( | 230 if (!Java_ChromeBluetoothRemoteGattCharacteristic_writeRemoteCharacteristic( |
| 232 env, j_characteristic_.obj(), | 231 env, j_characteristic_.obj(), |
| 233 base::android::ToJavaByteArray(env, new_value).obj())) { | 232 base::android::ToJavaByteArray(env, new_value).obj())) { |
| 234 base::MessageLoop::current()->PostTask( | 233 base::MessageLoop::current()->PostTask( |
| 235 FROM_HERE, | 234 FROM_HERE, |
| 236 base::Bind(error_callback, | 235 base::Bind(error_callback, BluetoothGattService::GATT_ERROR_FAILED)); |
| 237 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | |
| 238 return; | 236 return; |
| 239 } | 237 } |
| 240 | 238 |
| 241 write_pending_ = true; | 239 write_pending_ = true; |
| 242 write_callback_ = callback; | 240 write_callback_ = callback; |
| 243 write_error_callback_ = error_callback; | 241 write_error_callback_ = error_callback; |
| 244 } | 242 } |
| 245 | 243 |
| 246 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( | 244 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( |
| 247 JNIEnv* env, | 245 JNIEnv* env, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 343 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
| 346 const { | 344 const { |
| 347 if (!descriptors_.empty()) | 345 if (!descriptors_.empty()) |
| 348 return; | 346 return; |
| 349 | 347 |
| 350 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 348 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
| 351 AttachCurrentThread(), j_characteristic_.obj()); | 349 AttachCurrentThread(), j_characteristic_.obj()); |
| 352 } | 350 } |
| 353 | 351 |
| 354 } // namespace device | 352 } // namespace device |
| OLD | NEW |