blob: 96e6d2805f58759b3b492d19a07de3c3abfc311a [file] [log] [blame]
scheib59e28a22015-07-14 00:38:321// 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
avi176e2692015-12-22 19:26:528#include <stdint.h>
9
thakis08c9d69b02016-04-11 04:37:2510#include <memory>
11
scheib59e28a22015-07-14 00:38:3212#include "base/android/jni_android.h"
torne772283d2016-09-08 14:16:2813#include "base/android/scoped_java_ref.h"
avi176e2692015-12-22 19:26:5214#include "base/macros.h"
scheib59e28a22015-07-14 00:38:3215#include "base/memory/weak_ptr.h"
scheibc3ecc9f2015-10-10 15:49:1616#include "device/bluetooth/bluetooth_adapter_android.h"
scheib59e28a22015-07-14 00:38:3217#include "device/bluetooth/bluetooth_device.h"
18
19namespace device {
20
scheib41bcc682015-10-23 03:23:2421// BluetoothDeviceAndroid along with its owned Java class
scheibeea0c48e2015-09-16 22:09:5422// org.chromium.device.bluetooth.ChromeBluetoothDevice implement
23// BluetoothDevice.
scheib59e28a22015-07-14 00:38:3224class 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|.
juncai131f2d52017-03-22 01:58:4632 static std::unique_ptr<BluetoothDeviceAndroid> Create(
scheibeea0c48e2015-09-16 22:09:5433 BluetoothAdapterAndroid* adapter,
torne772283d2016-09-08 14:16:2834 const base::android::JavaRef<jobject>&
35 bluetooth_device_wrapper); // Java Type: bluetoothDeviceWrapper
scheib59e28a22015-07-14 00:38:3236
37 ~BluetoothDeviceAndroid() override;
38
scheibaf7ea912015-09-23 20:27:5339 // Returns the associated ChromeBluetoothDevice Java object.
40 base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
41
scheibc3ecc9f2015-10-10 15:49:1642 // Get owning BluetoothAdapter cast to BluetoothAdapterAndroid.
gogerald43eba582016-02-10 22:25:5643 BluetoothAdapterAndroid* GetAndroidAdapter() {
scheibc3ecc9f2015-10-10 15:49:1644 return static_cast<BluetoothAdapterAndroid*>(adapter_);
45 }
46
scheib59e28a22015-07-14 00:38:3247 // BluetoothDevice:
avi176e2692015-12-22 19:26:5248 uint32_t GetBluetoothClass() const override;
scheib59e28a22015-07-14 00:38:3249 std::string GetAddress() const override;
50 VendorIDSource GetVendorIDSource() const override;
avi176e2692015-12-22 19:26:5251 uint16_t GetVendorID() const override;
52 uint16_t GetProductID() const override;
53 uint16_t GetDeviceID() const override;
josephsih098a1cd82016-02-24 10:17:2354 uint16_t GetAppearance() const override;
scheibdd9ce7002016-07-21 07:06:0655 base::Optional<std::string> GetName() const override;
scheib59e28a22015-07-14 00:38:3256 bool IsPaired() const override;
57 bool IsConnected() const override;
scheib95560872015-09-21 05:09:3358 bool IsGattConnected() const override;
scheib59e28a22015-07-14 00:38:3259 bool IsConnectable() const override;
60 bool IsConnecting() const override;
scheib59e28a22015-07-14 00:38:3261 bool ExpectingPinCode() const override;
62 bool ExpectingPasskey() const override;
63 bool ExpectingConfirmation() const override;
64 void GetConnectionInfo(const ConnectionInfoCallback& callback) override;
tengsc967751962017-05-25 05:33:5065 void SetConnectionLatency(ConnectionLatency connection_latency,
66 const base::Closure& callback,
67 const ErrorCallback& error_callback) override;
scheib59e28a22015-07-14 00:38:3268 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;
avi176e2692015-12-22 19:26:5272 void SetPasskey(uint32_t passkey) override;
scheib59e28a22015-07-14 00:38:3273 void ConfirmPairing() override;
74 void RejectPairing() override;
75 void CancelPairing() override;
76 void Disconnect(const base::Closure& callback,
77 const ErrorCallback& error_callback) override;
stevenjb56612932015-11-12 23:14:5778 void Forget(const base::Closure& callback,
79 const ErrorCallback& error_callback) override;
scheib59e28a22015-07-14 00:38:3280 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;
scheib59e28a22015-07-14 00:38:3288
scheibaf7ea912015-09-23 20:27:5389 // Callback indicating when GATT client has connected/disconnected.
90 // See android.bluetooth.BluetoothGattCallback.onConnectionStateChange.
torneccbc0322015-11-27 12:33:5691 void OnConnectionStateChange(
92 JNIEnv* env,
93 const base::android::JavaParamRef<jobject>& jcaller,
94 int32_t status,
95 bool connected);
scheibaf7ea912015-09-23 20:27:5396
ortunodeb7045e2015-11-02 21:53:1097 // Callback indicating when all services of the device have been
98 // discovered.
torneccbc0322015-11-27 12:33:5699 void OnGattServicesDiscovered(
100 JNIEnv* env,
101 const base::android::JavaParamRef<jobject>& jcaller);
ortunodeb7045e2015-11-02 21:53:10102
scheibc3ecc9f2015-10-10 15:49:16103 // 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,
torneccbc0322015-11-27 12:33:56107 const base::android::JavaParamRef<jobject>& caller,
scheib494b0b22016-01-12 19:20:52108 const base::android::JavaParamRef<jstring>& instance_id,
torneccbc0322015-11-27 12:33:56109 const base::android::JavaParamRef<jobject>&
110 bluetooth_gatt_service_wrapper); // BluetoothGattServiceWrapper
scheibc3ecc9f2015-10-10 15:49:16111
scheib59e28a22015-07-14 00:38:32112 protected:
scheibeea0c48e2015-09-16 22:09:54113 BluetoothDeviceAndroid(BluetoothAdapterAndroid* adapter);
scheib59e28a22015-07-14 00:38:32114
115 // BluetoothDevice:
scheib95560872015-09-21 05:09:33116 void CreateGattConnectionImpl() override;
117 void DisconnectGatt() override;
scheib59e28a22015-07-14 00:38:32118
119 // Java object org.chromium.device.bluetooth.ChromeBluetoothDevice.
120 base::android::ScopedJavaGlobalRef<jobject> j_device_;
121
scheib95560872015-09-21 05:09:33122 bool gatt_connected_ = false;
123
scheib59e28a22015-07-14 00:38:32124 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceAndroid);
125};
126
127} // namespace device
128
129#endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_ANDROID_H_