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 "BluetoothLEDevice.h"
19 #include "BluetoothUtil.h"
22 #include "plugin_config_impl.h"
28 BluetoothLEDevice::BluetoothLEDevice(
29 bt_adapter_le_device_scan_result_info_s* deviceInfo)
33 m_address = deviceInfo->remote_address;
35 char *device_name = NULL;
36 int ret = bt_adapter_le_get_scan_result_device_name(deviceInfo,
37 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &device_name);
38 if (ret != BT_ERROR_NONE) {
39 LOGE("Failed to get device name from scan response: %d", ret);
40 int inRet = bt_adapter_le_get_scan_result_device_name(deviceInfo,
41 BT_ADAPTER_LE_PACKET_ADVERTISING , &device_name);
42 if (inRet != BT_ERROR_NONE) {
43 LOGE("Failed to get device name from advertise data: %d", ret);
56 ret = bt_adapter_le_get_scan_result_tx_power_level(deviceInfo,
57 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &powerLevel);
58 if (ret != BT_ERROR_NONE) {
59 LOGE("Failed to get txpower strength from scan response: %d", ret);
60 int inRet = bt_adapter_le_get_scan_result_tx_power_level(deviceInfo,
61 BT_ADAPTER_LE_PACKET_ADVERTISING , &powerLevel);
62 if (inRet != BT_ERROR_NONE) {
63 LOGE("Failed to get txpower strength from advertise data: %d", ret);
66 m_txPower = powerLevel;
70 m_txPower = powerLevel;
74 ret = bt_adapter_le_get_scan_result_appearance(deviceInfo,
75 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &appearance);
76 if (ret != BT_ERROR_NONE) {
77 LOGE("Failed to get appearance value from scan response: %d", ret);
78 int inRet = bt_adapter_le_get_scan_result_appearance(deviceInfo,
79 BT_ADAPTER_LE_PACKET_ADVERTISING , &appearance);
80 if (inRet != BT_ERROR_NONE) {
81 LOGE("Failed to get appearance value from advertise data: %d", ret);
84 m_appearance = appearance;
88 m_appearance = appearance;
91 bt_adapter_le_service_data_s *serviceDataList = NULL;
92 int serviceDataListCount = 0;
93 ret = bt_adapter_le_get_scan_result_service_data_list(deviceInfo,
94 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &serviceDataList, &serviceDataListCount);
95 if (ret != BT_ERROR_NONE) {
96 LOGE("Failed to get service data list from scan response: %d", ret);
97 int inRet = bt_adapter_le_get_scan_result_service_data_list(deviceInfo,
98 BT_ADAPTER_LE_PACKET_ADVERTISING , &serviceDataList, &serviceDataListCount);
99 if (inRet != BT_ERROR_NONE) {
100 LOGE("Failed to get service data list from advertise data: %d", ret);
103 assignServiceDataList(serviceDataList, serviceDataListCount);
107 assignServiceDataList(serviceDataList, serviceDataListCount);
110 int manufacturer_id = 0;
111 char* manufacturer_data = NULL;
112 int manufacturerDataCount = 0;
113 BluetoothLEManufacturerData* manufacturer = new BluetoothLEManufacturerData();
115 ret = bt_adapter_le_get_scan_result_manufacturer_data(deviceInfo,
116 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &manufacturer_id, &manufacturer_data, &manufacturerDataCount);
117 if (ret != BT_ERROR_NONE) {
118 LOGE("Failed to get manufacturer data list from scan response: %d", ret);
119 int inRet = bt_adapter_le_get_scan_result_manufacturer_data(deviceInfo,
120 BT_ADAPTER_LE_PACKET_ADVERTISING , &manufacturer_id, &manufacturer_data, &manufacturerDataCount);
121 if (inRet != BT_ERROR_NONE) {
122 LOGE("Failed to get manufacturer data list from advertise data: %d", ret);
123 manufacturer->setId("");
124 manufacturer->setData("");
125 m_manufacturerData = manufacturer;
126 g_free(manufacturer_data);
129 LOGD("manufacturer : id %d, data %s", manufacturer_id, manufacturer_data);
130 manufacturer->setId(std::to_string(manufacturer_id));
131 manufacturer->setData(manufacturer_data);
132 m_manufacturerData = manufacturer;
133 g_free(manufacturer_data);
137 manufacturer->setId(std::to_string(manufacturer_id));
138 manufacturer->setData(manufacturer_data);
139 m_manufacturerData = manufacturer;
140 g_free(manufacturer_data);
143 char** service_solicitation_uuids = NULL;
144 int service_solicitation_uuids_count = 0;
145 ret = bt_adapter_le_get_scan_result_service_solicitation_uuids(deviceInfo,
146 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &service_solicitation_uuids, &service_solicitation_uuids_count);
147 if (ret != BT_ERROR_NONE) {
148 LOGE("Failed to get the service solicitation UUID list from scan response: %d", ret);
149 int inRet = bt_adapter_le_get_scan_result_service_solicitation_uuids(deviceInfo,
150 BT_ADAPTER_LE_PACKET_ADVERTISING , &service_solicitation_uuids, &service_solicitation_uuids_count);
151 if (inRet != BT_ERROR_NONE) {
152 LOGE("Failed to get the service solicitation UUID list from advertise data: %d", ret);
155 assignServicesolicitationUUIDS(service_solicitation_uuids, service_solicitation_uuids_count);
159 assignServicesolicitationUUIDS(service_solicitation_uuids, service_solicitation_uuids_count);
164 ret = bt_adapter_le_get_scan_result_service_uuids(deviceInfo,
165 BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &uuids, &count);
166 if (ret != BT_ERROR_NONE) {
167 LOGE("Failed to get the service service_uuids list from scan response: %d", ret);
168 int inRet = bt_adapter_le_get_scan_result_service_uuids(deviceInfo,
169 BT_ADAPTER_LE_PACKET_ADVERTISING , &uuids, &count);
170 if (inRet != BT_ERROR_NONE) {
171 LOGE("Failed to get the service service_uuids list from advertise data: %d", ret);
174 m_uuids = std::vector<std::string>();
175 for (int i = 0; i < count; ++i) {
176 m_uuids->push_back(uuids[i]);
183 m_uuids = std::vector<std::string>();
184 for (int i = 0; i < count; ++i) {
185 m_uuids->push_back(uuids[i]);
192 void BluetoothLEDevice::assignServicesolicitationUUIDS(char** service_solicitation_uuids,
193 int service_solicitation_uuids_count) {
195 m_service_solicitation_uuids = std::vector<std::string>();
196 for (int i = 0; i < service_solicitation_uuids_count; ++i) {
197 m_service_solicitation_uuids->push_back(
198 std::string(service_solicitation_uuids[i]));
199 g_free(service_solicitation_uuids[i]);
201 g_free(service_solicitation_uuids);
204 void BluetoothLEDevice::assignServiceDataList(bt_adapter_le_service_data_s *serviceDataList,
205 int serviceDataListCount) {
207 m_servicesList = std::vector< std::pair<std::string, std::string>>();
208 for( int i = 0; i < serviceDataListCount; ++i){
209 m_servicesList->push_back(std::make_pair(std::string(serviceDataList[i].service_uuid, 4),
210 std::string(serviceDataList[i].service_data, serviceDataList[i].service_data_len)));
213 if( serviceDataListCount > 0 ) {
214 int ret = bt_adapter_le_free_service_data_list(serviceDataList, serviceDataListCount);
215 if( ret != BT_ERROR_NONE ) {
216 LOGW("Failed to free service data list: %d", ret);
219 g_free(serviceDataList);
222 std::string BluetoothLEDevice::getAddress() const
227 boost::optional<std::string> BluetoothLEDevice::getName() const
236 boost::optional<std::vector<std::string>> BluetoothLEDevice::getUUIDs() const
245 boost::optional<long> BluetoothLEDevice::getTXPower() const {
254 boost::optional<long> BluetoothLEDevice::getAppearance() const {
263 boost::optional<std::vector<std::string>> BluetoothLEDevice::getSolicitationUUIDs() const {
265 if(!m_service_solicitation_uuids) {
269 return m_service_solicitation_uuids;
272 JSValueRef BluetoothLEDevice::getManufacturer(JSContextRef ctx) const {
274 if(!m_manufacturerData) {
275 return JSValueMakeNull(ctx);
278 JSObjectRef jsobject = JSObjectMake(ctx, NULL, NULL );
279 Common::JSUtil::setProperty(ctx , jsobject, BLUETOOTH_LE_DEVICE_MANUFACTURER_DATA_ID,
280 Common::JSUtil::toJSValueRef(ctx, m_manufacturerData->getId()), kJSPropertyAttributeNone, NULL);
281 Common::JSUtil::setProperty(ctx , jsobject, BLUETOOTH_LE_DEVICE_MANUFACTURER_DATA_DATA,
282 Common::JSUtil::toJSValueRef(ctx, m_manufacturerData->getData()), kJSPropertyAttributeNone, NULL);
286 JSValueRef BluetoothLEDevice::getServiceData(JSContextRef ctx) const {
289 if(!m_servicesList) {
290 return JSValueMakeNull(ctx);
293 std::vector<JSObjectRef> servicesDatas;
294 for( auto it = m_servicesList->begin(); it != m_servicesList->end(); ++it )
296 JSObjectRef jsservice = JSObjectMake(ctx, NULL, NULL );
297 Common::JSUtil::setProperty(ctx , jsservice, BLUETOOTH_LE_DEVICE_SERVICE_DATA_UUIDS,
298 Common::JSUtil::toJSValueRef(ctx, it->first), kJSPropertyAttributeNone, NULL);
299 Common::JSUtil::setProperty(ctx , jsservice, BLUETOOTH_LE_DEVICE_SERVICE_DATA_DATA,
300 Common::JSUtil::toJSValueRef(ctx, it->second), kJSPropertyAttributeNone, NULL);
301 servicesDatas.push_back(jsservice);
303 return Common::JSUtil::toJSValueRef_<JSObjectRef>(ctx, servicesDatas);