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_adapter_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "device/bluetooth/android/wrappers.h"
13 #include "device/bluetooth/bluetooth_advertisement.h"
14 #include "device/bluetooth/bluetooth_device_android.h"
15 #include "jni/ChromeBluetoothAdapter_jni.h"
17 using base::android::AttachCurrentThread
;
18 using base::android::ConvertJavaStringToUTF8
;
23 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::CreateAdapter(
24 const InitCallback
& init_callback
) {
25 return BluetoothAdapterAndroid::Create(
26 BluetoothAdapterWrapper_CreateWithDefaultAdapter().obj());
30 base::WeakPtr
<BluetoothAdapterAndroid
> BluetoothAdapterAndroid::Create(
31 jobject bluetooth_adapter_wrapper
) { // Java Type: bluetoothAdapterWrapper
32 BluetoothAdapterAndroid
* adapter
= new BluetoothAdapterAndroid();
34 adapter
->j_adapter_
.Reset(Java_ChromeBluetoothAdapter_create(
35 AttachCurrentThread(), reinterpret_cast<intptr_t>(adapter
),
36 bluetooth_adapter_wrapper
));
38 return adapter
->weak_ptr_factory_
.GetWeakPtr();
42 bool BluetoothAdapterAndroid::RegisterJNI(JNIEnv
* env
) {
43 return RegisterNativesImpl(env
); // Generated in BluetoothAdapter_jni.h
46 std::string
BluetoothAdapterAndroid::GetAddress() const {
47 return ConvertJavaStringToUTF8(Java_ChromeBluetoothAdapter_getAddress(
48 AttachCurrentThread(), j_adapter_
.obj()));
51 std::string
BluetoothAdapterAndroid::GetName() const {
52 return ConvertJavaStringToUTF8(Java_ChromeBluetoothAdapter_getName(
53 AttachCurrentThread(), j_adapter_
.obj()));
56 void BluetoothAdapterAndroid::SetName(const std::string
& name
,
57 const base::Closure
& callback
,
58 const ErrorCallback
& error_callback
) {
62 bool BluetoothAdapterAndroid::IsInitialized() const {
66 bool BluetoothAdapterAndroid::IsPresent() const {
67 return Java_ChromeBluetoothAdapter_isPresent(AttachCurrentThread(),
71 bool BluetoothAdapterAndroid::IsPowered() const {
72 return Java_ChromeBluetoothAdapter_isPowered(AttachCurrentThread(),
76 void BluetoothAdapterAndroid::SetPowered(bool powered
,
77 const base::Closure
& callback
,
78 const ErrorCallback
& error_callback
) {
82 bool BluetoothAdapterAndroid::IsDiscoverable() const {
83 return Java_ChromeBluetoothAdapter_isDiscoverable(AttachCurrentThread(),
87 void BluetoothAdapterAndroid::SetDiscoverable(
89 const base::Closure
& callback
,
90 const ErrorCallback
& error_callback
) {
94 bool BluetoothAdapterAndroid::IsDiscovering() const {
95 return Java_ChromeBluetoothAdapter_isDiscovering(AttachCurrentThread(),
99 void BluetoothAdapterAndroid::CreateRfcommService(
100 const BluetoothUUID
& uuid
,
101 const ServiceOptions
& options
,
102 const CreateServiceCallback
& callback
,
103 const CreateServiceErrorCallback
& error_callback
) {
105 error_callback
.Run("Not Implemented");
108 void BluetoothAdapterAndroid::CreateL2capService(
109 const BluetoothUUID
& uuid
,
110 const ServiceOptions
& options
,
111 const CreateServiceCallback
& callback
,
112 const CreateServiceErrorCallback
& error_callback
) {
114 error_callback
.Run("Not Implemented");
117 void BluetoothAdapterAndroid::RegisterAudioSink(
118 const BluetoothAudioSink::Options
& options
,
119 const AcquiredCallback
& callback
,
120 const BluetoothAudioSink::ErrorCallback
& error_callback
) {
121 error_callback
.Run(BluetoothAudioSink::ERROR_UNSUPPORTED_PLATFORM
);
124 void BluetoothAdapterAndroid::RegisterAdvertisement(
125 scoped_ptr
<BluetoothAdvertisement::Data
> advertisement_data
,
126 const CreateAdvertisementCallback
& callback
,
127 const CreateAdvertisementErrorCallback
& error_callback
) {
128 error_callback
.Run(BluetoothAdvertisement::ERROR_UNSUPPORTED_PLATFORM
);
131 void BluetoothAdapterAndroid::OnScanFailed(JNIEnv
* env
, jobject caller
) {
132 MarkDiscoverySessionsAsInactive();
135 void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan(
138 const jstring
& address
,
139 jobject bluetooth_device_wrapper
, // Java Type: bluetoothDeviceWrapper
140 jobject advertised_uuids
) { // Java Type: List<ParcelUuid>
141 BluetoothDevice
*& device
= devices_
[ConvertJavaStringToUTF8(env
, address
)];
143 device
= BluetoothDeviceAndroid::Create(bluetooth_device_wrapper
);
144 static_cast<BluetoothDeviceAndroid
*>(device
)
145 ->UpdateAdvertisedUUIDs(advertised_uuids
);
146 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
147 DeviceAdded(this, device
));
149 if (static_cast<BluetoothDeviceAndroid
*>(device
)
150 ->UpdateAdvertisedUUIDs(advertised_uuids
)) {
151 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
152 DeviceChanged(this, device
));
157 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) {
160 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() {
161 Java_ChromeBluetoothAdapter_onBluetoothAdapterAndroidDestruction(
162 AttachCurrentThread(), j_adapter_
.obj());
165 void BluetoothAdapterAndroid::AddDiscoverySession(
166 BluetoothDiscoveryFilter
* discovery_filter
,
167 const base::Closure
& callback
,
168 const ErrorCallback
& error_callback
) {
169 // TODO(scheib): Support filters crbug.com/490401
170 if (Java_ChromeBluetoothAdapter_addDiscoverySession(AttachCurrentThread(),
174 error_callback
.Run();
178 void BluetoothAdapterAndroid::RemoveDiscoverySession(
179 BluetoothDiscoveryFilter
* discovery_filter
,
180 const base::Closure
& callback
,
181 const ErrorCallback
& error_callback
) {
182 if (Java_ChromeBluetoothAdapter_removeDiscoverySession(AttachCurrentThread(),
186 error_callback
.Run();
190 void BluetoothAdapterAndroid::SetDiscoveryFilter(
191 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
192 const base::Closure
& callback
,
193 const ErrorCallback
& error_callback
) {
194 // TODO(scheib): Support filters crbug.com/490401
196 error_callback
.Run();
199 void BluetoothAdapterAndroid::RemovePairingDelegateInternal(
200 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) {
203 } // namespace device