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 <GlobalContextManager.h>
19 #include <PlatformException.h>
21 #include "BluetoothServiceHandler.h"
22 #include "BluetoothAdapter.h"
23 #include "BluetoothCallbackUtil.h"
24 #include "plugin_config.h"
27 #include <TimeTracer.h>
29 using namespace DeviceAPI::Common;
34 BluetoothServiceHandler::BluetoothServiceHandler(std::string uuid, std::string name, int registeredSocket)
38 mRegisteredSocket = registeredSocket;
43 BluetoothServiceHandler::~BluetoothServiceHandler()
46 BluetoothAdapter::getInstance()->unregisterUUID(mUUID);
47 if(bt_socket_destroy_rfcomm(mRegisteredSocket) != BT_ERROR_NONE) {
48 LoggerW("Already destroyed");
53 bool BluetoothServiceHandler::setOnConnect(JSContextRef context, JSObjectRef onConnect)
55 MultiCallbackUserDataPtr callback(
56 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
58 LoggerW("Can't create MultiCallbackUserData");
61 callback->setCallback("onconnect", onConnect);
62 mOnConnect = callback;
64 return mLocalProperty.setProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT, onConnect);
67 std::string BluetoothServiceHandler::getUUID() const
72 std::string BluetoothServiceHandler::getName() const
77 int BluetoothServiceHandler::getRegisteredSocket() const
79 return mRegisteredSocket;
82 MultiCallbackUserDataPtr BluetoothServiceHandler::getOnConnect() const
87 JSValueRef BluetoothServiceHandler::getOnConnect(JSContextRef context)
89 JSValueRef onConnect = mLocalProperty.getProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT);
90 if(onConnect == NULL) {
91 LoggerD("onconnect is null");
92 return JSValueMakeNull(context);
98 void BluetoothServiceHandler::setConnectionState(bool isConnected)
100 mIsConnected = isConnected;
103 bool BluetoothServiceHandler::getConnectionState() const
108 void BluetoothServiceHandler::unregister(MultiCallbackUserDataPtr userData)
110 if(BluetoothAdapter::getInstance()->getPowered() == true) {
111 TIME_TRACER_ITEM_BEGIN("unregister::bt_socket_destroy_rfcomm", 1);
112 if(bt_socket_destroy_rfcomm(mRegisteredSocket) == BT_ERROR_NONE) {
113 TIME_TRACER_ITEM_END("unregister::bt_socket_destroy_rfcomm", 1);
114 mIsRegistered = false;
115 BluetoothAdapter::getInstance()->unregisterUUID(mUUID);
116 BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData);
119 UnknownException *error = new UnknownException("Unknown exception");
120 BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error);
122 } else { // Not enabled
123 ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off");
124 BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error);