bluetooth_remote_gatt_service_android.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_REMOTE_GATT_SERVICE_ANDROID_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_ANDROID_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/android/jni_android.h"
  11. #include "base/android/scoped_java_ref.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "device/bluetooth/bluetooth_remote_gatt_service.h"
  14. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  15. namespace device {
  16. class BluetoothAdapterAndroid;
  17. class BluetoothDeviceAndroid;
  18. // BluetoothRemoteGattServiceAndroid along with its owned Java class
  19. // org.chromium.device.bluetooth.ChromeBluetoothRemoteGattService implement
  20. // BluetoothRemoteGattService.
  21. class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattServiceAndroid
  22. : public device::BluetoothRemoteGattService {
  23. public:
  24. // Create a BluetoothRemoteGattServiceAndroid instance and associated Java
  25. // ChromeBluetoothRemoteGattService using the provided
  26. // |bluetooth_gatt_service_wrapper|.
  27. //
  28. // The ChromeBluetoothRemoteGattService instance will hold a Java reference
  29. // to |bluetooth_gatt_service_wrapper|.
  30. static std::unique_ptr<BluetoothRemoteGattServiceAndroid> Create(
  31. BluetoothAdapterAndroid* adapter,
  32. BluetoothDeviceAndroid* device,
  33. const base::android::JavaRef<jobject>&
  34. bluetooth_gatt_service_wrapper, // BluetoothGattServiceWrapper
  35. const std::string& instance_id,
  36. const base::android::JavaRef<jobject>&
  37. chrome_bluetooth_device); // ChromeBluetoothDevice
  38. BluetoothRemoteGattServiceAndroid(const BluetoothRemoteGattServiceAndroid&) =
  39. delete;
  40. BluetoothRemoteGattServiceAndroid& operator=(
  41. const BluetoothRemoteGattServiceAndroid&) = delete;
  42. ~BluetoothRemoteGattServiceAndroid() override;
  43. // Returns the associated ChromeBluetoothRemoteGattService Java object.
  44. base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
  45. // Returns a BluetoothGattService::GattErrorCode from a given
  46. // android.bluetooth.BluetoothGatt error code.
  47. // |bluetooth_gatt_code| must not be 0 == GATT_SUCCESS.
  48. static BluetoothGattService::GattErrorCode GetGattErrorCode(
  49. int bluetooth_gatt_code);
  50. // Returns an android.bluetooth.BluetoothGatt error code for a given
  51. // BluetoothGattService::GattErrorCode value.
  52. static int GetAndroidErrorCode(BluetoothGattService::GattErrorCode);
  53. // device::BluetoothRemoteGattService overrides.
  54. std::string GetIdentifier() const override;
  55. device::BluetoothUUID GetUUID() const override;
  56. bool IsPrimary() const override;
  57. device::BluetoothDevice* GetDevice() const override;
  58. std::vector<device::BluetoothRemoteGattCharacteristic*> GetCharacteristics()
  59. const override;
  60. std::vector<device::BluetoothRemoteGattService*> GetIncludedServices()
  61. const override;
  62. device::BluetoothRemoteGattCharacteristic* GetCharacteristic(
  63. const std::string& identifier) const override;
  64. std::vector<BluetoothRemoteGattCharacteristic*> GetCharacteristicsByUUID(
  65. const BluetoothUUID& characteristic_uuid) const override;
  66. bool IsDiscoveryComplete() const override;
  67. void SetDiscoveryComplete(bool complete) override;
  68. // Creates a Bluetooth GATT characteristic object and adds it to
  69. // |characteristics_|, DCHECKing that it has not already been created.
  70. void CreateGattRemoteCharacteristic(
  71. JNIEnv* env,
  72. const base::android::JavaParamRef<jobject>& caller,
  73. const base::android::JavaParamRef<jstring>& instance_id,
  74. const base::android::JavaParamRef<
  75. jobject>& /* BluetoothGattCharacteristicWrapper */
  76. bluetooth_gatt_characteristic_wrapper,
  77. const base::android::JavaParamRef<
  78. jobject>& /* ChromeBluetoothDevice */ chrome_bluetooth_device);
  79. private:
  80. BluetoothRemoteGattServiceAndroid(BluetoothAdapterAndroid* adapter,
  81. BluetoothDeviceAndroid* device,
  82. const std::string& instance_id);
  83. // Populates |characteristics_| from Java objects if necessary.
  84. void EnsureCharacteristicsCreated() const;
  85. // Java object org.chromium.device.bluetooth.ChromeBluetoothRemoteGattService.
  86. base::android::ScopedJavaGlobalRef<jobject> j_service_;
  87. // The adapter associated with this service. It's ok to store a raw pointer
  88. // here since |adapter_| indirectly owns this instance.
  89. raw_ptr<BluetoothAdapterAndroid> adapter_;
  90. // The device this GATT service belongs to. It's ok to store a raw pointer
  91. // here since |device_| owns this instance.
  92. raw_ptr<BluetoothDeviceAndroid> device_;
  93. // Adapter unique instance ID.
  94. std::string instance_id_;
  95. };
  96. } // namespace device
  97. #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_ANDROID_H_