1 // Copyright 2014 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 "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.h"
8 #include "base/lazy_instance.h"
9 #include "base/strings/stringprintf.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "extensions/browser/api/bluetooth_low_energy/utils.h"
12 #include "extensions/browser/event_router.h"
13 #include "extensions/common/api/bluetooth/bluetooth_manifest_data.h"
14 #include "extensions/common/api/bluetooth_low_energy.h"
15 #include "extensions/common/permissions/permissions_data.h"
17 using content::BrowserContext
;
18 using content::BrowserThread
;
20 namespace apibtle
= extensions::core_api::bluetooth_low_energy
;
22 namespace extensions
{
26 const char kErrorAdapterNotInitialized
[] =
27 "Could not initialize Bluetooth adapter";
28 const char kErrorAlreadyConnected
[] = "Already connected";
29 const char kErrorAlreadyNotifying
[] = "Already notifying";
30 const char kErrorAuthenticationFailed
[] = "Authentication failed";
31 const char kErrorCanceled
[] = "Request canceled";
32 const char kErrorGattNotSupported
[] = "Operation not supported by this service";
33 const char kErrorHigherSecurity
[] = "Higher security needed";
34 const char kErrorInProgress
[] = "In progress";
35 const char kErrorInsufficientAuthorization
[] = "Insufficient authorization";
36 const char kErrorInvalidLength
[] = "Invalid attribute value length";
37 const char kErrorNotConnected
[] = "Not connected";
38 const char kErrorNotFound
[] = "Instance not found";
39 const char kErrorNotNotifying
[] = "Not notifying";
40 const char kErrorOperationFailed
[] = "Operation failed";
41 const char kErrorPermissionDenied
[] = "Permission denied";
42 const char kErrorPlatformNotSupported
[] =
43 "This operation is not supported on the current platform";
44 const char kErrorTimeout
[] = "Operation timed out";
45 const char kErrorUnsupportedDevice
[] =
46 "This device is not supported on the current platform";
48 // Returns the correct error string based on error status |status|. This is used
49 // to set the value of |chrome.runtime.lastError.message| and should not be
50 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|.
51 std::string
StatusToString(BluetoothLowEnergyEventRouter::Status status
) {
53 case BluetoothLowEnergyEventRouter::kStatusErrorPermissionDenied
:
54 return kErrorPermissionDenied
;
55 case BluetoothLowEnergyEventRouter::kStatusErrorNotFound
:
56 return kErrorNotFound
;
57 case BluetoothLowEnergyEventRouter::kStatusErrorAlreadyConnected
:
58 return kErrorAlreadyConnected
;
59 case BluetoothLowEnergyEventRouter::kStatusErrorAlreadyNotifying
:
60 return kErrorAlreadyNotifying
;
61 case BluetoothLowEnergyEventRouter::kStatusErrorNotConnected
:
62 return kErrorNotConnected
;
63 case BluetoothLowEnergyEventRouter::kStatusErrorInsufficientAuthorization
:
64 return kErrorInsufficientAuthorization
;
65 case BluetoothLowEnergyEventRouter::kStatusErrorNotNotifying
:
66 return kErrorNotNotifying
;
67 case BluetoothLowEnergyEventRouter::kStatusErrorInProgress
:
68 return kErrorInProgress
;
69 case BluetoothLowEnergyEventRouter::kStatusErrorAuthenticationFailed
:
70 return kErrorAuthenticationFailed
;
71 case BluetoothLowEnergyEventRouter::kStatusErrorHigherSecurity
:
72 return kErrorHigherSecurity
;
73 case BluetoothLowEnergyEventRouter::kStatusErrorCanceled
:
74 return kErrorCanceled
;
75 case BluetoothLowEnergyEventRouter::kStatusErrorTimeout
:
77 case BluetoothLowEnergyEventRouter::kStatusErrorUnsupportedDevice
:
78 return kErrorUnsupportedDevice
;
79 case BluetoothLowEnergyEventRouter::kStatusErrorInvalidLength
:
80 return kErrorInvalidLength
;
81 case BluetoothLowEnergyEventRouter::kStatusErrorGattNotSupported
:
82 return kErrorGattNotSupported
;
83 case BluetoothLowEnergyEventRouter::kStatusSuccess
:
87 return kErrorOperationFailed
;
92 extensions::BluetoothLowEnergyEventRouter
* GetEventRouter(
93 BrowserContext
* context
) {
94 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
95 return extensions::BluetoothLowEnergyAPI::Get(context
)->event_router();
98 void DoWorkCallback(const base::Callback
<bool()>& callback
) {
99 DCHECK(!callback
.is_null());
106 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<BluetoothLowEnergyAPI
> >
107 g_factory
= LAZY_INSTANCE_INITIALIZER
;
110 BrowserContextKeyedAPIFactory
<BluetoothLowEnergyAPI
>*
111 BluetoothLowEnergyAPI::GetFactoryInstance() {
112 return g_factory
.Pointer();
116 BluetoothLowEnergyAPI
* BluetoothLowEnergyAPI::Get(BrowserContext
* context
) {
117 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
118 return GetFactoryInstance()->Get(context
);
121 BluetoothLowEnergyAPI::BluetoothLowEnergyAPI(BrowserContext
* context
)
122 : event_router_(new BluetoothLowEnergyEventRouter(context
)) {
123 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
126 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() {
129 void BluetoothLowEnergyAPI::Shutdown() {
130 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
135 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {
138 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {
141 bool BluetoothLowEnergyExtensionFunction::RunAsync() {
142 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
144 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) {
145 error_
= kErrorPermissionDenied
;
149 BluetoothLowEnergyEventRouter
* event_router
=
150 GetEventRouter(browser_context());
151 if (!event_router
->IsBluetoothSupported()) {
152 SetError(kErrorPlatformNotSupported
);
156 // It is safe to pass |this| here as ExtensionFunction is refcounted.
157 if (!event_router
->InitializeAdapterAndInvokeCallback(base::Bind(
159 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork
, this)))) {
160 SetError(kErrorAdapterNotInitialized
);
167 bool BluetoothLowEnergyConnectFunction::DoWork() {
168 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
170 BluetoothLowEnergyEventRouter
* event_router
=
171 GetEventRouter(browser_context());
173 // The adapter must be initialized at this point, but return an error instead
175 if (!event_router
->HasAdapter()) {
176 SetError(kErrorAdapterNotInitialized
);
181 scoped_ptr
<apibtle::Connect::Params
> params(
182 apibtle::Connect::Params::Create(*args_
));
183 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
185 bool persistent
= false; // Not persistent by default.
186 apibtle::ConnectProperties
* properties
= params
.get()->properties
.get();
188 persistent
= properties
->persistent
;
190 event_router
->Connect(
193 params
->device_address
,
194 base::Bind(&BluetoothLowEnergyConnectFunction::SuccessCallback
, this),
195 base::Bind(&BluetoothLowEnergyConnectFunction::ErrorCallback
, this));
200 void BluetoothLowEnergyConnectFunction::SuccessCallback() {
204 void BluetoothLowEnergyConnectFunction::ErrorCallback(
205 BluetoothLowEnergyEventRouter::Status status
) {
206 SetError(StatusToString(status
));
210 bool BluetoothLowEnergyDisconnectFunction::DoWork() {
211 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
213 BluetoothLowEnergyEventRouter
* event_router
=
214 GetEventRouter(browser_context());
216 // The adapter must be initialized at this point, but return an error instead
218 if (!event_router
->HasAdapter()) {
219 SetError(kErrorAdapterNotInitialized
);
224 scoped_ptr
<apibtle::Disconnect::Params
> params(
225 apibtle::Disconnect::Params::Create(*args_
));
226 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
228 event_router
->Disconnect(
230 params
->device_address
,
231 base::Bind(&BluetoothLowEnergyDisconnectFunction::SuccessCallback
, this),
232 base::Bind(&BluetoothLowEnergyDisconnectFunction::ErrorCallback
, this));
237 void BluetoothLowEnergyDisconnectFunction::SuccessCallback() {
241 void BluetoothLowEnergyDisconnectFunction::ErrorCallback(
242 BluetoothLowEnergyEventRouter::Status status
) {
243 SetError(StatusToString(status
));
247 bool BluetoothLowEnergyGetServiceFunction::DoWork() {
248 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
250 BluetoothLowEnergyEventRouter
* event_router
=
251 GetEventRouter(browser_context());
253 // The adapter must be initialized at this point, but return an error instead
255 if (!event_router
->HasAdapter()) {
256 SetError(kErrorAdapterNotInitialized
);
261 scoped_ptr
<apibtle::GetService::Params
> params(
262 apibtle::GetService::Params::Create(*args_
));
263 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
265 apibtle::Service service
;
266 BluetoothLowEnergyEventRouter::Status status
=
267 event_router
->GetService(params
->service_id
, &service
);
268 if (status
!= BluetoothLowEnergyEventRouter::kStatusSuccess
) {
269 SetError(StatusToString(status
));
274 results_
= apibtle::GetService::Results::Create(service
);
280 bool BluetoothLowEnergyGetServicesFunction::DoWork() {
281 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
283 BluetoothLowEnergyEventRouter
* event_router
=
284 GetEventRouter(browser_context());
286 // The adapter must be initialized at this point, but return an error instead
288 if (!event_router
->HasAdapter()) {
289 SetError(kErrorAdapterNotInitialized
);
294 scoped_ptr
<apibtle::GetServices::Params
> params(
295 apibtle::GetServices::Params::Create(*args_
));
296 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
298 BluetoothLowEnergyEventRouter::ServiceList service_list
;
299 if (!event_router
->GetServices(params
->device_address
, &service_list
)) {
300 SetError(kErrorNotFound
);
305 results_
= apibtle::GetServices::Results::Create(service_list
);
311 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() {
312 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
314 BluetoothLowEnergyEventRouter
* event_router
=
315 GetEventRouter(browser_context());
317 // The adapter must be initialized at this point, but return an error instead
319 if (!event_router
->HasAdapter()) {
320 SetError(kErrorAdapterNotInitialized
);
325 scoped_ptr
<apibtle::GetCharacteristic::Params
> params(
326 apibtle::GetCharacteristic::Params::Create(*args_
));
327 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
329 apibtle::Characteristic characteristic
;
330 BluetoothLowEnergyEventRouter::Status status
=
331 event_router
->GetCharacteristic(
332 extension(), params
->characteristic_id
, &characteristic
);
333 if (status
!= BluetoothLowEnergyEventRouter::kStatusSuccess
) {
334 SetError(StatusToString(status
));
339 // Manually construct the result instead of using
340 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of
342 SetResult(apibtle::CharacteristicToValue(&characteristic
).release());
348 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() {
349 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
351 BluetoothLowEnergyEventRouter
* event_router
=
352 GetEventRouter(browser_context());
354 // The adapter must be initialized at this point, but return an error instead
356 if (!event_router
->HasAdapter()) {
357 SetError(kErrorAdapterNotInitialized
);
362 scoped_ptr
<apibtle::GetCharacteristics::Params
> params(
363 apibtle::GetCharacteristics::Params::Create(*args_
));
364 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
366 BluetoothLowEnergyEventRouter::CharacteristicList characteristic_list
;
367 BluetoothLowEnergyEventRouter::Status status
=
368 event_router
->GetCharacteristics(
369 extension(), params
->service_id
, &characteristic_list
);
370 if (status
!= BluetoothLowEnergyEventRouter::kStatusSuccess
) {
371 SetError(StatusToString(status
));
376 // Manually construct the result instead of using
377 // apibtle::GetCharacteristics::Result::Create as it doesn't convert lists of
379 scoped_ptr
<base::ListValue
> result(new base::ListValue());
380 for (BluetoothLowEnergyEventRouter::CharacteristicList::iterator iter
=
381 characteristic_list
.begin();
382 iter
!= characteristic_list
.end();
384 result
->Append(apibtle::CharacteristicToValue(iter
->get()).release());
386 SetResult(result
.release());
392 bool BluetoothLowEnergyGetIncludedServicesFunction::DoWork() {
393 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
395 BluetoothLowEnergyEventRouter
* event_router
=
396 GetEventRouter(browser_context());
398 // The adapter must be initialized at this point, but return an error instead
400 if (!event_router
->HasAdapter()) {
401 SetError(kErrorAdapterNotInitialized
);
406 scoped_ptr
<apibtle::GetIncludedServices::Params
> params(
407 apibtle::GetIncludedServices::Params::Create(*args_
));
408 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
410 BluetoothLowEnergyEventRouter::ServiceList service_list
;
411 BluetoothLowEnergyEventRouter::Status status
=
412 event_router
->GetIncludedServices(params
->service_id
, &service_list
);
413 if (status
!= BluetoothLowEnergyEventRouter::kStatusSuccess
) {
414 SetError(StatusToString(status
));
419 results_
= apibtle::GetIncludedServices::Results::Create(service_list
);
425 bool BluetoothLowEnergyGetDescriptorFunction::DoWork() {
426 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
428 BluetoothLowEnergyEventRouter
* event_router
=
429 GetEventRouter(browser_context());
431 // The adapter must be initialized at this point, but return an error instead
433 if (!event_router
->HasAdapter()) {
434 SetError(kErrorAdapterNotInitialized
);
439 scoped_ptr
<apibtle::GetDescriptor::Params
> params(
440 apibtle::GetDescriptor::Params::Create(*args_
));
441 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
443 apibtle::Descriptor descriptor
;
444 BluetoothLowEnergyEventRouter::Status status
= event_router
->GetDescriptor(
445 extension(), params
->descriptor_id
, &descriptor
);
446 if (status
!= BluetoothLowEnergyEventRouter::kStatusSuccess
) {
447 SetError(StatusToString(status
));
452 // Manually construct the result instead of using
453 // apibtle::GetDescriptor::Result::Create as it doesn't convert lists of enums
455 SetResult(apibtle::DescriptorToValue(&descriptor
).release());
461 bool BluetoothLowEnergyGetDescriptorsFunction::DoWork() {
462 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
464 BluetoothLowEnergyEventRouter
* event_router
=
465 GetEventRouter(browser_context());
467 // The adapter must be initialized at this point, but return an error instead
469 if (!event_router
->HasAdapter()) {
470 SetError(kErrorAdapterNotInitialized
);
475 scoped_ptr
<apibtle::GetDescriptors::Params
> params(
476 apibtle::GetDescriptors::Params::Create(*args_
));
477 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
479 BluetoothLowEnergyEventRouter::DescriptorList descriptor_list
;
480 BluetoothLowEnergyEventRouter::Status status
= event_router
->GetDescriptors(
481 extension(), params
->characteristic_id
, &descriptor_list
);
482 if (status
!= BluetoothLowEnergyEventRouter::kStatusSuccess
) {
483 SetError(StatusToString(status
));
488 // Manually construct the result instead of using
489 // apibtle::GetDescriptors::Result::Create as it doesn't convert lists of
491 scoped_ptr
<base::ListValue
> result(new base::ListValue());
492 for (BluetoothLowEnergyEventRouter::DescriptorList::iterator iter
=
493 descriptor_list
.begin();
494 iter
!= descriptor_list
.end();
496 result
->Append(apibtle::DescriptorToValue(iter
->get()).release());
498 SetResult(result
.release());
504 bool BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() {
505 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
507 BluetoothLowEnergyEventRouter
* event_router
=
508 GetEventRouter(browser_context());
510 // The adapter must be initialized at this point, but return an error instead
512 if (!event_router
->HasAdapter()) {
513 SetError(kErrorAdapterNotInitialized
);
518 scoped_ptr
<apibtle::ReadCharacteristicValue::Params
> params(
519 apibtle::ReadCharacteristicValue::Params::Create(*args_
));
520 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
522 instance_id_
= params
->characteristic_id
;
523 event_router
->ReadCharacteristicValue(
527 &BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback
,
530 &BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback
,
536 void BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback() {
537 // Obtain info on the characteristic and see whether or not the characteristic
539 apibtle::Characteristic characteristic
;
540 BluetoothLowEnergyEventRouter::Status status
=
541 GetEventRouter(browser_context())
542 ->GetCharacteristic(extension(), instance_id_
, &characteristic
);
543 if (status
!= BluetoothLowEnergyEventRouter::kStatusSuccess
) {
544 SetError(StatusToString(status
));
549 // Manually construct the result instead of using
550 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of
552 SetResult(apibtle::CharacteristicToValue(&characteristic
).release());
556 void BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback(
557 BluetoothLowEnergyEventRouter::Status status
) {
558 SetError(StatusToString(status
));
562 bool BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() {
563 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
565 BluetoothLowEnergyEventRouter
* event_router
=
566 GetEventRouter(browser_context());
568 // The adapter must be initialized at this point, but return an error instead
570 if (!event_router
->HasAdapter()) {
571 SetError(kErrorAdapterNotInitialized
);
576 scoped_ptr
<apibtle::WriteCharacteristicValue::Params
> params(
577 apibtle::WriteCharacteristicValue::Params::Create(*args_
));
578 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
580 std::vector
<uint8
> value(params
->value
.begin(), params
->value
.end());
581 event_router
->WriteCharacteristicValue(
583 params
->characteristic_id
,
586 &BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback
,
589 &BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback
,
595 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() {
596 results_
= apibtle::WriteCharacteristicValue::Results::Create();
600 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback(
601 BluetoothLowEnergyEventRouter::Status status
) {
602 SetError(StatusToString(status
));
606 bool BluetoothLowEnergyStartCharacteristicNotificationsFunction::DoWork() {
607 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
609 BluetoothLowEnergyEventRouter
* event_router
=
610 GetEventRouter(browser_context());
612 // The adapter must be initialized at this point, but return an error instead
614 if (!event_router
->HasAdapter()) {
615 SetError(kErrorAdapterNotInitialized
);
620 scoped_ptr
<apibtle::StartCharacteristicNotifications::Params
> params(
621 apibtle::StartCharacteristicNotifications::Params::Create(*args_
));
622 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
624 bool persistent
= false; // Not persistent by default.
625 apibtle::NotificationProperties
* properties
= params
.get()->properties
.get();
627 persistent
= properties
->persistent
;
629 event_router
->StartCharacteristicNotifications(
632 params
->characteristic_id
,
633 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction::
636 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction::
644 BluetoothLowEnergyStartCharacteristicNotificationsFunction::SuccessCallback() {
648 void BluetoothLowEnergyStartCharacteristicNotificationsFunction::ErrorCallback(
649 BluetoothLowEnergyEventRouter::Status status
) {
650 SetError(StatusToString(status
));
654 bool BluetoothLowEnergyStopCharacteristicNotificationsFunction::DoWork() {
655 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
657 BluetoothLowEnergyEventRouter
* event_router
=
658 GetEventRouter(browser_context());
660 // The adapter must be initialized at this point, but return an error instead
662 if (!event_router
->HasAdapter()) {
663 SetError(kErrorAdapterNotInitialized
);
668 scoped_ptr
<apibtle::StopCharacteristicNotifications::Params
> params(
669 apibtle::StopCharacteristicNotifications::Params::Create(*args_
));
670 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
672 event_router
->StopCharacteristicNotifications(
674 params
->characteristic_id
,
675 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction::
678 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction::
686 BluetoothLowEnergyStopCharacteristicNotificationsFunction::SuccessCallback() {
690 void BluetoothLowEnergyStopCharacteristicNotificationsFunction::ErrorCallback(
691 BluetoothLowEnergyEventRouter::Status status
) {
692 SetError(StatusToString(status
));
696 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() {
697 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
699 BluetoothLowEnergyEventRouter
* event_router
=
700 GetEventRouter(browser_context());
702 // The adapter must be initialized at this point, but return an error instead
704 if (!event_router
->HasAdapter()) {
705 SetError(kErrorAdapterNotInitialized
);
710 scoped_ptr
<apibtle::ReadDescriptorValue::Params
> params(
711 apibtle::ReadDescriptorValue::Params::Create(*args_
));
712 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
714 instance_id_
= params
->descriptor_id
;
715 event_router
->ReadDescriptorValue(
719 &BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback
,
721 base::Bind(&BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback
,
727 void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() {
728 // Obtain info on the descriptor and see whether or not the descriptor is
730 apibtle::Descriptor descriptor
;
731 BluetoothLowEnergyEventRouter::Status status
=
732 GetEventRouter(browser_context())
733 ->GetDescriptor(extension(), instance_id_
, &descriptor
);
734 if (status
!= BluetoothLowEnergyEventRouter::kStatusSuccess
) {
735 SetError(StatusToString(status
));
740 // Manually construct the result instead of using
741 // apibtle::GetDescriptor::Results::Create as it doesn't convert lists of
743 SetResult(apibtle::DescriptorToValue(&descriptor
).release());
747 void BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback(
748 BluetoothLowEnergyEventRouter::Status status
) {
749 SetError(StatusToString(status
));
753 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() {
754 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
756 BluetoothLowEnergyEventRouter
* event_router
=
757 GetEventRouter(browser_context());
759 // The adapter must be initialized at this point, but return an error instead
761 if (!event_router
->HasAdapter()) {
762 SetError(kErrorAdapterNotInitialized
);
767 scoped_ptr
<apibtle::WriteDescriptorValue::Params
> params(
768 apibtle::WriteDescriptorValue::Params::Create(*args_
));
769 EXTENSION_FUNCTION_VALIDATE(params
.get() != NULL
);
771 std::vector
<uint8
> value(params
->value
.begin(), params
->value
.end());
772 event_router
->WriteDescriptorValue(
774 params
->descriptor_id
,
777 &BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback
,
779 base::Bind(&BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback
,
785 void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() {
786 results_
= apibtle::WriteDescriptorValue::Results::Create();
790 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback(
791 BluetoothLowEnergyEventRouter::Status status
) {
792 SetError(StatusToString(status
));
796 } // namespace core_api
797 } // namespace extensions