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.
5 #include "device/bluetooth/bluetooth_device_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "device/bluetooth/bluetooth_adapter_android.h"
11 #include "jni/ChromeBluetoothDevice_jni.h"
13 using base::android::AttachCurrentThread
;
14 using base::android::AppendJavaStringArrayToStringVector
;
18 BluetoothDeviceAndroid
* BluetoothDeviceAndroid::Create(
19 BluetoothAdapterAndroid
* adapter
,
20 jobject bluetooth_device_wrapper
) { // Java Type: bluetoothDeviceWrapper
21 BluetoothDeviceAndroid
* device
= new BluetoothDeviceAndroid(adapter
);
23 device
->j_device_
.Reset(Java_ChromeBluetoothDevice_create(
24 AttachCurrentThread(), bluetooth_device_wrapper
));
29 BluetoothDeviceAndroid::~BluetoothDeviceAndroid() {
32 bool BluetoothDeviceAndroid::UpdateAdvertisedUUIDs(jobject advertised_uuids
) {
33 return Java_ChromeBluetoothDevice_updateAdvertisedUUIDs(
34 AttachCurrentThread(), j_device_
.obj(), advertised_uuids
);
38 bool BluetoothDeviceAndroid::RegisterJNI(JNIEnv
* env
) {
39 return RegisterNativesImpl(env
); // Generated in ChromeBluetoothDevice_jni.h
42 uint32
BluetoothDeviceAndroid::GetBluetoothClass() const {
43 return Java_ChromeBluetoothDevice_getBluetoothClass(AttachCurrentThread(),
47 std::string
BluetoothDeviceAndroid::GetAddress() const {
48 return ConvertJavaStringToUTF8(Java_ChromeBluetoothDevice_getAddress(
49 AttachCurrentThread(), j_device_
.obj()));
52 BluetoothDevice::VendorIDSource
BluetoothDeviceAndroid::GetVendorIDSource()
54 // Android API does not provide Vendor ID.
55 return VENDOR_ID_UNKNOWN
;
58 uint16
BluetoothDeviceAndroid::GetVendorID() const {
59 // Android API does not provide Vendor ID.
63 uint16
BluetoothDeviceAndroid::GetProductID() const {
64 // Android API does not provide Product ID.
68 uint16
BluetoothDeviceAndroid::GetDeviceID() const {
69 // Android API does not provide Device ID.
73 bool BluetoothDeviceAndroid::IsPaired() const {
74 return Java_ChromeBluetoothDevice_isPaired(AttachCurrentThread(),
78 bool BluetoothDeviceAndroid::IsConnected() const {
79 return IsGattConnected();
82 bool BluetoothDeviceAndroid::IsGattConnected() const {
83 return gatt_connected_
;
86 bool BluetoothDeviceAndroid::IsConnectable() const {
91 bool BluetoothDeviceAndroid::IsConnecting() const {
96 BluetoothDevice::UUIDList
BluetoothDeviceAndroid::GetUUIDs() const {
97 JNIEnv
* env
= AttachCurrentThread();
98 std::vector
<std::string
> uuid_strings
;
99 AppendJavaStringArrayToStringVector(
100 env
, Java_ChromeBluetoothDevice_getUuids(env
, j_device_
.obj()).obj(),
102 BluetoothDevice::UUIDList uuids
;
103 uuids
.reserve(uuid_strings
.size());
104 for (auto uuid_string
: uuid_strings
) {
105 uuids
.push_back(BluetoothUUID(uuid_string
));
110 int16
BluetoothDeviceAndroid::GetInquiryRSSI() const {
112 return kUnknownPower
;
115 int16
BluetoothDeviceAndroid::GetInquiryTxPower() const {
117 return kUnknownPower
;
120 bool BluetoothDeviceAndroid::ExpectingPinCode() const {
125 bool BluetoothDeviceAndroid::ExpectingPasskey() const {
130 bool BluetoothDeviceAndroid::ExpectingConfirmation() const {
135 void BluetoothDeviceAndroid::GetConnectionInfo(
136 const ConnectionInfoCallback
& callback
) {
138 callback
.Run(ConnectionInfo());
141 void BluetoothDeviceAndroid::Connect(
142 PairingDelegate
* pairing_delegate
,
143 const base::Closure
& callback
,
144 const ConnectErrorCallback
& error_callback
) {
148 void BluetoothDeviceAndroid::SetPinCode(const std::string
& pincode
) {
152 void BluetoothDeviceAndroid::SetPasskey(uint32 passkey
) {
156 void BluetoothDeviceAndroid::ConfirmPairing() {
160 void BluetoothDeviceAndroid::RejectPairing() {
164 void BluetoothDeviceAndroid::CancelPairing() {
168 void BluetoothDeviceAndroid::Disconnect(const base::Closure
& callback
,
169 const ErrorCallback
& error_callback
) {
173 void BluetoothDeviceAndroid::Forget(const ErrorCallback
& error_callback
) {
177 void BluetoothDeviceAndroid::ConnectToService(
178 const BluetoothUUID
& uuid
,
179 const ConnectToServiceCallback
& callback
,
180 const ConnectToServiceErrorCallback
& error_callback
) {
184 void BluetoothDeviceAndroid::ConnectToServiceInsecurely(
185 const BluetoothUUID
& uuid
,
186 const ConnectToServiceCallback
& callback
,
187 const ConnectToServiceErrorCallback
& error_callback
) {
191 BluetoothDeviceAndroid::BluetoothDeviceAndroid(BluetoothAdapterAndroid
* adapter
)
192 : BluetoothDevice(adapter
) {}
194 std::string
BluetoothDeviceAndroid::GetDeviceName() const {
195 return ConvertJavaStringToUTF8(Java_ChromeBluetoothDevice_getDeviceName(
196 AttachCurrentThread(), j_device_
.obj()));
199 void BluetoothDeviceAndroid::CreateGattConnectionImpl() {
200 // Implemented in following patch https://codereview.chromium.org/1256313002
202 DidFailToConnectGatt(ERROR_UNKNOWN
);
205 void BluetoothDeviceAndroid::DisconnectGatt() {
206 // Implemented in following patch https://codereview.chromium.org/1256313002
210 } // namespace device