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/bluetooth_advertisement.h"
13 #include "jni/BluetoothAdapter_jni.h"
15 using base::android::AttachCurrentThread
;
16 using base::android::ConvertJavaStringToUTF8
;
21 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::CreateAdapter(
22 const InitCallback
& init_callback
) {
23 return BluetoothAdapterAndroid::CreateAdapter();
27 base::WeakPtr
<BluetoothAdapterAndroid
>
28 BluetoothAdapterAndroid::CreateAdapter() {
29 BluetoothAdapterAndroid
* adapter
= new BluetoothAdapterAndroid();
30 adapter
->j_bluetooth_adapter_
.Reset(Java_BluetoothAdapter_create(
31 AttachCurrentThread(), base::android::GetApplicationContext()));
32 return adapter
->weak_ptr_factory_
.GetWeakPtr();
35 base::WeakPtr
<BluetoothAdapterAndroid
>
36 BluetoothAdapterAndroid::CreateAdapterWithoutPermissionForTesting() {
37 BluetoothAdapterAndroid
* adapter
= new BluetoothAdapterAndroid();
38 adapter
->j_bluetooth_adapter_
.Reset(
39 Java_BluetoothAdapter_createWithoutPermissionForTesting(
40 AttachCurrentThread(), base::android::GetApplicationContext()));
41 return adapter
->weak_ptr_factory_
.GetWeakPtr();
45 bool BluetoothAdapterAndroid::RegisterJNI(JNIEnv
* env
) {
46 return RegisterNativesImpl(env
); // Generated in BluetoothAdapter_jni.h
49 bool BluetoothAdapterAndroid::HasBluetoothPermission() const {
50 return Java_BluetoothAdapter_hasBluetoothPermission(
51 AttachCurrentThread(), j_bluetooth_adapter_
.obj());
54 std::string
BluetoothAdapterAndroid::GetAddress() const {
55 return ConvertJavaStringToUTF8(Java_BluetoothAdapter_getAddress(
56 AttachCurrentThread(), j_bluetooth_adapter_
.obj()));
59 std::string
BluetoothAdapterAndroid::GetName() const {
60 return ConvertJavaStringToUTF8(Java_BluetoothAdapter_getName(
61 AttachCurrentThread(), j_bluetooth_adapter_
.obj()));
64 void BluetoothAdapterAndroid::SetName(const std::string
& name
,
65 const base::Closure
& callback
,
66 const ErrorCallback
& error_callback
) {
70 bool BluetoothAdapterAndroid::IsInitialized() const {
74 bool BluetoothAdapterAndroid::IsPresent() const {
75 return Java_BluetoothAdapter_isPresent(AttachCurrentThread(),
76 j_bluetooth_adapter_
.obj());
79 bool BluetoothAdapterAndroid::IsPowered() const {
80 return Java_BluetoothAdapter_isPowered(AttachCurrentThread(),
81 j_bluetooth_adapter_
.obj());
84 void BluetoothAdapterAndroid::SetPowered(bool powered
,
85 const base::Closure
& callback
,
86 const ErrorCallback
& error_callback
) {
90 bool BluetoothAdapterAndroid::IsDiscoverable() const {
91 return Java_BluetoothAdapter_isDiscoverable(AttachCurrentThread(),
92 j_bluetooth_adapter_
.obj());
95 void BluetoothAdapterAndroid::SetDiscoverable(
97 const base::Closure
& callback
,
98 const ErrorCallback
& error_callback
) {
102 bool BluetoothAdapterAndroid::IsDiscovering() const {
103 return Java_BluetoothAdapter_isDiscovering(AttachCurrentThread(),
104 j_bluetooth_adapter_
.obj());
107 void BluetoothAdapterAndroid::CreateRfcommService(
108 const BluetoothUUID
& uuid
,
109 const ServiceOptions
& options
,
110 const CreateServiceCallback
& callback
,
111 const CreateServiceErrorCallback
& error_callback
) {
113 error_callback
.Run("Not Implemented");
116 void BluetoothAdapterAndroid::CreateL2capService(
117 const BluetoothUUID
& uuid
,
118 const ServiceOptions
& options
,
119 const CreateServiceCallback
& callback
,
120 const CreateServiceErrorCallback
& error_callback
) {
122 error_callback
.Run("Not Implemented");
125 void BluetoothAdapterAndroid::RegisterAudioSink(
126 const BluetoothAudioSink::Options
& options
,
127 const AcquiredCallback
& callback
,
128 const BluetoothAudioSink::ErrorCallback
& error_callback
) {
129 error_callback
.Run(BluetoothAudioSink::ERROR_UNSUPPORTED_PLATFORM
);
132 void BluetoothAdapterAndroid::RegisterAdvertisement(
133 scoped_ptr
<BluetoothAdvertisement::Data
> advertisement_data
,
134 const CreateAdvertisementCallback
& callback
,
135 const CreateAdvertisementErrorCallback
& error_callback
) {
136 error_callback
.Run(BluetoothAdvertisement::ERROR_UNSUPPORTED_PLATFORM
);
139 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) {
142 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() {
145 void BluetoothAdapterAndroid::AddDiscoverySession(
146 BluetoothDiscoveryFilter
* discovery_filter
,
147 const base::Closure
& callback
,
148 const ErrorCallback
& error_callback
) {
149 error_callback
.Run();
152 void BluetoothAdapterAndroid::RemoveDiscoverySession(
153 BluetoothDiscoveryFilter
* discovery_filter
,
154 const base::Closure
& callback
,
155 const ErrorCallback
& error_callback
) {
156 error_callback
.Run();
159 void BluetoothAdapterAndroid::SetDiscoveryFilter(
160 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
161 const base::Closure
& callback
,
162 const ErrorCallback
& error_callback
) {
163 error_callback
.Run();
166 void BluetoothAdapterAndroid::RemovePairingDelegateInternal(
167 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) {
170 } // namespace device