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 <PlatformException.h>
21 #include "plugin_config.h"
22 #include "BluetoothDevice.h"
23 #include "BluetoothAdapter.h"
24 #include "JSBluetoothClass.h"
28 using namespace DeviceAPI::Common;
33 BluetoothDevice::BluetoothDevice(bt_adapter_device_discovery_info_s *discoveryInfo)
37 mName = std::string(discoveryInfo->remote_name);
38 mAddress = std::string(discoveryInfo->remote_address);
39 mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(discoveryInfo->bt_class));
40 for(int i = 0; i < discoveryInfo->service_count; i++) {
41 mUUIDs.push_back(std::string(discoveryInfo->service_uuid[i]));
46 BluetoothDevice::BluetoothDevice(bt_device_info_s *deviceInfo)
50 mName = std::string(deviceInfo->remote_name);
51 mAddress = std::string(deviceInfo->remote_address);
52 mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(deviceInfo->bt_class));
53 for(int i = 0; i < deviceInfo->service_count; i++) {
54 mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
59 BluetoothDevice::~BluetoothDevice()
62 BluetoothAdapter::getInstance()->removeConnReq(mAddress);
65 void BluetoothDevice::updateInfo(bt_device_info_s *deviceInfo)
69 mName = std::string(deviceInfo->remote_name);
71 for(int i = 0; i < deviceInfo->service_count; i++) {
72 mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
77 std::string BluetoothDevice::getName() const
83 std::string BluetoothDevice::getAddress() const
89 JSValueRef BluetoothDevice::getDeviceClass(JSContextRef context)
93 JSValueRef deviceClass = mLocalProperty.getProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS);
94 if(deviceClass == NULL) {
95 deviceClass = JSBluetoothClass::createJSObject(context, mDeviceClass);
96 mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, deviceClass);
101 return JSBluetoothClass::createJSObject(context, mDeviceClass);
104 bool BluetoothDevice::isBonded() const
109 bt_device_info_s *deviceInfo = NULL;
110 if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
111 ret = deviceInfo->is_bonded;
112 bt_adapter_free_device_info(deviceInfo);
118 bool BluetoothDevice::isTrusted() const
123 bt_device_info_s *deviceInfo = NULL;
124 if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
125 ret = deviceInfo->is_authorized;
126 bt_adapter_free_device_info(deviceInfo);
132 bool BluetoothDevice::isConnected() const
137 bt_device_info_s *deviceInfo = NULL;
138 if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
139 ret = deviceInfo->is_connected;
140 bt_adapter_free_device_info(deviceInfo);
146 JSValueRef BluetoothDevice::getUUIDs(JSContextRef context)
150 if(isUpdated == true) {
151 mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_UUIDS, JSUtil::toJSValueRef(context, mUUIDs));
155 return mLocalProperty.getProperty(context, BLUETOOTH_DEVICE_UUIDS);
157 return JSUtil::toJSValueRef(context, mUUIDs);