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