bluetooth_device_android.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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
  3. // found in the LICENSE file.
  4. #include "device/bluetooth/bluetooth_device_android.h"
  5. #include "base/android/jni_android.h"
  6. #include "base/android/jni_string.h"
  7. #include "base/containers/contains.h"
  8. #include "device/bluetooth/bluetooth_adapter_android.h"
  9. #include "device/bluetooth/bluetooth_remote_gatt_service_android.h"
  10. #include "device/bluetooth/jni_headers/ChromeBluetoothDevice_jni.h"
  11. using base::android::AttachCurrentThread;
  12. using base::android::JavaParamRef;
  13. using base::android::JavaRef;
  14. namespace device {
  15. std::unique_ptr<BluetoothDeviceAndroid> BluetoothDeviceAndroid::Create(
  16. BluetoothAdapterAndroid* adapter,
  17. const JavaRef<jobject>&
  18. bluetooth_device_wrapper) { // Java Type: bluetoothDeviceWrapper
  19. std::unique_ptr<BluetoothDeviceAndroid> device(
  20. new BluetoothDeviceAndroid(adapter));
  21. device->j_device_.Reset(Java_ChromeBluetoothDevice_create(
  22. AttachCurrentThread(), reinterpret_cast<intptr_t>(device.get()),
  23. bluetooth_device_wrapper));
  24. return device;
  25. }
  26. BluetoothDeviceAndroid::~BluetoothDeviceAndroid() {
  27. Java_ChromeBluetoothDevice_onBluetoothDeviceAndroidDestruction(
  28. AttachCurrentThread(), j_device_);
  29. }
  30. base::android::ScopedJavaLocalRef<jobject>
  31. BluetoothDeviceAndroid::GetJavaObject() {
  32. return base::android::ScopedJavaLocalRef<jobject>(j_device_);
  33. }
  34. uint32_t BluetoothDeviceAndroid::GetBluetoothClass() const {
  35. return Java_ChromeBluetoothDevice_getBluetoothClass(AttachCurrentThread(),
  36. j_device_);
  37. }
  38. std::string BluetoothDeviceAndroid::GetAddress() const {
  39. return ConvertJavaStringToUTF8(
  40. Java_ChromeBluetoothDevice_getAddress(AttachCurrentThread(), j_device_));
  41. }
  42. BluetoothDevice::AddressType BluetoothDeviceAndroid::GetAddressType() const {
  43. NOTIMPLEMENTED();
  44. return ADDR_TYPE_UNKNOWN;
  45. }
  46. BluetoothDevice::VendorIDSource BluetoothDeviceAndroid::GetVendorIDSource()
  47. const {
  48. // Android API does not provide Vendor ID.
  49. return VENDOR_ID_UNKNOWN;
  50. }
  51. uint16_t BluetoothDeviceAndroid::GetVendorID() const {
  52. // Android API does not provide Vendor ID.
  53. return 0;
  54. }
  55. uint16_t BluetoothDeviceAndroid::GetProductID() const {
  56. // Android API does not provide Product ID.
  57. return 0;
  58. }
  59. uint16_t BluetoothDeviceAndroid::GetDeviceID() const {
  60. // Android API does not provide Device ID.
  61. return 0;
  62. }
  63. uint16_t BluetoothDeviceAndroid::GetAppearance() const {
  64. // TODO(crbug.com/588083): Implementing GetAppearance()
  65. // on mac, win, and android platforms for chrome
  66. NOTIMPLEMENTED();
  67. return 0;
  68. }
  69. absl::optional<std::string> BluetoothDeviceAndroid::GetName() const {
  70. auto name =
  71. Java_ChromeBluetoothDevice_getName(AttachCurrentThread(), j_device_);
  72. if (name.is_null())
  73. return absl::nullopt;
  74. return ConvertJavaStringToUTF8(name);
  75. }
  76. bool BluetoothDeviceAndroid::IsPaired() const {
  77. return Java_ChromeBluetoothDevice_isPaired(AttachCurrentThread(), j_device_);
  78. }
  79. bool BluetoothDeviceAndroid::IsConnected() const {
  80. return IsGattConnected();
  81. }
  82. bool BluetoothDeviceAndroid::IsGattConnected() const {
  83. return gatt_connected_;
  84. }
  85. bool BluetoothDeviceAndroid::IsConnectable() const {
  86. NOTIMPLEMENTED();
  87. return false;
  88. }
  89. bool BluetoothDeviceAndroid::IsConnecting() const {
  90. NOTIMPLEMENTED();
  91. return false;
  92. }
  93. bool BluetoothDeviceAndroid::ExpectingPinCode() const {
  94. NOTIMPLEMENTED();
  95. return false;
  96. }
  97. bool BluetoothDeviceAndroid::ExpectingPasskey() const {
  98. NOTIMPLEMENTED();
  99. return false;
  100. }
  101. bool BluetoothDeviceAndroid::ExpectingConfirmation() const {
  102. NOTIMPLEMENTED();
  103. return false;
  104. }
  105. void BluetoothDeviceAndroid::GetConnectionInfo(
  106. ConnectionInfoCallback callback) {
  107. NOTIMPLEMENTED();
  108. std::move(callback).Run(ConnectionInfo());
  109. }
  110. void BluetoothDeviceAndroid::SetConnectionLatency(
  111. ConnectionLatency connection_latency,
  112. base::OnceClosure callback,
  113. ErrorCallback error_callback) {
  114. NOTIMPLEMENTED();
  115. }
  116. void BluetoothDeviceAndroid::Connect(PairingDelegate* pairing_delegate,
  117. ConnectCallback callback) {
  118. NOTIMPLEMENTED();
  119. }
  120. void BluetoothDeviceAndroid::SetPinCode(const std::string& pincode) {
  121. NOTIMPLEMENTED();
  122. }
  123. void BluetoothDeviceAndroid::SetPasskey(uint32_t passkey) {
  124. NOTIMPLEMENTED();
  125. }
  126. void BluetoothDeviceAndroid::ConfirmPairing() {
  127. NOTIMPLEMENTED();
  128. }
  129. void BluetoothDeviceAndroid::RejectPairing() {
  130. NOTIMPLEMENTED();
  131. }
  132. void BluetoothDeviceAndroid::CancelPairing() {
  133. NOTIMPLEMENTED();
  134. }
  135. void BluetoothDeviceAndroid::Disconnect(base::OnceClosure callback,
  136. ErrorCallback error_callback) {
  137. // TODO(scheib): Also update unit tests for BluetoothGattConnection.
  138. NOTIMPLEMENTED();
  139. }
  140. void BluetoothDeviceAndroid::Forget(base::OnceClosure callback,
  141. ErrorCallback error_callback) {
  142. NOTIMPLEMENTED();
  143. }
  144. void BluetoothDeviceAndroid::ConnectToService(
  145. const BluetoothUUID& uuid,
  146. ConnectToServiceCallback callback,
  147. ConnectToServiceErrorCallback error_callback) {
  148. NOTIMPLEMENTED();
  149. }
  150. void BluetoothDeviceAndroid::ConnectToServiceInsecurely(
  151. const BluetoothUUID& uuid,
  152. ConnectToServiceCallback callback,
  153. ConnectToServiceErrorCallback error_callback) {
  154. NOTIMPLEMENTED();
  155. }
  156. void BluetoothDeviceAndroid::OnConnectionStateChange(
  157. JNIEnv* env,
  158. const JavaParamRef<jobject>& jcaller,
  159. int32_t status,
  160. bool connected) {
  161. gatt_connected_ = connected;
  162. if (gatt_connected_) {
  163. DidConnectGatt(/*error_code=*/absl::nullopt);
  164. } else if (!create_gatt_connection_callbacks_.empty()) {
  165. // We assume that if there are any pending connection callbacks there
  166. // was a failed connection attempt.
  167. // TODO(ortuno): Return an error code based on |status|
  168. // http://crbug.com/578191
  169. DidConnectGatt(ERROR_FAILED);
  170. } else {
  171. // Otherwise an existing connection was terminated.
  172. gatt_services_.clear();
  173. device_uuids_.ClearServiceUUIDs();
  174. SetGattServicesDiscoveryComplete(false);
  175. DidDisconnectGatt();
  176. }
  177. }
  178. void BluetoothDeviceAndroid::OnGattServicesDiscovered(
  179. JNIEnv* env,
  180. const JavaParamRef<jobject>& jcaller) {
  181. device_uuids_.ReplaceServiceUUIDs(gatt_services_);
  182. SetGattServicesDiscoveryComplete(true);
  183. adapter_->NotifyGattServicesDiscovered(this);
  184. adapter_->NotifyDeviceChanged(this);
  185. }
  186. void BluetoothDeviceAndroid::CreateGattRemoteService(
  187. JNIEnv* env,
  188. const JavaParamRef<jobject>& caller,
  189. const JavaParamRef<jstring>& instance_id,
  190. const JavaParamRef<jobject>&
  191. bluetooth_gatt_service_wrapper) { // BluetoothGattServiceWrapper
  192. std::string instance_id_string =
  193. base::android::ConvertJavaStringToUTF8(env, instance_id);
  194. if (base::Contains(gatt_services_, instance_id_string))
  195. return;
  196. std::unique_ptr<BluetoothRemoteGattServiceAndroid> service =
  197. BluetoothRemoteGattServiceAndroid::Create(GetAndroidAdapter(), this,
  198. bluetooth_gatt_service_wrapper,
  199. instance_id_string, j_device_);
  200. BluetoothRemoteGattServiceAndroid* service_ptr = service.get();
  201. gatt_services_[instance_id_string] = std::move(service);
  202. adapter_->NotifyGattServiceAdded(service_ptr);
  203. }
  204. BluetoothDeviceAndroid::BluetoothDeviceAndroid(BluetoothAdapterAndroid* adapter)
  205. : BluetoothDevice(adapter) {}
  206. void BluetoothDeviceAndroid::CreateGattConnectionImpl(
  207. absl::optional<device::BluetoothUUID> service_uuid) {
  208. Java_ChromeBluetoothDevice_createGattConnectionImpl(AttachCurrentThread(),
  209. j_device_);
  210. }
  211. void BluetoothDeviceAndroid::DisconnectGatt() {
  212. Java_ChromeBluetoothDevice_disconnectGatt(AttachCurrentThread(), j_device_);
  213. }
  214. } // namespace device