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 "BluetoothUtil.h"
25 #include "JSBluetoothClass.h"
29 using namespace DeviceAPI::Common;
34 BluetoothDevice::BluetoothDevice(
35 bt_adapter_device_discovery_info_s *discoveryInfo)
37 Common::SecurityAccessor();
38 mName = std::string(discoveryInfo->remote_name);
39 mAddress = std::string(discoveryInfo->remote_address);
40 mDeviceClass = BluetoothClassSharedPtr(
41 new BluetoothClass(discoveryInfo->bt_class));
43 for(int i = 0; i < discoveryInfo->service_count; i++) {
44 mUUIDs.push_back(std::string(discoveryInfo->service_uuid[i]));
49 BluetoothDevice::BluetoothDevice(bt_device_info_s *deviceInfo)
51 Common::SecurityAccessor();
52 mName = std::string(deviceInfo->remote_name);
53 mAddress = std::string(deviceInfo->remote_address);
54 mDeviceClass = BluetoothClassSharedPtr(
55 new BluetoothClass(deviceInfo->bt_class));
57 for(int i = 0; i < deviceInfo->service_count; i++) {
58 mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
63 BluetoothDevice::~BluetoothDevice()
65 BluetoothAdapter::getInstance()->removeConnReq(mAddress);
68 void BluetoothDevice::updateInfo(bt_device_info_s *deviceInfo)
70 mName = std::string(deviceInfo->remote_name);
72 for(int i = 0; i < deviceInfo->service_count; i++) {
73 mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
78 std::string BluetoothDevice::getName() const
83 std::string BluetoothDevice::getAddress() const
88 void BluetoothDevice::copyAceCheckAccessFunction(
89 const Common::SecurityAccessor *securityAccessor)
91 Common::SecurityAccessor::copyAceCheckAccessFunction(securityAccessor);
92 mDeviceClass->copyAceCheckAccessFunction(securityAccessor);
95 JSValueRef BluetoothDevice::getDeviceClass(JSContextRef context)
97 return JSBluetoothClass::createJSObject(context, mDeviceClass);
100 bool BluetoothDevice::isBonded() const
102 bool isBonded = false;
104 bt_device_info_s *deviceInfo = NULL;
107 ret = bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo);
108 if (ret != BT_ERROR_NONE) {
110 BluetoothUtil::throwBluetoothException(ret,
111 "bt_adapter_get_bonded_device_info failed");
114 LOGE("deviceInfo is null");
115 throw UnknownException("deviceInfo is null");
118 isBonded = deviceInfo->is_bonded;
119 ret = bt_adapter_free_device_info(deviceInfo);
120 if (ret != BT_ERROR_NONE) {
122 BluetoothUtil::throwBluetoothException(ret,
123 "bt_adapter_free_device_info failed");
125 } catch (BasePlatformException &e) {
126 LOGE("%s: %s", e.getName().c_str(), e.getMessage().c_str());
131 bool BluetoothDevice::isTrusted() const
133 bool isTrusted = false;
135 bt_device_info_s *deviceInfo = NULL;
138 ret = bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo);
139 if (ret != BT_ERROR_NONE) {
141 BluetoothUtil::throwBluetoothException(ret,
142 "bt_adapter_get_bonded_device_info failed");
145 LOGE("deviceInfo is null");
146 throw UnknownException("deviceInfo is null");
149 isTrusted = deviceInfo->is_authorized;
150 ret = bt_adapter_free_device_info(deviceInfo);
151 if (ret != BT_ERROR_NONE) {
153 BluetoothUtil::throwBluetoothException(ret,
154 "bt_adapter_free_device_info failed");
156 } catch (BasePlatformException &e) {
157 LOGE("%s: %s", e.getName().c_str(), e.getMessage().c_str());
162 bool BluetoothDevice::isConnected() const
164 bool isConnected = false;
166 bt_device_info_s *deviceInfo = NULL;
169 ret = bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo);
170 if (ret != BT_ERROR_NONE) {
172 BluetoothUtil::throwBluetoothException(ret,
173 "bt_adapter_get_bonded_device_info failed");
176 LOGE("deviceInfo is null");
177 throw UnknownException("deviceInfo is null");
180 isConnected = deviceInfo->is_connected;
181 ret = bt_adapter_free_device_info(deviceInfo);
182 if (ret != BT_ERROR_NONE) {
184 BluetoothUtil::throwBluetoothException(ret,
185 "bt_adapter_free_device_info failed");
187 } catch (BasePlatformException &e) {
188 LOGE("%s: %s", e.getName().c_str(), e.getMessage().c_str());
193 JSValueRef BluetoothDevice::getUUIDs(JSContextRef context)
195 return JSUtil::toJSValueRef(context, mUUIDs);