bluetooth_adapter_android.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_ANDROID_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_ANDROID_H_
  6. #include <memory>
  7. #include "base/android/jni_android.h"
  8. #include "base/android/scoped_java_ref.h"
  9. #include "base/gtest_prod_util.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "device/bluetooth/bluetooth_adapter.h"
  12. using base::android::ScopedJavaLocalRef;
  13. namespace device {
  14. // BluetoothAdapterAndroid, along with the Java class
  15. // org.chromium.device.bluetooth.BluetoothAdapter, implement BluetoothAdapter.
  16. //
  17. // The GATT Profile over Low Energy is supported, but not Classic Bluetooth at
  18. // this time. LE GATT support has been initially built out to support Web
  19. // Bluetooth, which does not need other Bluetooth features. There is no
  20. // technical reason they can not be supported should a need arrise.
  21. //
  22. // BluetoothAdapterAndroid is reference counted, and owns the lifetime of the
  23. // Java class BluetoothAdapter via j_adapter_. The adapter also owns a tree of
  24. // additional C++ objects (Devices, Services, Characteristics, Descriptors),
  25. // with each C++ object owning its associated Java class.
  26. class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterAndroid final
  27. : public BluetoothAdapter {
  28. public:
  29. // Create a BluetoothAdapterAndroid instance.
  30. //
  31. // |java_bluetooth_adapter_wrapper| is optional. If it is NULL the adapter
  32. // will return false for |IsPresent()| and not be functional.
  33. //
  34. // The BluetoothAdapterAndroid instance will indirectly hold a Java reference
  35. // to |bluetooth_adapter_wrapper|.
  36. static scoped_refptr<BluetoothAdapterAndroid> Create(
  37. const base::android::JavaRef<jobject>&
  38. bluetooth_adapter_wrapper); // Java Type: bluetoothAdapterWrapper
  39. BluetoothAdapterAndroid(const BluetoothAdapterAndroid&) = delete;
  40. BluetoothAdapterAndroid& operator=(const BluetoothAdapterAndroid&) = delete;
  41. // BluetoothAdapter:
  42. void Initialize(base::OnceClosure callback) override;
  43. std::string GetAddress() const override;
  44. std::string GetName() const override;
  45. void SetName(const std::string& name,
  46. base::OnceClosure callback,
  47. ErrorCallback error_callback) override;
  48. bool IsInitialized() const override;
  49. bool IsPresent() const override;
  50. bool IsPowered() const override;
  51. bool IsDiscoverable() const override;
  52. void SetDiscoverable(bool discoverable,
  53. base::OnceClosure callback,
  54. ErrorCallback error_callback) override;
  55. bool IsDiscovering() const override;
  56. UUIDList GetUUIDs() const override;
  57. void CreateRfcommService(const BluetoothUUID& uuid,
  58. const ServiceOptions& options,
  59. CreateServiceCallback callback,
  60. CreateServiceErrorCallback error_callback) override;
  61. void CreateL2capService(const BluetoothUUID& uuid,
  62. const ServiceOptions& options,
  63. CreateServiceCallback callback,
  64. CreateServiceErrorCallback error_callback) override;
  65. void RegisterAdvertisement(
  66. std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data,
  67. CreateAdvertisementCallback callback,
  68. AdvertisementErrorCallback error_callback) override;
  69. BluetoothLocalGattService* GetGattService(
  70. const std::string& identifier) const override;
  71. // Called when adapter state changes.
  72. void OnAdapterStateChanged(JNIEnv* env,
  73. const base::android::JavaParamRef<jobject>& caller,
  74. const bool powered);
  75. // Handles a scan error event by invalidating all discovery sessions.
  76. void OnScanFailed(JNIEnv* env,
  77. const base::android::JavaParamRef<jobject>& caller);
  78. // Creates or updates device with advertised UUID information when a device is
  79. // discovered during a scan.
  80. void CreateOrUpdateDeviceOnScan(
  81. JNIEnv* env,
  82. const base::android::JavaParamRef<jobject>& caller,
  83. const base::android::JavaParamRef<jstring>& address,
  84. const base::android::JavaParamRef<jobject>&
  85. bluetooth_device_wrapper, // Java Type: bluetoothDeviceWrapper
  86. const base::android::JavaParamRef<jstring>& local_name,
  87. int32_t rssi,
  88. const base::android::JavaParamRef<jobjectArray>&
  89. advertised_uuids, // Java Type: String[]
  90. int32_t tx_power,
  91. const base::android::JavaParamRef<jobjectArray>&
  92. service_data_keys, // Java Type: String[]
  93. const base::android::JavaParamRef<jobjectArray>&
  94. service_data_values, // Java Type: byte[]
  95. const base::android::JavaParamRef<jintArray>&
  96. manufacturer_data_keys, // Java Type: int[]
  97. const base::android::JavaParamRef<jobjectArray>&
  98. manufacturer_data_values, // Java Type: byte[]
  99. int32_t advertisement_flags);
  100. protected:
  101. BluetoothAdapterAndroid();
  102. ~BluetoothAdapterAndroid() override;
  103. // BluetoothAdapter:
  104. base::WeakPtr<BluetoothAdapter> GetWeakPtr() override;
  105. bool SetPoweredImpl(bool powered) override;
  106. void StartScanWithFilter(
  107. std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
  108. DiscoverySessionResultCallback callback) override;
  109. void UpdateFilter(std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
  110. DiscoverySessionResultCallback callback) override;
  111. void StopScan(DiscoverySessionResultCallback callback) override;
  112. void RemovePairingDelegateInternal(
  113. BluetoothDevice::PairingDelegate* pairing_delegate) override;
  114. void PurgeTimedOutDevices();
  115. // Utility function used to create a Java object that represents the filter.
  116. base::android::ScopedJavaLocalRef<jobject> CreateAndroidFilter(
  117. const BluetoothDiscoveryFilter* discovery_filter);
  118. // Java object org.chromium.device.bluetooth.ChromeBluetoothAdapter.
  119. base::android::ScopedJavaGlobalRef<jobject> j_adapter_;
  120. private:
  121. FRIEND_TEST_ALL_PREFIXES(BluetoothAdapterAndroidTest, ScanFilterTest);
  122. // Note: This should remain the last member so it'll be destroyed and
  123. // invalidate its weak pointers before any other members are destroyed.
  124. base::WeakPtrFactory<BluetoothAdapterAndroid> weak_ptr_factory_{this};
  125. };
  126. } // namespace device
  127. #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_ANDROID_H_