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 {
83 bool BluetoothDeviceAndroid::IsConnectable() const {
88 bool BluetoothDeviceAndroid::IsConnecting() const {
93 BluetoothDevice::UUIDList
BluetoothDeviceAndroid::GetUUIDs() const {
94 JNIEnv
* env
= AttachCurrentThread();
95 std::vector
<std::string
> uuid_strings
;
96 AppendJavaStringArrayToStringVector(
97 env
, Java_ChromeBluetoothDevice_getUuids(env
, j_device_
.obj()).obj(),
99 BluetoothDevice::UUIDList uuids
;
100 uuids
.reserve(uuid_strings
.size());
101 for (auto uuid_string
: uuid_strings
) {
102 uuids
.push_back(BluetoothUUID(uuid_string
));
107 int16
BluetoothDeviceAndroid::GetInquiryRSSI() const {
109 return kUnknownPower
;
112 int16
BluetoothDeviceAndroid::GetInquiryTxPower() const {
114 return kUnknownPower
;
117 bool BluetoothDeviceAndroid::ExpectingPinCode() const {
122 bool BluetoothDeviceAndroid::ExpectingPasskey() const {
127 bool BluetoothDeviceAndroid::ExpectingConfirmation() const {
132 void BluetoothDeviceAndroid::GetConnectionInfo(
133 const ConnectionInfoCallback
& callback
) {
135 callback
.Run(ConnectionInfo());
138 void BluetoothDeviceAndroid::Connect(
139 PairingDelegate
* pairing_delegate
,
140 const base::Closure
& callback
,
141 const ConnectErrorCallback
& error_callback
) {
145 void BluetoothDeviceAndroid::SetPinCode(const std::string
& pincode
) {
149 void BluetoothDeviceAndroid::SetPasskey(uint32 passkey
) {
153 void BluetoothDeviceAndroid::ConfirmPairing() {
157 void BluetoothDeviceAndroid::RejectPairing() {
161 void BluetoothDeviceAndroid::CancelPairing() {
165 void BluetoothDeviceAndroid::Disconnect(const base::Closure
& callback
,
166 const ErrorCallback
& error_callback
) {
170 void BluetoothDeviceAndroid::Forget(const ErrorCallback
& error_callback
) {
174 void BluetoothDeviceAndroid::ConnectToService(
175 const BluetoothUUID
& uuid
,
176 const ConnectToServiceCallback
& callback
,
177 const ConnectToServiceErrorCallback
& error_callback
) {
181 void BluetoothDeviceAndroid::ConnectToServiceInsecurely(
182 const BluetoothUUID
& uuid
,
183 const ConnectToServiceCallback
& callback
,
184 const ConnectToServiceErrorCallback
& error_callback
) {
188 void BluetoothDeviceAndroid::CreateGattConnection(
189 const GattConnectionCallback
& callback
,
190 const ConnectErrorCallback
& error_callback
) {
194 BluetoothDeviceAndroid::BluetoothDeviceAndroid(BluetoothAdapterAndroid
* adapter
)
195 : BluetoothDevice(adapter
) {}
197 std::string
BluetoothDeviceAndroid::GetDeviceName() const {
198 return ConvertJavaStringToUTF8(Java_ChromeBluetoothDevice_getDeviceName(
199 AttachCurrentThread(), j_device_
.obj()));
202 } // namespace device