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 "device/bluetooth/bluetooth_discovery_session_outcome.h"
16 #include "jni/ChromeBluetoothAdapter_jni.h"
18 using base::android::AttachCurrentThread
;
19 using base::android::ConvertJavaStringToUTF8
;
24 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::CreateAdapter(
25 const InitCallback
& init_callback
) {
26 return BluetoothAdapterAndroid::Create(
27 BluetoothAdapterWrapper_CreateWithDefaultAdapter().obj());
31 base::WeakPtr
<BluetoothAdapterAndroid
> BluetoothAdapterAndroid::Create(
32 jobject bluetooth_adapter_wrapper
) { // Java Type: bluetoothAdapterWrapper
33 BluetoothAdapterAndroid
* adapter
= new BluetoothAdapterAndroid();
35 adapter
->j_adapter_
.Reset(Java_ChromeBluetoothAdapter_create(
36 AttachCurrentThread(), reinterpret_cast<intptr_t>(adapter
),
37 bluetooth_adapter_wrapper
));
39 return adapter
->weak_ptr_factory_
.GetWeakPtr();
43 bool BluetoothAdapterAndroid::RegisterJNI(JNIEnv
* env
) {
44 return RegisterNativesImpl(env
); // Generated in BluetoothAdapter_jni.h
47 std::string
BluetoothAdapterAndroid::GetAddress() const {
48 return ConvertJavaStringToUTF8(Java_ChromeBluetoothAdapter_getAddress(
49 AttachCurrentThread(), j_adapter_
.obj()));
52 std::string
BluetoothAdapterAndroid::GetName() const {
53 return ConvertJavaStringToUTF8(Java_ChromeBluetoothAdapter_getName(
54 AttachCurrentThread(), j_adapter_
.obj()));
57 void BluetoothAdapterAndroid::SetName(const std::string
& name
,
58 const base::Closure
& callback
,
59 const ErrorCallback
& error_callback
) {
63 bool BluetoothAdapterAndroid::IsInitialized() const {
67 bool BluetoothAdapterAndroid::IsPresent() const {
68 return Java_ChromeBluetoothAdapter_isPresent(AttachCurrentThread(),
72 bool BluetoothAdapterAndroid::IsPowered() const {
73 return Java_ChromeBluetoothAdapter_isPowered(AttachCurrentThread(),
77 void BluetoothAdapterAndroid::SetPowered(bool powered
,
78 const base::Closure
& callback
,
79 const ErrorCallback
& error_callback
) {
83 bool BluetoothAdapterAndroid::IsDiscoverable() const {
84 return Java_ChromeBluetoothAdapter_isDiscoverable(AttachCurrentThread(),
88 void BluetoothAdapterAndroid::SetDiscoverable(
90 const base::Closure
& callback
,
91 const ErrorCallback
& error_callback
) {
95 bool BluetoothAdapterAndroid::IsDiscovering() const {
96 return Java_ChromeBluetoothAdapter_isDiscovering(AttachCurrentThread(),
100 void BluetoothAdapterAndroid::CreateRfcommService(
101 const BluetoothUUID
& uuid
,
102 const ServiceOptions
& options
,
103 const CreateServiceCallback
& callback
,
104 const CreateServiceErrorCallback
& error_callback
) {
106 error_callback
.Run("Not Implemented");
109 void BluetoothAdapterAndroid::CreateL2capService(
110 const BluetoothUUID
& uuid
,
111 const ServiceOptions
& options
,
112 const CreateServiceCallback
& callback
,
113 const CreateServiceErrorCallback
& error_callback
) {
115 error_callback
.Run("Not Implemented");
118 void BluetoothAdapterAndroid::RegisterAudioSink(
119 const BluetoothAudioSink::Options
& options
,
120 const AcquiredCallback
& callback
,
121 const BluetoothAudioSink::ErrorCallback
& error_callback
) {
122 error_callback
.Run(BluetoothAudioSink::ERROR_UNSUPPORTED_PLATFORM
);
125 void BluetoothAdapterAndroid::RegisterAdvertisement(
126 scoped_ptr
<BluetoothAdvertisement::Data
> advertisement_data
,
127 const CreateAdvertisementCallback
& callback
,
128 const CreateAdvertisementErrorCallback
& error_callback
) {
129 error_callback
.Run(BluetoothAdvertisement::ERROR_UNSUPPORTED_PLATFORM
);
132 void BluetoothAdapterAndroid::OnScanFailed(JNIEnv
* env
, jobject caller
) {
133 MarkDiscoverySessionsAsInactive();
136 void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan(
139 const jstring
& address
,
140 jobject bluetooth_device_wrapper
, // Java Type: bluetoothDeviceWrapper
141 jobject advertised_uuids
) { // Java Type: List<ParcelUuid>
142 BluetoothDevice
*& device
= devices_
[ConvertJavaStringToUTF8(env
, address
)];
144 device
= BluetoothDeviceAndroid::Create(this, bluetooth_device_wrapper
);
145 static_cast<BluetoothDeviceAndroid
*>(device
)
146 ->UpdateAdvertisedUUIDs(advertised_uuids
);
147 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
148 DeviceAdded(this, device
));
150 if (static_cast<BluetoothDeviceAndroid
*>(device
)
151 ->UpdateAdvertisedUUIDs(advertised_uuids
)) {
152 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
153 DeviceChanged(this, device
));
158 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) {
161 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() {
162 Java_ChromeBluetoothAdapter_onBluetoothAdapterAndroidDestruction(
163 AttachCurrentThread(), j_adapter_
.obj());
166 void BluetoothAdapterAndroid::AddDiscoverySession(
167 BluetoothDiscoveryFilter
* discovery_filter
,
168 const base::Closure
& callback
,
169 const DiscoverySessionErrorCallback
& error_callback
) {
170 // TODO(scheib): Support filters crbug.com/490401
171 if (Java_ChromeBluetoothAdapter_addDiscoverySession(AttachCurrentThread(),
175 // TODO(scheib): Eventually wire the SCAN_FAILED result through to here.
176 error_callback
.Run(UMABluetoothDiscoverySessionOutcome::UNKNOWN
);
180 void BluetoothAdapterAndroid::RemoveDiscoverySession(
181 BluetoothDiscoveryFilter
* discovery_filter
,
182 const base::Closure
& callback
,
183 const DiscoverySessionErrorCallback
& error_callback
) {
184 if (Java_ChromeBluetoothAdapter_removeDiscoverySession(AttachCurrentThread(),
188 // TODO(scheib): Eventually wire the SCAN_FAILED result through to here.
189 error_callback
.Run(UMABluetoothDiscoverySessionOutcome::UNKNOWN
);
193 void BluetoothAdapterAndroid::SetDiscoveryFilter(
194 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
195 const base::Closure
& callback
,
196 const DiscoverySessionErrorCallback
& error_callback
) {
197 // TODO(scheib): Support filters crbug.com/490401
199 error_callback
.Run(UMABluetoothDiscoverySessionOutcome::NOT_IMPLEMENTED
);
202 void BluetoothAdapterAndroid::RemovePairingDelegateInternal(
203 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) {
206 } // namespace device