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 
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_ANDROID_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_ANDROID_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "base/android/jni_android.h"
13 #include "base/android/scoped_java_ref.h"
14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/optional.h"
17 #include "device/bluetooth/bluetooth_adapter_android.h"
18 #include "device/bluetooth/bluetooth_device.h"
19 
20 namespace device {
21 
22 class BluetoothUUID;
23 
24 // BluetoothDeviceAndroid along with its owned Java class
25 // org.chromium.device.bluetooth.ChromeBluetoothDevice implement
26 // BluetoothDevice.
27 class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceAndroid final
28     : public BluetoothDevice {
29  public:
30   // Create a BluetoothDeviceAndroid instance and associated Java
31   // ChromeBluetoothDevice using the provided |java_bluetooth_device_wrapper|.
32   //
33   // The ChromeBluetoothDevice instance will hold a Java reference
34   // to |bluetooth_device_wrapper|.
35   static std::unique_ptr<BluetoothDeviceAndroid> Create(
36       BluetoothAdapterAndroid* adapter,
37       const base::android::JavaRef<jobject>&
38           bluetooth_device_wrapper);  // Java Type: bluetoothDeviceWrapper
39 
40   ~BluetoothDeviceAndroid() override;
41 
42   // Returns the associated ChromeBluetoothDevice Java object.
43   base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
44 
45   // Get owning BluetoothAdapter cast to BluetoothAdapterAndroid.
GetAndroidAdapter()46   BluetoothAdapterAndroid* GetAndroidAdapter() {
47     return static_cast<BluetoothAdapterAndroid*>(adapter_);
48   }
49 
50   // BluetoothDevice:
51   uint32_t GetBluetoothClass() const override;
52   std::string GetAddress() const override;
53   AddressType GetAddressType() const override;
54   VendorIDSource GetVendorIDSource() const override;
55   uint16_t GetVendorID() const override;
56   uint16_t GetProductID() const override;
57   uint16_t GetDeviceID() const override;
58   uint16_t GetAppearance() const override;
59   base::Optional<std::string> GetName() const override;
60   bool IsPaired() const override;
61   bool IsConnected() const override;
62   bool IsGattConnected() const override;
63   bool IsConnectable() const override;
64   bool IsConnecting() const override;
65   bool ExpectingPinCode() const override;
66   bool ExpectingPasskey() const override;
67   bool ExpectingConfirmation() const override;
68   void GetConnectionInfo(ConnectionInfoCallback callback) override;
69   void SetConnectionLatency(ConnectionLatency connection_latency,
70                             base::OnceClosure callback,
71                             ErrorCallback error_callback) override;
72   void Connect(device::BluetoothDevice::PairingDelegate* pairing_delegate,
73                base::OnceClosure callback,
74                ConnectErrorCallback error_callback) override;
75   void SetPinCode(const std::string& pincode) override;
76   void SetPasskey(uint32_t passkey) override;
77   void ConfirmPairing() override;
78   void RejectPairing() override;
79   void CancelPairing() override;
80   void Disconnect(base::OnceClosure callback,
81                   ErrorCallback error_callback) override;
82   void Forget(base::OnceClosure callback,
83               ErrorCallback error_callback) override;
84   void ConnectToService(const device::BluetoothUUID& uuid,
85                         ConnectToServiceCallback callback,
86                         ConnectToServiceErrorCallback error_callback) override;
87   void ConnectToServiceInsecurely(
88       const device::BluetoothUUID& uuid,
89       ConnectToServiceCallback callback,
90       ConnectToServiceErrorCallback error_callback) override;
91 
92   // Callback indicating when GATT client has connected/disconnected.
93   // See android.bluetooth.BluetoothGattCallback.onConnectionStateChange.
94   void OnConnectionStateChange(
95       JNIEnv* env,
96       const base::android::JavaParamRef<jobject>& jcaller,
97       int32_t status,
98       bool connected);
99 
100   // Callback indicating when all services of the device have been
101   // discovered.
102   void OnGattServicesDiscovered(
103       JNIEnv* env,
104       const base::android::JavaParamRef<jobject>& jcaller);
105 
106   // Creates Bluetooth GATT service objects and adds them to
107   // BluetoothDevice::gatt_services_ if they are not already there.
108   void CreateGattRemoteService(
109       JNIEnv* env,
110       const base::android::JavaParamRef<jobject>& caller,
111       const base::android::JavaParamRef<jstring>& instance_id,
112       const base::android::JavaParamRef<jobject>&
113           bluetooth_gatt_service_wrapper);  // BluetoothGattServiceWrapper
114 
115  protected:
116   BluetoothDeviceAndroid(BluetoothAdapterAndroid* adapter);
117 
118   // BluetoothDevice:
119   void CreateGattConnectionImpl(
120       base::Optional<device::BluetoothUUID> service_uuid) override;
121   void DisconnectGatt() override;
122 
123   // Java object org.chromium.device.bluetooth.ChromeBluetoothDevice.
124   base::android::ScopedJavaGlobalRef<jobject> j_device_;
125 
126   bool gatt_connected_ = false;
127 
128   DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceAndroid);
129 };
130 
131 }  // namespace device
132 
133 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_ANDROID_H_
134