bluetooth_remote_gatt_service_android.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "device/bluetooth/bluetooth_remote_gatt_service_android.h"
  5. #include <memory>
  6. #include "base/android/jni_android.h"
  7. #include "base/android/jni_string.h"
  8. #include "base/containers/contains.h"
  9. #include "device/bluetooth/bluetooth_adapter_android.h"
  10. #include "device/bluetooth/bluetooth_device_android.h"
  11. #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h"
  12. #include "device/bluetooth/jni_headers/ChromeBluetoothRemoteGattService_jni.h"
  13. using base::android::AttachCurrentThread;
  14. using base::android::JavaParamRef;
  15. using base::android::JavaRef;
  16. namespace device {
  17. // static
  18. std::unique_ptr<BluetoothRemoteGattServiceAndroid>
  19. BluetoothRemoteGattServiceAndroid::Create(
  20. BluetoothAdapterAndroid* adapter,
  21. BluetoothDeviceAndroid* device,
  22. const JavaRef<jobject>&
  23. bluetooth_gatt_service_wrapper, // BluetoothGattServiceWrapper
  24. const std::string& instance_id,
  25. const JavaRef<jobject>& chrome_bluetooth_device) { // ChromeBluetoothDevice
  26. std::unique_ptr<BluetoothRemoteGattServiceAndroid> service(
  27. new BluetoothRemoteGattServiceAndroid(adapter, device, instance_id));
  28. JNIEnv* env = AttachCurrentThread();
  29. service->j_service_.Reset(Java_ChromeBluetoothRemoteGattService_create(
  30. env, reinterpret_cast<intptr_t>(service.get()),
  31. bluetooth_gatt_service_wrapper,
  32. base::android::ConvertUTF8ToJavaString(env, instance_id),
  33. chrome_bluetooth_device));
  34. return service;
  35. }
  36. BluetoothRemoteGattServiceAndroid::~BluetoothRemoteGattServiceAndroid() {
  37. Java_ChromeBluetoothRemoteGattService_onBluetoothRemoteGattServiceAndroidDestruction(
  38. AttachCurrentThread(), j_service_);
  39. }
  40. base::android::ScopedJavaLocalRef<jobject>
  41. BluetoothRemoteGattServiceAndroid::GetJavaObject() {
  42. return base::android::ScopedJavaLocalRef<jobject>(j_service_);
  43. }
  44. // static
  45. BluetoothGattService::GattErrorCode
  46. BluetoothRemoteGattServiceAndroid::GetGattErrorCode(int bluetooth_gatt_code) {
  47. DCHECK(bluetooth_gatt_code != 0) << "Only errors valid. 0 == GATT_SUCCESS.";
  48. // TODO(scheib) Create new BluetoothGattService::GattErrorCode enums for
  49. // android values not yet represented. http://crbug.com/548498
  50. switch (bluetooth_gatt_code) { // android.bluetooth.BluetoothGatt values:
  51. case 0x00000101: // GATT_FAILURE
  52. return GATT_ERROR_FAILED;
  53. case 0x0000000d: // GATT_INVALID_ATTRIBUTE_LENGTH
  54. return GATT_ERROR_INVALID_LENGTH;
  55. case 0x00000002: // GATT_READ_NOT_PERMITTED
  56. return GATT_ERROR_NOT_PERMITTED;
  57. case 0x00000006: // GATT_REQUEST_NOT_SUPPORTED
  58. return GATT_ERROR_NOT_SUPPORTED;
  59. case 0x00000003: // GATT_WRITE_NOT_PERMITTED
  60. return GATT_ERROR_NOT_PERMITTED;
  61. default:
  62. DVLOG(1) << "Unhandled status: " << bluetooth_gatt_code;
  63. return BluetoothGattService::GATT_ERROR_UNKNOWN;
  64. }
  65. }
  66. // static
  67. int BluetoothRemoteGattServiceAndroid::GetAndroidErrorCode(
  68. BluetoothGattService::GattErrorCode error_code) {
  69. // TODO(scheib) Create new BluetoothGattService::GattErrorCode enums for
  70. // android values not yet represented. http://crbug.com/548498
  71. switch (error_code) { // Return values from android.bluetooth.BluetoothGatt:
  72. case GATT_ERROR_UNKNOWN:
  73. return 0x00000101; // GATT_FAILURE. No good match.
  74. case GATT_ERROR_FAILED:
  75. return 0x00000101; // GATT_FAILURE
  76. case GATT_ERROR_IN_PROGRESS:
  77. return 0x00000101; // GATT_FAILURE. No good match.
  78. case GATT_ERROR_INVALID_LENGTH:
  79. return 0x0000000d; // GATT_INVALID_ATTRIBUTE_LENGTH
  80. case GATT_ERROR_NOT_PERMITTED:
  81. // Can't distinguish between:
  82. // 0x00000002: // GATT_READ_NOT_PERMITTED
  83. // 0x00000003: // GATT_WRITE_NOT_PERMITTED
  84. return 0x00000101; // GATT_FAILURE. No good match.
  85. case GATT_ERROR_NOT_AUTHORIZED:
  86. return 0x00000101; // GATT_FAILURE. No good match.
  87. case GATT_ERROR_NOT_PAIRED:
  88. return 0x00000101; // GATT_FAILURE. No good match.
  89. case GATT_ERROR_NOT_SUPPORTED:
  90. return 0x00000006; // GATT_REQUEST_NOT_SUPPORTED
  91. }
  92. DVLOG(1) << "Unhandled error_code: " << error_code;
  93. return 0x00000101; // GATT_FAILURE. No good match.
  94. }
  95. std::string BluetoothRemoteGattServiceAndroid::GetIdentifier() const {
  96. return instance_id_;
  97. }
  98. device::BluetoothUUID BluetoothRemoteGattServiceAndroid::GetUUID() const {
  99. return device::BluetoothUUID(
  100. ConvertJavaStringToUTF8(Java_ChromeBluetoothRemoteGattService_getUUID(
  101. AttachCurrentThread(), j_service_)));
  102. }
  103. bool BluetoothRemoteGattServiceAndroid::IsPrimary() const {
  104. NOTIMPLEMENTED();
  105. return true;
  106. }
  107. device::BluetoothDevice* BluetoothRemoteGattServiceAndroid::GetDevice() const {
  108. return device_;
  109. }
  110. std::vector<device::BluetoothRemoteGattCharacteristic*>
  111. BluetoothRemoteGattServiceAndroid::GetCharacteristics() const {
  112. EnsureCharacteristicsCreated();
  113. return BluetoothRemoteGattService::GetCharacteristics();
  114. }
  115. std::vector<device::BluetoothRemoteGattService*>
  116. BluetoothRemoteGattServiceAndroid::GetIncludedServices() const {
  117. NOTIMPLEMENTED();
  118. return std::vector<device::BluetoothRemoteGattService*>();
  119. }
  120. device::BluetoothRemoteGattCharacteristic*
  121. BluetoothRemoteGattServiceAndroid::GetCharacteristic(
  122. const std::string& identifier) const {
  123. EnsureCharacteristicsCreated();
  124. return BluetoothRemoteGattService::GetCharacteristic(identifier);
  125. }
  126. std::vector<BluetoothRemoteGattCharacteristic*>
  127. BluetoothRemoteGattServiceAndroid::GetCharacteristicsByUUID(
  128. const BluetoothUUID& characteristic_uuid) const {
  129. EnsureCharacteristicsCreated();
  130. return BluetoothRemoteGattService::GetCharacteristicsByUUID(
  131. characteristic_uuid);
  132. }
  133. bool BluetoothRemoteGattServiceAndroid::IsDiscoveryComplete() const {
  134. // Not used on Android, because Android sends an event when service discovery
  135. // is complete for the entire device.
  136. NOTIMPLEMENTED();
  137. return true;
  138. }
  139. void BluetoothRemoteGattServiceAndroid::SetDiscoveryComplete(bool complete) {
  140. // Not used on Android, because Android sends an event when service discovery
  141. // is complete for the entire device.
  142. NOTIMPLEMENTED();
  143. }
  144. void BluetoothRemoteGattServiceAndroid::CreateGattRemoteCharacteristic(
  145. JNIEnv* env,
  146. const JavaParamRef<jobject>& caller,
  147. const JavaParamRef<jstring>& instance_id,
  148. const JavaParamRef<jobject>& /* BluetoothGattCharacteristicWrapper */
  149. bluetooth_gatt_characteristic_wrapper,
  150. const JavaParamRef<jobject>& /* ChromeBluetoothDevice */
  151. chrome_bluetooth_device) {
  152. std::string instance_id_string =
  153. base::android::ConvertJavaStringToUTF8(env, instance_id);
  154. DCHECK(!base::Contains(characteristics_, instance_id_string));
  155. AddCharacteristic(BluetoothRemoteGattCharacteristicAndroid::Create(
  156. adapter_, this, instance_id_string, bluetooth_gatt_characteristic_wrapper,
  157. chrome_bluetooth_device));
  158. }
  159. BluetoothRemoteGattServiceAndroid::BluetoothRemoteGattServiceAndroid(
  160. BluetoothAdapterAndroid* adapter,
  161. BluetoothDeviceAndroid* device,
  162. const std::string& instance_id)
  163. : adapter_(adapter), device_(device), instance_id_(instance_id) {}
  164. void BluetoothRemoteGattServiceAndroid::EnsureCharacteristicsCreated() const {
  165. if (!characteristics_.empty())
  166. return;
  167. // Java call
  168. Java_ChromeBluetoothRemoteGattService_createCharacteristics(
  169. AttachCurrentThread(), j_service_);
  170. }
  171. } // namespace device