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()
47 BluetoothAdapter::getInstance()->unregisterUUID(mUUID);
48 if(bt_socket_destroy_rfcomm(mRegisteredSocket) != BT_ERROR_NONE) {
49 LoggerW("Already destroyed");
54 bool BluetoothServiceHandler::setOnConnect(JSContextRef context, JSObjectRef onConnect)
58 MultiCallbackUserDataPtr callback(
59 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
61 LoggerW("Can't create MultiCallbackUserData");
64 callback->setCallback("onconnect", onConnect);
65 mOnConnect = callback;
67 return mLocalProperty.setProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT, onConnect);
70 std::string BluetoothServiceHandler::getUUID() const
75 std::string BluetoothServiceHandler::getName() const
80 int BluetoothServiceHandler::getRegisteredSocket() const
82 return mRegisteredSocket;
85 MultiCallbackUserDataPtr BluetoothServiceHandler::getOnConnect() const
90 JSValueRef BluetoothServiceHandler::getOnConnect(JSContextRef context)
94 JSValueRef onConnect = mLocalProperty.getProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT);
95 if(onConnect == NULL) {
96 LoggerD("onconnect is null");
97 return JSValueMakeNull(context);
103 void BluetoothServiceHandler::setConnectionState(bool isConnected)
105 mIsConnected = isConnected;
108 bool BluetoothServiceHandler::getConnectionState()
113 void BluetoothServiceHandler::unregister(MultiCallbackUserDataPtr userData)
117 if(BluetoothAdapter::getInstance()->getPowered() == true) {
118 if(bt_socket_destroy_rfcomm(mRegisteredSocket) == BT_ERROR_NONE) {
119 mIsRegistered = false;
120 BluetoothAdapter::getInstance()->unregisterUUID(mUUID);
121 BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData);
124 UnknownException *error = new UnknownException("Unknown exception");
125 BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error);
127 } else { // Not enabled
128 ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off");
129 BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error);