2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <SecurityExceptions.h>
21 #include <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
24 #include "plugin_config.h"
25 #include "JSBluetoothAdapter.h"
26 #include "BluetoothAdapter.h"
27 #include "JSBluetoothHealthProfileHandler.h"
29 #include <TimeTracer.h>
32 using namespace WrtDeviceApis::Commons;
33 using namespace DeviceAPI::Common;
38 JSClassDefinition JSBluetoothAdapter::m_classInfo = {
40 kJSClassAttributeNone,
43 m_property, //StaticValues
44 m_function, //StaticFunctions
45 initialize, //Initialize
50 NULL, //DeleteProperty,
51 NULL, //GetPropertyNames,
52 NULL, //CallAsFunction,
53 NULL, //CallAsConstructor,
58 JSStaticValue JSBluetoothAdapter::m_property[] = {
59 { BLUETOOTH_ADAPTER_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
60 { BLUETOOTH_ADAPTER_POWERED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
61 { BLUETOOTH_ADAPTER_VISIBLE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_ADAPTER_ADDRESS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
66 JSStaticFunction JSBluetoothAdapter::m_function[] = {
67 { BLUETOOTH_ADAPTER_API_SET_NAME, setName, kJSPropertyAttributeNone },
68 { BLUETOOTH_ADAPTER_API_SET_POWERED, setPowered, kJSPropertyAttributeNone },
69 { BLUETOOTH_ADAPTER_API_SET_VISIBLE, setVisible, kJSPropertyAttributeNone },
70 { BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES, discoverDevices, kJSPropertyAttributeNone },
71 { BLUETOOTH_ADAPTER_API_STOP_DISCOVERY, stopDiscovery, kJSPropertyAttributeNone },
72 { BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES, getKnownDevices, kJSPropertyAttributeNone },
73 { BLUETOOTH_ADAPTER_API_GET_DEVICE, getDevice, kJSPropertyAttributeNone },
74 { BLUETOOTH_ADAPTER_API_CREATE_BONDING, createBonding, kJSPropertyAttributeNone },
75 { BLUETOOTH_ADAPTER_API_DESTROY_BONDING, destroyBonding, kJSPropertyAttributeNone },
76 { BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID, registerRFCOMMServiceByUUID, kJSPropertyAttributeNone },
77 { BLUETOOTH_ADAPTER_API_GET_BLUETOOTH_PROFILE_HANDLER, getBluetoothProfileHandler, kJSPropertyAttributeNone },
78 { BLUETOOTH_ADAPTER_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
79 { BLUETOOTH_ADAPTER_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone },
83 JSClassRef JSBluetoothAdapter::m_jsClassRef = JSClassCreate(JSBluetoothAdapter::getClassInfo());
85 const JSClassRef JSBluetoothAdapter::getClassRef()
88 m_jsClassRef = JSClassCreate(&m_classInfo);
93 const JSClassDefinition* JSBluetoothAdapter::getClassInfo()
98 JSObjectRef JSBluetoothAdapter::createJSObject(JSContextRef context)
100 return JSObjectMake(context, getClassRef(), NULL);
103 void JSBluetoothAdapter::initialize(JSContextRef context, JSObjectRef object)
109 void JSBluetoothAdapter::finalize(JSObjectRef object)
114 JSValueRef JSBluetoothAdapter::getProperty(JSContextRef context,
116 JSStringRef propertyName,
117 JSValueRef* exception)
120 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_NAME)) {
121 std::string name = BluetoothAdapter::getInstance()->getName();
122 return JSUtil::toJSValueRef(context, name);
124 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_POWERED)) {
125 return JSUtil::toJSValueRef(context, BluetoothAdapter::getInstance()->getPowered());
127 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_VISIBLE)) {
128 return JSUtil::toJSValueRef(context, BluetoothAdapter::getInstance()->getVisible());
130 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
131 std::string address = BluetoothAdapter::getInstance()->getAddress();
132 return JSUtil::toJSValueRef(context, address);
134 } catch (const BasePlatformException &err) {
135 LoggerW("Getting property is failed" << err.getMessage().c_str());
141 JSValueRef JSBluetoothAdapter::setName(JSContextRef context,
143 JSObjectRef thisObject,
144 size_t argumentCount,
145 const JSValueRef arguments[],
146 JSValueRef* exception)
148 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
151 TIME_TRACER_ITEM_BEGIN("setName::ACE", 1);
152 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_NAME);
153 TIME_TRACER_ITEM_END("setName::ACE", 1);
154 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
157 ArgumentValidator validator(context, argumentCount, arguments);
158 std::string name = validator.toString(0); // name
159 JSObjectRef successCallback = validator.toFunction(1, true); // successCallback
160 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
163 MultiCallbackUserDataPtr callback(
164 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
166 LoggerW("Can't create MultiCallbackUserData");
169 callback->setCallback("success", successCallback);
170 callback->setCallback("error", errorCallback);
173 BluetoothAdapter::getInstance()->setName(name, callback);
174 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
176 return JSValueMakeUndefined(context);
177 } catch (const BasePlatformException &err) {
178 return JSWebAPIErrorFactory::postException(context, exception, err);
180 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
181 return JSWebAPIErrorFactory::postException(context, exception, err);
185 JSValueRef JSBluetoothAdapter::setPowered(JSContextRef context,
187 JSObjectRef thisObject,
188 size_t argumentCount,
189 const JSValueRef arguments[],
190 JSValueRef* exception)
192 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
195 TIME_TRACER_ITEM_BEGIN("setPowered::ACE", 1);
196 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_POWERED);
197 TIME_TRACER_ITEM_END("setPowered::ACE", 1);
198 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
201 // Validate arguments
202 ArgumentValidator validator(context, argumentCount, arguments);
203 bool state = validator.toBool(0); // state
204 JSObjectRef successCallback = validator.toFunction(1, true); // successCallback
205 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
208 MultiCallbackUserDataPtr callback(
209 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
211 LoggerW("Can't create MultiCallbackUserData");
214 callback->setCallback("success", successCallback);
215 callback->setCallback("error", errorCallback);
218 BluetoothAdapter::getInstance()->setPowered(state, callback);
219 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
221 return JSValueMakeUndefined(context);
222 } catch (const BasePlatformException &err) {
223 return JSWebAPIErrorFactory::postException(context, exception, err);
225 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setPowered().");
226 return JSWebAPIErrorFactory::postException(context, exception, err);
230 JSValueRef JSBluetoothAdapter::setVisible(JSContextRef context,
232 JSObjectRef thisObject,
233 size_t argumentCount,
234 const JSValueRef arguments[],
235 JSValueRef* exception)
237 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
240 TIME_TRACER_ITEM_BEGIN("setVisible::ACE", 1);
241 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_VISIBLE);
242 TIME_TRACER_ITEM_END("setVisible::ACE", 1);
243 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
246 // Validate arguments
247 ArgumentValidator validator(context, argumentCount, arguments);
248 bool mode = validator.toBool(0); // mode
249 JSObjectRef successCallback = validator.toFunction(1, true); // successCallback
250 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
251 unsigned long timeout = validator.toULong(3, true); // timeout
256 MultiCallbackUserDataPtr callback(
257 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
259 LoggerW("Can't create MultiCallbackUserData");
262 callback->setCallback("success", successCallback);
263 callback->setCallback("error", errorCallback);
266 BluetoothAdapter::getInstance()->setVisible(mode, timeout, callback);
267 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
269 return JSValueMakeUndefined(context);
270 } catch (const BasePlatformException &err) {
271 return JSWebAPIErrorFactory::postException(context, exception, err);
273 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setVisible().");
274 return JSWebAPIErrorFactory::postException(context, exception, err);
278 JSValueRef JSBluetoothAdapter::discoverDevices(JSContextRef context,
280 JSObjectRef thisObject,
281 size_t argumentCount,
282 const JSValueRef arguments[],
283 JSValueRef* exception)
285 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
288 TIME_TRACER_ITEM_BEGIN("discoverDevices::ACE", 1);
289 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES);
290 TIME_TRACER_ITEM_END("discoverDevices::ACE", 1);
291 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
294 TIME_TRACER_ITEM_BEGIN("discoverDevices::parameter", 1);
296 // Validate arguments
297 ArgumentValidator validator(context, argumentCount, arguments);
300 JSObjectRef successCallback = validator.toCallbackObject(0, false, "onstarted", "ondevicefound", "ondevicedisappeared", "onfinished", NULL);
303 JSObjectRef errorCallback = validator.toFunction(1, true);
305 MultiCallbackUserDataPtr callback(
306 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
308 LoggerW("Can't create MultiMultiCallbackUserData");
312 JSValueRef onstartedValue = JSUtil::getProperty(context , successCallback, "onstarted");
313 if(!JSValueIsUndefined(context, onstartedValue)) {
314 LoggerD("There is a onstarted()");
315 callback->setCallback("onstarted", JSUtil::JSValueToObject(context, onstartedValue));
319 JSValueRef ondevicefoundValue = JSUtil::getProperty(context , successCallback, "ondevicefound");
320 if(!JSValueIsUndefined(context, ondevicefoundValue)) {
321 LoggerD("There is a ondevicefound()");
322 callback->setCallback("ondevicefound", JSUtil::JSValueToObject(context, ondevicefoundValue));
325 // ondevicedisappeared
326 JSValueRef ondevicedisappearedValue = JSUtil::getProperty(context , successCallback, "ondevicedisappeared");
327 if(!JSValueIsUndefined(context, ondevicedisappearedValue)) {
328 LoggerD("There is a ondevicedisappeared()");
329 callback->setCallback("ondevicedisappeared", JSUtil::JSValueToObject(context, ondevicedisappearedValue));
333 JSValueRef onfinishedValue = JSUtil::getProperty(context , successCallback, "onfinished");
334 if(!JSValueIsUndefined(context, onfinishedValue)) {
335 LoggerD("There is a onfinished()");
336 callback->setCallback("onfinished", JSUtil::JSValueToObject(context, onfinishedValue));
339 callback->setCallback("error", errorCallback);
341 TIME_TRACER_ITEM_END("discoverDevices::parameter", 1);
344 BluetoothAdapter::getInstance()->discoverDevices(callback);
345 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
347 return JSValueMakeUndefined(context);
348 } catch (const BasePlatformException &err) {
349 return JSWebAPIErrorFactory::postException(context, exception, err);
351 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.discoverDevices().");
352 return JSWebAPIErrorFactory::postException(context, exception, err);
356 JSValueRef JSBluetoothAdapter::stopDiscovery(JSContextRef context,
358 JSObjectRef thisObject,
359 size_t argumentCount,
360 const JSValueRef arguments[],
361 JSValueRef* exception)
363 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
366 TIME_TRACER_ITEM_BEGIN("stopDiscovery::ACE", 1);
367 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_STOP_DISCOVERY);
368 TIME_TRACER_ITEM_END("stopDiscovery::ACE", 1);
369 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
372 // Validate arguments
373 ArgumentValidator validator(context, argumentCount, arguments);
374 JSObjectRef successCallback = validator.toFunction(0, true); // successCallback
375 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
378 MultiCallbackUserDataPtr callback(
379 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
381 LoggerW("Can't create MultiCallbackUserData");
384 callback->setCallback("success", successCallback);
385 callback->setCallback("error", errorCallback);
388 BluetoothAdapter::getInstance()->stopDiscovery(callback);
389 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
391 return JSValueMakeUndefined(context);
392 } catch (const BasePlatformException &err) {
393 return JSWebAPIErrorFactory::postException(context, exception, err);
395 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.stopDiscovery().");
396 return JSWebAPIErrorFactory::postException(context, exception, err);
400 JSValueRef JSBluetoothAdapter::getKnownDevices(JSContextRef context,
402 JSObjectRef thisObject,
403 size_t argumentCount,
404 const JSValueRef arguments[],
405 JSValueRef* exception)
407 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
410 TIME_TRACER_ITEM_BEGIN("getKnownDevices::ACE", 1);
411 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES);
412 TIME_TRACER_ITEM_END("getKnownDevices::ACE", 1);
413 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
416 // Validate arguments
417 ArgumentValidator validator(context, argumentCount, arguments);
418 JSObjectRef successCallback = validator.toFunction(0); // successCallback
419 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
422 MultiCallbackUserDataPtr callback(
423 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
425 LoggerW("Can't create MultiCallbackUserData");
428 callback->setCallback("success", successCallback);
429 callback->setCallback("error", errorCallback);
432 BluetoothAdapter::getInstance()->getKnownDevices(callback);
433 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
435 return JSValueMakeUndefined(context);
436 } catch (const BasePlatformException &err) {
437 return JSWebAPIErrorFactory::postException(context, exception, err);
439 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.getKnownDevices().");
440 return JSWebAPIErrorFactory::postException(context, exception, err);
444 JSValueRef JSBluetoothAdapter::getDevice(JSContextRef context,
446 JSObjectRef thisObject,
447 size_t argumentCount,
448 const JSValueRef arguments[],
449 JSValueRef* exception)
451 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
454 TIME_TRACER_ITEM_BEGIN("getDevice::ACE", 1);
455 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_GET_DEVICE);
456 TIME_TRACER_ITEM_END("getDevice::ACE", 1);
457 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
460 // Validate arguments
461 ArgumentValidator validator(context, argumentCount, arguments);
462 std::string address = validator.toString(0); // address
463 JSObjectRef successCallback = validator.toFunction(1); // successCallback
464 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
467 MultiCallbackUserDataPtr callback(
468 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
470 LoggerW("Can't create MultiCallbackUserData");
473 callback->setCallback("success", successCallback);
474 callback->setCallback("error", errorCallback);
477 BluetoothAdapter::getInstance()->getDevice(address, callback);
478 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
480 return JSValueMakeUndefined(context);
481 } catch (const BasePlatformException &err) {
482 return JSWebAPIErrorFactory::postException(context, exception, err);
484 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.getDevice().");
485 return JSWebAPIErrorFactory::postException(context, exception, err);
489 JSValueRef JSBluetoothAdapter::createBonding(JSContextRef context,
491 JSObjectRef thisObject,
492 size_t argumentCount,
493 const JSValueRef arguments[],
494 JSValueRef* exception)
496 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
499 TIME_TRACER_ITEM_BEGIN("createBonding::ACE", 1);
500 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_CREATE_BONDING);
501 TIME_TRACER_ITEM_END("createBonding::ACE", 1);
502 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
505 // Validate arguments
506 ArgumentValidator validator(context, argumentCount, arguments);
507 std::string address = validator.toString(0); // address
508 JSObjectRef successCallback = validator.toFunction(1); // successCallback
509 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
512 MultiCallbackUserDataPtr callback(
513 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
515 LoggerW("Can't create MultiCallbackUserData");
518 callback->setCallback("success", successCallback);
519 callback->setCallback("error", errorCallback);
522 BluetoothAdapter::getInstance()->createBonding(address, callback);
523 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
525 return JSValueMakeUndefined(context);
526 } catch (const BasePlatformException &err) {
527 return JSWebAPIErrorFactory::postException(context, exception, err);
529 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.createBonding().");
530 return JSWebAPIErrorFactory::postException(context, exception, err);
534 JSValueRef JSBluetoothAdapter::destroyBonding(JSContextRef context,
536 JSObjectRef thisObject,
537 size_t argumentCount,
538 const JSValueRef arguments[],
539 JSValueRef* exception)
541 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
544 TIME_TRACER_ITEM_BEGIN("destroyBonding::ACE", 1);
545 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_DESTROY_BONDING);
546 TIME_TRACER_ITEM_END("destroyBonding::ACE", 1);
547 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
550 // Validate arguments
551 ArgumentValidator validator(context, argumentCount, arguments);
552 std::string address = validator.toString(0); // address
553 JSObjectRef successCallback = validator.toFunction(1, true); // successCallback
554 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
557 MultiCallbackUserDataPtr callback(
558 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
560 LoggerW("Can't create MultiCallbackUserData");
563 callback->setCallback("success", successCallback);
564 callback->setCallback("error", errorCallback);
567 BluetoothAdapter::getInstance()->destroyBonding(address, callback);
568 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
570 return JSValueMakeUndefined(context);
571 } catch (const BasePlatformException &err) {
572 return JSWebAPIErrorFactory::postException(context, exception, err);
574 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.destroyBonding().");
575 return JSWebAPIErrorFactory::postException(context, exception, err);
579 JSValueRef JSBluetoothAdapter::registerRFCOMMServiceByUUID(JSContextRef context,
581 JSObjectRef thisObject,
582 size_t argumentCount,
583 const JSValueRef arguments[],
584 JSValueRef* exception)
586 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
589 TIME_TRACER_ITEM_BEGIN("registerRFCOMMServiceByUUID::ACE", 1);
590 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID);
591 TIME_TRACER_ITEM_END("registerRFCOMMServiceByUUID::ACE", 1);
592 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
595 // Validate arguments
596 ArgumentValidator validator(context, argumentCount, arguments);
597 std::string uuid = validator.toString(0); // uuid
598 std::string name = validator.toString(1); // name
599 JSObjectRef successCallback = validator.toFunction(2); // successCallback
600 JSObjectRef errorCallback = validator.toFunction(3, true); // errorCallback
603 MultiCallbackUserDataPtr callback(
604 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
606 LoggerW("Can't create MultiCallbackUserData");
609 callback->setCallback("success", successCallback);
610 callback->setCallback("error", errorCallback);
613 BluetoothAdapter::getInstance()->registerRFCOMMServiceByUUID(uuid, name, callback);
614 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
616 return JSValueMakeUndefined(context);
617 } catch (const BasePlatformException &err) {
618 return JSWebAPIErrorFactory::postException(context, exception, err);
620 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
621 return JSWebAPIErrorFactory::postException(context, exception, err);
625 JSValueRef JSBluetoothAdapter::getBluetoothProfileHandler(JSContextRef context,
627 JSObjectRef thisObject,
628 size_t argumentCount,
629 const JSValueRef arguments[],
630 JSValueRef* exception)
633 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
636 // Validate arguments
637 ArgumentValidator validator(context, argumentCount, arguments);
638 std::string type = validator.toString(0); // profileType
641 JSObjectRef profileHandler;
642 bool isCorrectParameter = false;
643 if(type.compare("HEALTH") == 0) {
644 isCorrectParameter = true;
645 profileHandler = JSBluetoothHealthProfileHandler::createJSObject(context);
647 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
649 if(!isCorrectParameter) {
650 throw InvalidValuesException("Invalid Value");
653 return profileHandler;
654 } catch (const BasePlatformException &err) {
655 return JSWebAPIErrorFactory::postException(context, exception, err);
657 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
658 return JSWebAPIErrorFactory::postException(context, exception, err);
662 JSValueRef JSBluetoothAdapter::setChangeListener(JSContextRef context,
664 JSObjectRef thisObject,
665 size_t argumentCount,
666 const JSValueRef arguments[],
667 JSValueRef* exception)
670 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
673 // Validate arguments
674 ArgumentValidator validator(context, argumentCount, arguments);
675 JSObjectRef changeCallbackObj = validator.toCallbackObject(0, false, "onstatechanged", "onnamechanged", "onvisibilitychanged", NULL);
677 MultiCallbackUserDataPtr callback(
678 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
680 LoggerW("Can't create MultiMultiCallbackUserData");
684 JSValueRef onstatechangedValue = JSUtil::getProperty(context , changeCallbackObj, "onstatechanged");
685 if(!JSValueIsUndefined(context, onstatechangedValue)) {
686 LoggerD("There is a onstatechanged()");
687 callback->setCallback("onstatechanged", JSUtil::JSValueToObject(context, onstatechangedValue));
691 JSValueRef onnamechangedValue = JSUtil::getProperty(context , changeCallbackObj, "onnamechanged");
692 if(!JSValueIsUndefined(context, onnamechangedValue)) {
693 LoggerD("There is a onnamechanged()");
694 callback->setCallback("onnamechanged", JSUtil::JSValueToObject(context, onnamechangedValue));
697 // onvisibilitychanged
698 JSValueRef onvisibilitychangedValue = JSUtil::getProperty(context , changeCallbackObj, "onvisibilitychanged");
699 if(!JSValueIsUndefined(context, onvisibilitychangedValue)) {
700 LoggerD("There is a onvisibilitychanged()");
701 callback->setCallback("onvisibilitychanged", JSUtil::JSValueToObject(context, onvisibilitychangedValue));
706 BluetoothAdapter::getInstance()->setChangeListener(callback);
707 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
709 return JSValueMakeUndefined(context);
710 } catch (const BasePlatformException &err) {
711 return JSWebAPIErrorFactory::postException(context, exception, err);
713 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
714 return JSWebAPIErrorFactory::postException(context, exception, err);
718 JSValueRef JSBluetoothAdapter::unsetChangeListener(JSContextRef context,
720 JSObjectRef thisObject,
721 size_t argumentCount,
722 const JSValueRef arguments[],
723 JSValueRef* exception)
726 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
730 BluetoothAdapter::getInstance()->unsetChangeListener();
731 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
733 return JSValueMakeUndefined(context);
734 } catch (const BasePlatformException &err) {
735 return JSWebAPIErrorFactory::postException(context, exception, err);
737 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
738 return JSWebAPIErrorFactory::postException(context, exception, err);