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"
28 using namespace DeviceAPI::Common;
33 BluetoothServiceHandler::BluetoothServiceHandler(std::string uuid, std::string name, int registeredSocket)
37 mRegisteredSocket = registeredSocket;
42 BluetoothServiceHandler::~BluetoothServiceHandler()
45 BluetoothAdapter::getInstance()->unregisterUUID(mUUID);
46 if(bt_socket_destroy_rfcomm(mRegisteredSocket) != BT_ERROR_NONE) {
47 LoggerW("Already destroyed");
52 bool BluetoothServiceHandler::setOnConnect(JSContextRef context, JSObjectRef onConnect)
54 MultiCallbackUserDataPtr callback(
55 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
57 LoggerW("Can't create MultiCallbackUserData");
60 callback->setCallback("onconnect", onConnect);
61 mOnConnect = callback;
63 return mLocalProperty.setProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT, onConnect);
66 std::string BluetoothServiceHandler::getUUID() const
71 std::string BluetoothServiceHandler::getName() const
76 int BluetoothServiceHandler::getRegisteredSocket() const
78 return mRegisteredSocket;
81 MultiCallbackUserDataPtr BluetoothServiceHandler::getOnConnect() const
86 JSValueRef BluetoothServiceHandler::getOnConnect(JSContextRef context)
88 JSValueRef onConnect = mLocalProperty.getProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT);
89 if(onConnect == NULL) {
90 LoggerD("onconnect is null");
91 return JSValueMakeNull(context);
97 void BluetoothServiceHandler::setConnectionState(bool isConnected)
99 mIsConnected = isConnected;
102 bool BluetoothServiceHandler::getConnectionState()
107 void BluetoothServiceHandler::unregister(MultiCallbackUserDataPtr userData)
109 if(BluetoothAdapter::getInstance()->getPowered() == true) {
110 if(bt_socket_destroy_rfcomm(mRegisteredSocket) == BT_ERROR_NONE) {
111 mIsRegistered = false;
112 BluetoothAdapter::getInstance()->unregisterUUID(mUUID);
113 BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData);
116 UnknownException *error = new UnknownException("Unknown exception");
117 BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error);
119 } else { // Not enabled
120 ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off");
121 BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error);