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 <system_info.h>
30 #include <TimeTracer.h>
33 using namespace WrtDeviceApis::Commons;
34 using namespace DeviceAPI::Common;
39 JSClassDefinition JSBluetoothAdapter::m_classInfo = {
41 kJSClassAttributeNone,
44 m_property, //StaticValues
45 m_function, //StaticFunctions
46 initialize, //Initialize
51 NULL, //DeleteProperty,
52 NULL, //GetPropertyNames,
53 NULL, //CallAsFunction,
54 NULL, //CallAsConstructor,
59 JSStaticValue JSBluetoothAdapter::m_property[] = {
60 { BLUETOOTH_ADAPTER_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
61 { BLUETOOTH_ADAPTER_POWERED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_ADAPTER_VISIBLE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63 { BLUETOOTH_ADAPTER_ADDRESS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
67 JSStaticFunction JSBluetoothAdapter::m_function[] = {
68 { BLUETOOTH_ADAPTER_API_SET_NAME, setName, kJSPropertyAttributeNone },
69 { BLUETOOTH_ADAPTER_API_SET_POWERED, setPowered, kJSPropertyAttributeNone },
70 { BLUETOOTH_ADAPTER_API_SET_VISIBLE, setVisible, kJSPropertyAttributeNone },
71 { BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES, discoverDevices, kJSPropertyAttributeNone },
72 { BLUETOOTH_ADAPTER_API_STOP_DISCOVERY, stopDiscovery, kJSPropertyAttributeNone },
73 { BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES, getKnownDevices, kJSPropertyAttributeNone },
74 { BLUETOOTH_ADAPTER_API_GET_DEVICE, getDevice, kJSPropertyAttributeNone },
75 { BLUETOOTH_ADAPTER_API_CREATE_BONDING, createBonding, kJSPropertyAttributeNone },
76 { BLUETOOTH_ADAPTER_API_DESTROY_BONDING, destroyBonding, kJSPropertyAttributeNone },
77 { BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID, registerRFCOMMServiceByUUID, kJSPropertyAttributeNone },
78 { BLUETOOTH_ADAPTER_API_GET_BLUETOOTH_PROFILE_HANDLER, getBluetoothProfileHandler, kJSPropertyAttributeNone },
79 { BLUETOOTH_ADAPTER_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
80 { BLUETOOTH_ADAPTER_API_UNSET_CHANGE_LISTENER, unsetChangeListener, kJSPropertyAttributeNone },
84 JSClassRef JSBluetoothAdapter::m_jsClassRef = JSClassCreate(JSBluetoothAdapter::getClassInfo());
86 const JSClassRef JSBluetoothAdapter::getClassRef()
89 m_jsClassRef = JSClassCreate(&m_classInfo);
94 const JSClassDefinition* JSBluetoothAdapter::getClassInfo()
99 JSObjectRef JSBluetoothAdapter::createJSObject(JSContextRef context)
101 return JSObjectMake(context, getClassRef(), NULL);
104 void JSBluetoothAdapter::initialize(JSContextRef context, JSObjectRef object)
110 void JSBluetoothAdapter::finalize(JSObjectRef object)
115 JSValueRef JSBluetoothAdapter::getProperty(JSContextRef context,
117 JSStringRef propertyName,
118 JSValueRef* exception)
121 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_NAME)) {
122 std::string name = BluetoothAdapter::getInstance()->getName();
123 return JSUtil::toJSValueRef(context, name);
125 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_POWERED)) {
126 return JSUtil::toJSValueRef(context, BluetoothAdapter::getInstance()->getPowered());
128 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_VISIBLE)) {
129 return JSUtil::toJSValueRef(context, BluetoothAdapter::getInstance()->getVisible());
131 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
132 std::string address = BluetoothAdapter::getInstance()->getAddress();
133 return JSUtil::toJSValueRef(context, address);
135 } catch (const BasePlatformException &err) {
136 LoggerW("Getting property is failed" << err.getMessage().c_str());
142 JSValueRef JSBluetoothAdapter::setName(JSContextRef context,
144 JSObjectRef thisObject,
145 size_t argumentCount,
146 const JSValueRef arguments[],
147 JSValueRef* exception)
149 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
152 TIME_TRACER_ITEM_BEGIN("setName::ACE", 1);
153 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_NAME);
154 TIME_TRACER_ITEM_END("setName::ACE", 1);
155 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
158 ArgumentValidator validator(context, argumentCount, arguments);
159 std::string name = validator.toString(0); // name
160 JSObjectRef successCallback = validator.toFunction(1, true); // successCallback
161 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
164 MultiCallbackUserDataPtr callback(
165 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
167 LoggerW("Can't create MultiCallbackUserData");
170 callback->setCallback("success", successCallback);
171 callback->setCallback("error", errorCallback);
174 BluetoothAdapter::getInstance()->setName(name, callback);
175 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
177 return JSValueMakeUndefined(context);
178 } catch (const BasePlatformException &err) {
179 return JSWebAPIErrorFactory::postException(context, exception, err);
181 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
182 return JSWebAPIErrorFactory::postException(context, exception, err);
186 JSValueRef JSBluetoothAdapter::setPowered(JSContextRef context,
188 JSObjectRef thisObject,
189 size_t argumentCount,
190 const JSValueRef arguments[],
191 JSValueRef* exception)
195 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
198 TIME_TRACER_ITEM_BEGIN("setPowered::ACE", 1);
199 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_POWERED);
200 TIME_TRACER_ITEM_END("setPowered::ACE", 1);
201 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
204 // Check whether BT is supported or not
205 bool supported = false;
206 if(system_info_get_platform_bool("tizen.org/feature/network.bluetooth", &supported) != SYSTEM_INFO_ERROR_NONE) {
207 LoggerW("Can't check BT is supported or not");
210 if(supported == false) {
211 LoggerW("BT is not supported");
212 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
215 LoggerD("BT is supported");
218 // Validate arguments
219 ArgumentValidator validator(context, argumentCount, arguments);
220 bool state = validator.toBool(0); // state
221 JSObjectRef successCallback = validator.toFunction(1, true); // successCallback
222 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
225 MultiCallbackUserDataPtr callback(
226 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
228 LoggerW("Can't create MultiCallbackUserData");
231 callback->setCallback("success", successCallback);
232 callback->setCallback("error", errorCallback);
235 BluetoothAdapter::getInstance()->setPowered(state, callback);
236 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
238 return JSValueMakeUndefined(context);
239 } catch (const BasePlatformException &err) {
240 return JSWebAPIErrorFactory::postException(context, exception, err);
242 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setPowered().");
243 return JSWebAPIErrorFactory::postException(context, exception, err);
247 JSValueRef JSBluetoothAdapter::setVisible(JSContextRef context,
249 JSObjectRef thisObject,
250 size_t argumentCount,
251 const JSValueRef arguments[],
252 JSValueRef* exception)
254 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
257 TIME_TRACER_ITEM_BEGIN("setVisible::ACE", 1);
258 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_VISIBLE);
259 TIME_TRACER_ITEM_END("setVisible::ACE", 1);
260 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
263 // Validate arguments
264 ArgumentValidator validator(context, argumentCount, arguments);
265 bool mode = validator.toBool(0); // mode
266 JSObjectRef successCallback = validator.toFunction(1, true); // successCallback
267 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
268 unsigned long timeout = validator.toULong(3, true, 180); // timeout
273 MultiCallbackUserDataPtr callback(
274 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
276 LoggerW("Can't create MultiCallbackUserData");
279 callback->setCallback("success", successCallback);
280 callback->setCallback("error", errorCallback);
283 BluetoothAdapter::getInstance()->setVisible(mode, timeout, callback);
284 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
286 return JSValueMakeUndefined(context);
287 } catch (const BasePlatformException &err) {
288 return JSWebAPIErrorFactory::postException(context, exception, err);
290 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setVisible().");
291 return JSWebAPIErrorFactory::postException(context, exception, err);
295 JSValueRef JSBluetoothAdapter::discoverDevices(JSContextRef context,
297 JSObjectRef thisObject,
298 size_t argumentCount,
299 const JSValueRef arguments[],
300 JSValueRef* exception)
302 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
305 TIME_TRACER_ITEM_BEGIN("discoverDevices::ACE", 1);
306 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES);
307 TIME_TRACER_ITEM_END("discoverDevices::ACE", 1);
308 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
311 TIME_TRACER_ITEM_BEGIN("discoverDevices::parameter", 1);
313 // Validate arguments
314 ArgumentValidator validator(context, argumentCount, arguments);
317 JSObjectRef successCallback = validator.toCallbackObject(0, false, "onstarted", "ondevicefound", "ondevicedisappeared", "onfinished", NULL);
320 JSObjectRef errorCallback = validator.toFunction(1, true);
322 MultiCallbackUserDataPtr callback(
323 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
325 LoggerW("Can't create MultiMultiCallbackUserData");
329 JSValueRef onstartedValue = JSUtil::getProperty(context , successCallback, "onstarted");
330 if(!JSValueIsUndefined(context, onstartedValue)) {
331 LoggerD("There is a onstarted()");
332 callback->setCallback("onstarted", JSUtil::JSValueToObject(context, onstartedValue));
336 JSValueRef ondevicefoundValue = JSUtil::getProperty(context , successCallback, "ondevicefound");
337 if(!JSValueIsUndefined(context, ondevicefoundValue)) {
338 LoggerD("There is a ondevicefound()");
339 callback->setCallback("ondevicefound", JSUtil::JSValueToObject(context, ondevicefoundValue));
342 // ondevicedisappeared
343 JSValueRef ondevicedisappearedValue = JSUtil::getProperty(context , successCallback, "ondevicedisappeared");
344 if(!JSValueIsUndefined(context, ondevicedisappearedValue)) {
345 LoggerD("There is a ondevicedisappeared()");
346 callback->setCallback("ondevicedisappeared", JSUtil::JSValueToObject(context, ondevicedisappearedValue));
350 JSValueRef onfinishedValue = JSUtil::getProperty(context , successCallback, "onfinished");
351 if(!JSValueIsUndefined(context, onfinishedValue)) {
352 LoggerD("There is a onfinished()");
353 callback->setCallback("onfinished", JSUtil::JSValueToObject(context, onfinishedValue));
356 callback->setCallback("error", errorCallback);
358 TIME_TRACER_ITEM_END("discoverDevices::parameter", 1);
361 BluetoothAdapter::getInstance()->discoverDevices(callback);
362 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
364 return JSValueMakeUndefined(context);
365 } catch (const BasePlatformException &err) {
366 return JSWebAPIErrorFactory::postException(context, exception, err);
368 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.discoverDevices().");
369 return JSWebAPIErrorFactory::postException(context, exception, err);
373 JSValueRef JSBluetoothAdapter::stopDiscovery(JSContextRef context,
375 JSObjectRef thisObject,
376 size_t argumentCount,
377 const JSValueRef arguments[],
378 JSValueRef* exception)
380 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
383 TIME_TRACER_ITEM_BEGIN("stopDiscovery::ACE", 1);
384 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_STOP_DISCOVERY);
385 TIME_TRACER_ITEM_END("stopDiscovery::ACE", 1);
386 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
389 // Validate arguments
390 ArgumentValidator validator(context, argumentCount, arguments);
391 JSObjectRef successCallback = validator.toFunction(0, true); // successCallback
392 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
395 MultiCallbackUserDataPtr callback(
396 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
398 LoggerW("Can't create MultiCallbackUserData");
401 callback->setCallback("success", successCallback);
402 callback->setCallback("error", errorCallback);
405 BluetoothAdapter::getInstance()->stopDiscovery(callback);
406 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
408 return JSValueMakeUndefined(context);
409 } catch (const BasePlatformException &err) {
410 return JSWebAPIErrorFactory::postException(context, exception, err);
412 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.stopDiscovery().");
413 return JSWebAPIErrorFactory::postException(context, exception, err);
417 JSValueRef JSBluetoothAdapter::getKnownDevices(JSContextRef context,
419 JSObjectRef thisObject,
420 size_t argumentCount,
421 const JSValueRef arguments[],
422 JSValueRef* exception)
424 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
427 TIME_TRACER_ITEM_BEGIN("getKnownDevices::ACE", 1);
428 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES);
429 TIME_TRACER_ITEM_END("getKnownDevices::ACE", 1);
430 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
433 // Validate arguments
434 ArgumentValidator validator(context, argumentCount, arguments);
435 JSObjectRef successCallback = validator.toFunction(0); // successCallback
436 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
439 MultiCallbackUserDataPtr callback(
440 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
442 LoggerW("Can't create MultiCallbackUserData");
445 callback->setCallback("success", successCallback);
446 callback->setCallback("error", errorCallback);
449 BluetoothAdapter::getInstance()->getKnownDevices(callback);
450 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
452 return JSValueMakeUndefined(context);
453 } catch (const BasePlatformException &err) {
454 return JSWebAPIErrorFactory::postException(context, exception, err);
456 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.getKnownDevices().");
457 return JSWebAPIErrorFactory::postException(context, exception, err);
461 JSValueRef JSBluetoothAdapter::getDevice(JSContextRef context,
463 JSObjectRef thisObject,
464 size_t argumentCount,
465 const JSValueRef arguments[],
466 JSValueRef* exception)
468 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
471 TIME_TRACER_ITEM_BEGIN("getDevice::ACE", 1);
472 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_GET_DEVICE);
473 TIME_TRACER_ITEM_END("getDevice::ACE", 1);
474 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
477 // Validate arguments
478 ArgumentValidator validator(context, argumentCount, arguments);
479 std::string address = validator.toString(0); // address
480 JSObjectRef successCallback = validator.toFunction(1); // successCallback
481 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
484 MultiCallbackUserDataPtr callback(
485 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
487 LoggerW("Can't create MultiCallbackUserData");
490 callback->setCallback("success", successCallback);
491 callback->setCallback("error", errorCallback);
494 BluetoothAdapter::getInstance()->getDevice(address, callback);
495 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
497 return JSValueMakeUndefined(context);
498 } catch (const BasePlatformException &err) {
499 return JSWebAPIErrorFactory::postException(context, exception, err);
501 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.getDevice().");
502 return JSWebAPIErrorFactory::postException(context, exception, err);
506 JSValueRef JSBluetoothAdapter::createBonding(JSContextRef context,
508 JSObjectRef thisObject,
509 size_t argumentCount,
510 const JSValueRef arguments[],
511 JSValueRef* exception)
513 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
516 TIME_TRACER_ITEM_BEGIN("createBonding::ACE", 1);
517 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_CREATE_BONDING);
518 TIME_TRACER_ITEM_END("createBonding::ACE", 1);
519 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
522 // Validate arguments
523 ArgumentValidator validator(context, argumentCount, arguments);
524 std::string address = validator.toString(0); // address
525 JSObjectRef successCallback = validator.toFunction(1); // successCallback
526 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
529 MultiCallbackUserDataPtr callback(
530 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
532 LoggerW("Can't create MultiCallbackUserData");
535 callback->setCallback("success", successCallback);
536 callback->setCallback("error", errorCallback);
539 BluetoothAdapter::getInstance()->createBonding(address, callback);
540 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
542 return JSValueMakeUndefined(context);
543 } catch (const BasePlatformException &err) {
544 return JSWebAPIErrorFactory::postException(context, exception, err);
546 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.createBonding().");
547 return JSWebAPIErrorFactory::postException(context, exception, err);
551 JSValueRef JSBluetoothAdapter::destroyBonding(JSContextRef context,
553 JSObjectRef thisObject,
554 size_t argumentCount,
555 const JSValueRef arguments[],
556 JSValueRef* exception)
558 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
561 TIME_TRACER_ITEM_BEGIN("destroyBonding::ACE", 1);
562 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_DESTROY_BONDING);
563 TIME_TRACER_ITEM_END("destroyBonding::ACE", 1);
564 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
567 // Validate arguments
568 ArgumentValidator validator(context, argumentCount, arguments);
569 std::string address = validator.toString(0); // address
570 JSObjectRef successCallback = validator.toFunction(1, true); // successCallback
571 JSObjectRef errorCallback = validator.toFunction(2, true); // errorCallback
574 MultiCallbackUserDataPtr callback(
575 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
577 LoggerW("Can't create MultiCallbackUserData");
580 callback->setCallback("success", successCallback);
581 callback->setCallback("error", errorCallback);
584 BluetoothAdapter::getInstance()->destroyBonding(address, callback);
585 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
587 return JSValueMakeUndefined(context);
588 } catch (const BasePlatformException &err) {
589 return JSWebAPIErrorFactory::postException(context, exception, err);
591 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.destroyBonding().");
592 return JSWebAPIErrorFactory::postException(context, exception, err);
596 JSValueRef JSBluetoothAdapter::registerRFCOMMServiceByUUID(JSContextRef context,
598 JSObjectRef thisObject,
599 size_t argumentCount,
600 const JSValueRef arguments[],
601 JSValueRef* exception)
603 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
606 TIME_TRACER_ITEM_BEGIN("registerRFCOMMServiceByUUID::ACE", 1);
607 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID);
608 TIME_TRACER_ITEM_END("registerRFCOMMServiceByUUID::ACE", 1);
609 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
612 // Validate arguments
613 ArgumentValidator validator(context, argumentCount, arguments);
614 std::string uuid = validator.toString(0); // uuid
615 std::string name = validator.toString(1); // name
616 JSObjectRef successCallback = validator.toFunction(2); // successCallback
617 JSObjectRef errorCallback = validator.toFunction(3, true); // errorCallback
620 MultiCallbackUserDataPtr callback(
621 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
623 LoggerW("Can't create MultiCallbackUserData");
626 callback->setCallback("success", successCallback);
627 callback->setCallback("error", errorCallback);
630 BluetoothAdapter::getInstance()->registerRFCOMMServiceByUUID(uuid, name, callback);
631 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
633 return JSValueMakeUndefined(context);
634 } catch (const BasePlatformException &err) {
635 return JSWebAPIErrorFactory::postException(context, exception, err);
637 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
638 return JSWebAPIErrorFactory::postException(context, exception, err);
642 JSValueRef JSBluetoothAdapter::getBluetoothProfileHandler(JSContextRef context,
644 JSObjectRef thisObject,
645 size_t argumentCount,
646 const JSValueRef arguments[],
647 JSValueRef* exception)
650 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
653 // Validate arguments
654 ArgumentValidator validator(context, argumentCount, arguments);
655 std::string type = validator.toString(0); // profileType
658 JSObjectRef profileHandler;
659 bool isCorrectParameter = false;
660 if(type.compare("HEALTH") == 0) {
661 isCorrectParameter = true;
662 profileHandler = JSBluetoothHealthProfileHandler::createJSObject(context);
664 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
666 if(!isCorrectParameter) {
667 throw TypeMismatchException("Type Mismatch");
670 return profileHandler;
671 } catch (const BasePlatformException &err) {
672 return JSWebAPIErrorFactory::postException(context, exception, err);
674 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
675 return JSWebAPIErrorFactory::postException(context, exception, err);
679 JSValueRef JSBluetoothAdapter::setChangeListener(JSContextRef context,
681 JSObjectRef thisObject,
682 size_t argumentCount,
683 const JSValueRef arguments[],
684 JSValueRef* exception)
687 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
690 // Validate arguments
691 ArgumentValidator validator(context, argumentCount, arguments);
692 JSObjectRef changeCallbackObj = validator.toCallbackObject(0, false, "onstatechanged", "onnamechanged", "onvisibilitychanged", NULL);
694 MultiCallbackUserDataPtr callback(
695 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
697 LoggerW("Can't create MultiMultiCallbackUserData");
701 JSValueRef onstatechangedValue = JSUtil::getProperty(context , changeCallbackObj, "onstatechanged");
702 if(!JSValueIsUndefined(context, onstatechangedValue)) {
703 LoggerD("There is a onstatechanged()");
704 callback->setCallback("onstatechanged", JSUtil::JSValueToObject(context, onstatechangedValue));
708 JSValueRef onnamechangedValue = JSUtil::getProperty(context , changeCallbackObj, "onnamechanged");
709 if(!JSValueIsUndefined(context, onnamechangedValue)) {
710 LoggerD("There is a onnamechanged()");
711 callback->setCallback("onnamechanged", JSUtil::JSValueToObject(context, onnamechangedValue));
714 // onvisibilitychanged
715 JSValueRef onvisibilitychangedValue = JSUtil::getProperty(context , changeCallbackObj, "onvisibilitychanged");
716 if(!JSValueIsUndefined(context, onvisibilitychangedValue)) {
717 LoggerD("There is a onvisibilitychanged()");
718 callback->setCallback("onvisibilitychanged", JSUtil::JSValueToObject(context, onvisibilitychangedValue));
723 BluetoothAdapter::getInstance()->setChangeListener(callback);
724 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
726 return JSValueMakeUndefined(context);
727 } catch (const BasePlatformException &err) {
728 return JSWebAPIErrorFactory::postException(context, exception, err);
730 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
731 return JSWebAPIErrorFactory::postException(context, exception, err);
735 JSValueRef JSBluetoothAdapter::unsetChangeListener(JSContextRef context,
737 JSObjectRef thisObject,
738 size_t argumentCount,
739 const JSValueRef arguments[],
740 JSValueRef* exception)
743 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
747 BluetoothAdapter::getInstance()->unsetChangeListener();
748 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
750 return JSValueMakeUndefined(context);
751 } catch (const BasePlatformException &err) {
752 return JSWebAPIErrorFactory::postException(context, exception, err);
754 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
755 return JSWebAPIErrorFactory::postException(context, exception, err);