2 // Open Service Platform
3 // Copyright (c) 2012-2013 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.
17 // @file FNetBt_BluetoothGapSystemAdapter.cpp
18 // @brief This is the implementation file for the _BluetoothGapSystemAdapter class.
21 #include <unique_ptr.h>
23 #include <FBaseByteBuffer.h>
24 #include <FBaseUtilStringUtil.h>
25 #include <FBaseUuId.h>
27 #include <FNetBtBluetoothTypes.h>
28 #include <FNetBtBluetoothDevice.h>
29 #include <FBaseSysLog.h>
30 #include <FApp_AppInfo.h>
31 #include <FBase_StringConverter.h>
32 #include "FNetBt_BluetoothGapSystemAdapter.h"
33 #include "FNetBt_BluetoothDeviceImpl.h"
34 #include "FNetBt_IBluetoothDeviceEventListener.h"
35 #include "FNetBt_IBluetoothManagerEventListener.h"
36 #include "FNetBt_BluetoothAdapterUtility.h"
39 using namespace Tizen::App;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Utility;
43 using namespace Tizen::Io;
45 static const Tizen::Base::String _BT_ADDRESS_FILE_PATH(L"/opt/etc/.bd_addr");
47 namespace Tizen { namespace Net { namespace Bluetooth
50 struct _DeviceInfoDeleter
52 template<typename bt_device_info_s>
53 void operator()(bt_device_info_s* pDeviceInfo)
55 bt_adapter_free_device_info(pDeviceInfo);
59 _BluetoothGapSystemAdapter* _BluetoothGapSystemAdapter::__pInstance = null;
61 _BluetoothGapSystemAdapter::_BluetoothGapSystemAdapter(void)
62 : __mgrEvtListenerList()
63 , __devEvtListenerList()
67 _BluetoothGapSystemAdapter::~_BluetoothGapSystemAdapter(void)
69 // unset all the system callback methods
70 bt_adapter_unset_state_changed_cb();
71 bt_adapter_unset_device_discovery_state_changed_cb();
72 bt_adapter_unset_visibility_mode_changed_cb();
73 bt_device_unset_bond_created_cb();
74 bt_device_unset_bond_destroyed_cb();
75 bt_device_unset_service_searched_cb();
76 bt_device_unset_connection_state_changed_cb();
80 _BluetoothGapSystemAdapter::InitSingleton(void)
82 bool isConstructed = false;
83 static _BluetoothGapSystemAdapter inst;
85 isConstructed = inst.Construct();
86 SysTryReturnVoidResult(NID_NET_BT, isConstructed == true, E_SYSTEM,
87 "[E_SYSTEM] Construction of _BluetoothGapSystemAdpater fails.");
92 _BluetoothGapSystemAdapter*
93 _BluetoothGapSystemAdapter::GetInstance(void)
95 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
97 if (__pInstance == null)
100 pthread_once(&onceBlock, InitSingleton);
101 result r = GetLastResult();
104 onceBlock = PTHREAD_ONCE_INIT;
112 _BluetoothGapSystemAdapter::Construct(void)
114 if (bt_initialize() == BT_ERROR_NONE)
116 //register all the call backs.
117 if ((bt_adapter_set_state_changed_cb(&OnAdapterStateChanged, NULL) == BT_ERROR_NONE) &&
118 (bt_adapter_set_visibility_mode_changed_cb(&OnVisibilityModeChanged, NULL) == BT_ERROR_NONE) &&
119 (bt_device_set_bond_created_cb(&OnPairingCompleted, NULL) == BT_ERROR_NONE) &&
120 (bt_device_set_bond_destroyed_cb(&OnPairedDeviceRemoved, NULL) == BT_ERROR_NONE) &&
121 (bt_adapter_set_device_discovery_state_changed_cb(&OnDeviceDiscoveryStateChanged, NULL) == BT_ERROR_NONE) &&
122 (bt_device_set_service_searched_cb(&OnServiceDiscoveryCompleted, NULL) == BT_ERROR_NONE) &&
123 (bt_device_set_connection_state_changed_cb(&OnConnectionStateChanged, NULL) == BT_ERROR_NONE))
128 SysLog(NID_NET_BT, "Initialising the Bluetooth subsystem has failed");
134 _BluetoothGapSystemAdapter::RegisterManagerEventListener(const _IBluetoothManagerEventListener& listener, bool isHighPriority)
136 if (isHighPriority == true)
138 // insert the specified event listener at the begin of the list
139 return __mgrEvtListenerList.InsertAt(const_cast<_IBluetoothManagerEventListener*>(&listener), 0);
143 // insert the specified event listener at the end of the list
144 return __mgrEvtListenerList.Add(const_cast<_IBluetoothManagerEventListener*>(&listener));
149 _BluetoothGapSystemAdapter::UnregisterManagerEventListener(const _IBluetoothManagerEventListener& listener)
152 return __mgrEvtListenerList.Remove(const_cast<_IBluetoothManagerEventListener*>(&listener));
156 _BluetoothGapSystemAdapter::RegisterDeviceEventListener(const _IBluetoothDeviceEventListener& listener)
158 return __devEvtListenerList.Add(const_cast<_IBluetoothDeviceEventListener*>(&listener));
162 _BluetoothGapSystemAdapter::UnregisterDeviceEventListener(const _IBluetoothDeviceEventListener& listener)
164 return __devEvtListenerList.Remove(const_cast<_IBluetoothDeviceEventListener*>(&listener));
168 _BluetoothGapSystemAdapter::Activate(void)
170 if (bt_adapter_enable() == BT_ERROR_NONE)
178 _BluetoothGapSystemAdapter::Deactivate(void)
180 if (bt_adapter_disable() == BT_ERROR_NONE)
188 _BluetoothGapSystemAdapter::IsActivated(void) const
190 bt_adapter_state_e btAdapterState = BT_ADAPTER_DISABLED;
192 if (bt_adapter_get_state(&btAdapterState) == BT_ERROR_NONE)
194 if (btAdapterState == BT_ADAPTER_ENABLED)
203 _BluetoothGapSystemAdapter::IsAvailable(BluetoothConnectionType type) const
205 bool isAvailable = false;
207 char* pUuidStr = null;
211 SysLog(NID_NET_BT, "All connection type is not available because Bluetooth is not activated.");
217 case BT_CONNECTION_GENERIC:
221 case BT_CONNECTION_SPP_ACCEPTOR:
222 pUuidStr = _StringConverter::CopyToCharArrayN(UuId(BT_SVC_UUID_SPP).ToString());
223 // TODO: meaning of bt_adapter_is_service_used() method is quite wrong
224 if ((pUuidStr != null) && (bt_adapter_is_service_used(pUuidStr, &isUsed) == BT_ERROR_NONE))
226 SysLog(NID_NET_BT, "SPP(default UUID) server is %s.", isUsed ? "used" : "available");
229 isAvailable = isUsed ? false : true;
234 case BT_CONNECTION_OPP_SERVER:
235 pUuidStr = _StringConverter::CopyToCharArrayN(UuId(BT_SVC_UUID_OPP).ToString());
236 // TODO: meaning of bt_adapter_is_service_used() method is quite wrong
237 if ((pUuidStr != null) && (bt_adapter_is_service_used(pUuidStr, &isUsed) == BT_ERROR_NONE))
239 SysLog(NID_NET_BT, "OPP server is %s.", isUsed ? "used" : "available");
242 isAvailable = isUsed ? false : true;
247 // This is always true, because multiple SPP initiators can be run simultaneously since 3.0
248 case BT_CONNECTION_SPP_INITIATOR:
252 // The only one OPP client can be run per process.
253 case BT_CONNECTION_OPP_CLIENT:
265 _BluetoothGapSystemAdapter::GetLocalDeviceAddress(void) const
267 result r = E_SUCCESS;
269 ByteBuffer addressBuffer;
270 String localAddrString(_BT_ADDRESS_LENGTH*3);
271 char* pLocalAddr = null;
273 ret = bt_adapter_get_address(&pLocalAddr);
275 if (ret == BT_ERROR_NONE)
277 localAddrString = String(pLocalAddr);
285 // Opens the local cache file and gets the local address from it.
286 r = file.Construct(_BT_ADDRESS_FILE_PATH, L"r");
287 SysTryReturn(NID_NET_BT, r == E_SUCCESS, localAddrString, E_SYSTEM, "Construction of the address info file has failed.");
289 (void) file.Seek(FILESEEKPOSITION_BEGIN, 0);
291 addressBuffer.Construct(_BT_ADDRESS_LENGTH*3);
292 r = file.Read(addressBuffer);
293 SysTryReturn(NID_NET_BT, r == E_SUCCESS, localAddrString, E_SYSTEM, "Read the address from the info file has failed.");
295 bufLimit = addressBuffer.GetLimit();
297 for (int i = 0, addressIndex = 0 ; i < bufLimit ; i++)
299 addressBuffer.GetByte(i, curByte);
300 if ((curByte >= '0' && curByte <= '9') ||
301 (curByte >= 'a' && curByte <= 'f') ||
302 (curByte >= 'A' && curByte <= 'F'))
304 localAddrString.Append((char)curByte);
306 if ((addressIndex % 3 == 2) && (addressIndex < (_BT_ADDRESS_LENGTH * 3 - 1 )))
308 // append ":" int the localAddrString to form a correct BT address format[aa:bb:cc:dd:ee:ff]
309 localAddrString.Append(':');
315 localAddrString.ToUpperCase();
318 return localAddrString;
321 BluetoothDiscoverableMode
322 _BluetoothGapSystemAdapter::GetDiscoverableMode(void) const
324 bt_adapter_visibility_mode_e visMode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
325 BluetoothDiscoverableMode discMode = BT_DISC_MODE_NOT_DISCOVERABLE;
327 // CONNECTABLE is actually not exactly the same as NONE mode but dealt with as the same values herein.
328 // TODO: resolve the problem about failure when the BT adapter is disabled
329 if (bt_adapter_get_visibility(&visMode, null)== BT_ERROR_NONE)
331 if (visMode == BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE)
333 discMode = BT_DISC_MODE_DISCOVERABLE;
335 else if (visMode == BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE)
337 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
339 discMode = BT_DISC_MODE_DISCOVERABLE;
343 discMode = BT_DISC_MODE_TIME_LIMITED_DISCOVERABLE;
346 // BT_DISC_MODE_NOT_DISCOVERABLE, otherwise.
353 _BluetoothGapSystemAdapter::GetRemainingTimeAsDiscoverable(void) const
355 bt_adapter_visibility_mode_e visMode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
359 ret = bt_adapter_get_visibility(&visMode, &duration);
361 if ((ret != BT_ERROR_NONE) || (visMode != BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE))
363 SysLog(NID_NET_BT, "Not the time-limited discoverable mode! Remaining time is 0.");
369 Tizen::Base::Collection::IList*
370 _BluetoothGapSystemAdapter::GetAllPairedDeviceListN(void) const
372 ArrayList* pPairedDeviceList = null;
373 SysLog(NID_NET_BT, "Getting all paired device list...");
375 pPairedDeviceList = new (std::nothrow) ArrayList();
377 bt_adapter_state_e btAdapterState;
378 if (bt_adapter_get_state(&btAdapterState) == BT_ERROR_NONE)
380 if (btAdapterState == BT_ADAPTER_ENABLED)
382 if (bt_adapter_foreach_bonded_device(&OnPairedDeviceFound, pPairedDeviceList) != BT_ERROR_NONE)
384 pPairedDeviceList->RemoveAll(true);
385 delete pPairedDeviceList;
389 else //BT_ADAPTER_DISABLED
391 SysLog(NID_NET_BT, "Cannot get the paired device list because the Bluetooth apdater is disabled!!");
395 return pPairedDeviceList;
399 _BluetoothGapSystemAdapter::Pair(const Tizen::Base::ByteBuffer& deviceAddress)
402 char* pPairDevAddr = null;
404 SysTryReturnResult(NID_NET_BT, deviceAddress.GetRemaining() == _BT_ADDRESS_LENGTH, E_SYSTEM,
405 "The address of the remote device is incorrect.");
407 pPairDevAddr = _StringConverter::CopyToCharArrayN(_BluetoothDeviceImpl::GetAddressString(deviceAddress));
408 SysTryReturnResult(NID_NET_BT, pPairDevAddr != null, E_SYSTEM, "Address converting fails.");
410 if (bt_device_create_bond(pPairDevAddr) == BT_ERROR_NONE)
415 SysLog(NID_NET_BT, "Pairing with [%ls] %s.", _BluetoothDeviceImpl::GetAddressString(deviceAddress).GetPointer(),
416 r == E_SUCCESS ? "is successful" : "fails");
418 delete[] pPairDevAddr;
423 _BluetoothGapSystemAdapter::CancelPair(void)
427 if (bt_device_cancel_bonding() == BT_ERROR_NONE)
432 SysLog(NID_NET_BT, "Cancel of bonding %s.", r == E_SUCCESS ? "is successful" : "fails");
438 _BluetoothGapSystemAdapter::Unpair(const Tizen::Base::ByteBuffer& deviceAddress)
441 char* pUnpairDevAddr = null;
443 SysTryReturnResult(NID_NET_BT, deviceAddress.GetRemaining() == _BT_ADDRESS_LENGTH, E_SYSTEM,
444 "The address of the remote device is incorrect.");
446 pUnpairDevAddr = _StringConverter::CopyToCharArrayN(_BluetoothDeviceImpl::GetAddressString(deviceAddress));
447 SysTryReturnResult(NID_NET_BT, pUnpairDevAddr != null, E_SYSTEM, "Address converting fails.");
449 if (bt_device_destroy_bond(pUnpairDevAddr) == BT_ERROR_NONE)
454 SysLog(NID_NET_BT, "Unpairing with [%ls] %s.", _BluetoothDeviceImpl::GetAddressString(deviceAddress).GetPointer(),
455 r == E_SUCCESS ? "is successful" : "fails");
457 delete[] pUnpairDevAddr;
462 _BluetoothGapSystemAdapter::IsPaired(const Tizen::Base::ByteBuffer& deviceAddress) const
464 bool isPaired = false;
466 if (deviceAddress.GetRemaining() != _BT_ADDRESS_LENGTH)
468 SysLog(NID_NET_BT, "The address of the remote device is incorrect.");
472 char* pRemoteDevAddress = _StringConverter::CopyToCharArrayN(_BluetoothDeviceImpl::GetAddressString(deviceAddress));
473 bt_device_info_s* pDeviceInfo = null;
475 if (bt_adapter_get_bonded_device_info(pRemoteDevAddress, &pDeviceInfo) == BT_ERROR_NONE)
479 if (pDeviceInfo->is_bonded)
483 bt_adapter_free_device_info(pDeviceInfo);
487 delete[] pRemoteDevAddress;
492 _BluetoothGapSystemAdapter::StartDiscovery(void)
496 if (bt_adapter_start_device_discovery() == BT_ERROR_NONE)
501 SysLog(NID_NET_BT, "Start of discovery %s.", r == E_SUCCESS ? "is successful" : "fails");
507 _BluetoothGapSystemAdapter::CancelDiscovery(void)
511 if (bt_adapter_stop_device_discovery() == BT_ERROR_NONE)
516 SysLog(NID_NET_BT, "Cancel of discovery %s.", r == E_SUCCESS ? "is successful" : "fails");
522 _BluetoothGapSystemAdapter::IsDiscoveryInProgress(void) const
524 bool is_discovering = false;
526 (void) bt_adapter_is_discovering(&is_discovering);
528 return is_discovering;
532 _BluetoothGapSystemAdapter::RetrieveServiceList(const Tizen::Base::ByteBuffer& deviceAddress)
535 char* pDevAddr = null;
537 SysTryReturnResult(NID_NET_BT, deviceAddress.GetRemaining() == _BT_ADDRESS_LENGTH, E_SYSTEM,
538 "The address of the remote device is incorrect.");
540 pDevAddr = _StringConverter::CopyToCharArrayN(_BluetoothDeviceImpl::GetAddressString(deviceAddress));
541 SysTryReturnResult(NID_NET_BT, pDevAddr != null, E_SYSTEM, "Address converting fails.");
543 if (bt_device_start_service_search(pDevAddr) == BT_ERROR_NONE)
548 SysLog(NID_NET_BT, "Retrieving the service list from [%ls] %s.",
549 _BluetoothDeviceImpl::GetAddressString(deviceAddress).GetPointer(), r == E_SUCCESS ? "is successful" : "fails");
556 _BluetoothGapSystemAdapter::GetPairedDevice(const ByteBuffer& address, BluetoothDevice& pairedDevice)
558 int err = BT_ERROR_NONE;
559 unique_ptr<char[]> pDevAddr;
560 bt_device_info_s* pDeviceInfo = null;
561 unique_ptr<bt_device_info_s, _DeviceInfoDeleter> pDeviceInfoPtr;
562 _BluetoothDeviceImpl* pDevImpl = null;
563 String convertedName;
565 pDevAddr.reset(_StringConverter::CopyToCharArrayN(_BluetoothDeviceImpl::GetAddressString(address)));
566 SysTryReturnResult(NID_NET_BT, pDevAddr != null, E_SYSTEM, "Address converting fails.");
568 err = bt_adapter_get_bonded_device_info(pDevAddr.get(), &pDeviceInfo);
569 SysTryReturnResult(NID_NET_BT, err == BT_ERROR_NONE, E_SYSTEM, "Getting paired device infomation has failed.");
570 pDeviceInfoPtr.reset(pDeviceInfo);
572 pDevImpl = _BluetoothDeviceImpl::GetInstance(pairedDevice);
574 // converts the UTF8 multibyte string to Unicode String
575 (void) StringUtil::Utf8ToString(pDeviceInfoPtr->remote_name, convertedName);
577 pDevImpl->SetAddress(address);
578 pDevImpl->SetName(convertedName);
579 pDevImpl->SetPaired(pDeviceInfoPtr->is_bonded);
580 pDevImpl->SetMajorDeviceClassType(_BluetoothAdapterUtility::ConvertToMajorDeviceClassType(
581 pDeviceInfoPtr->bt_class.major_device_class));
582 pDevImpl->SetMinorDeviceClassType(_BluetoothAdapterUtility::ConvertToMinorDeviceClassType(
583 pDeviceInfoPtr->bt_class.major_device_class, pDeviceInfoPtr->bt_class.minor_device_class));
584 pDevImpl->SetServiceClassList(_BluetoothAdapterUtility::ConvertToServiceClassList(
585 pDeviceInfoPtr->bt_class.major_service_class_mask));
586 pDevImpl->SetServiceList(_BluetoothAdapterUtility::ConvertToServiceList(
587 pDeviceInfoPtr->service_uuid, pDeviceInfoPtr->service_count));
588 pDevImpl->SetServiceUuidList(_BluetoothAdapterUtility::ConvertServiceUuidListN(
589 pDeviceInfoPtr->service_uuid, pDeviceInfoPtr->service_count), false);
595 _BluetoothGapSystemAdapter::OnAdapterStateChanged(int status, bt_adapter_state_e adapterState, void* pUserdata)
597 result r = E_SUCCESS;
598 _IBluetoothManagerEventListener* pMgrEvtListener = null;
599 IEnumeratorT<_IBluetoothManagerEventListener*>* pEnum = null;
600 _BluetoothGapSystemAdapter* pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
602 if (status != BT_ERROR_NONE)
607 pEnum = pGapAdapter->__mgrEvtListenerList.GetEnumeratorN();
608 while (pEnum->MoveNext() == E_SUCCESS)
610 pEnum->GetCurrent(pMgrEvtListener);
611 if(adapterState == BT_ADAPTER_ENABLED)
613 pMgrEvtListener->OnBluetoothActivated(r);
617 pMgrEvtListener->OnBluetoothDeactivated(r);
625 _BluetoothGapSystemAdapter::OnVisibilityModeChanged(int result, bt_adapter_visibility_mode_e mode, void* pUserdata)
627 _IBluetoothManagerEventListener* pMgrEvtListener = null;
628 IEnumeratorT<_IBluetoothManagerEventListener*>* pEnum = null;
629 _BluetoothGapSystemAdapter* pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
630 BluetoothDiscoverableMode changedMode = BT_DISC_MODE_NOT_DISCOVERABLE;
632 if (result == BT_ERROR_NONE)
636 case BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE:
637 changedMode = BT_DISC_MODE_NOT_DISCOVERABLE;
640 case BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE:
641 changedMode = BT_DISC_MODE_DISCOVERABLE;
644 case BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE:
645 changedMode = BT_DISC_MODE_TIME_LIMITED_DISCOVERABLE;
652 pEnum = pGapAdapter->__mgrEvtListenerList.GetEnumeratorN();
653 while (pEnum->MoveNext() == E_SUCCESS)
655 pEnum->GetCurrent(pMgrEvtListener);
656 pMgrEvtListener->OnBluetoothDiscoverableModeChanged(changedMode);
664 _BluetoothGapSystemAdapter::OnDeviceDiscoveryStateChanged(int status, bt_adapter_device_discovery_state_e discoveryState,
665 bt_adapter_device_discovery_info_s* pDiscoveryInfo, void* pUserData)
667 result r = E_SUCCESS;
668 _IBluetoothDeviceEventListener* pDevEventListener = null;
669 IEnumeratorT<_IBluetoothDeviceEventListener*>* pEnum = null;
670 _BluetoothGapSystemAdapter* pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
672 pEnum = pGapAdapter->__devEvtListenerList.GetEnumeratorN();
674 switch(discoveryState)
676 case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
678 if (status != BT_ERROR_NONE)
683 while (pEnum->MoveNext() == E_SUCCESS)
685 pEnum->GetCurrent(pDevEventListener);
686 pDevEventListener->OnBluetoothDiscoveryStarted(r);
691 case BT_ADAPTER_DEVICE_DISCOVERY_FOUND:
693 BluetoothDevice foundDevice;
694 ByteBuffer deviceAddress;
695 deviceAddress.Construct(_BT_ADDRESS_LENGTH);
696 String convertedName;
698 String tempDevAddr(pDiscoveryInfo->remote_address);
699 _BluetoothDeviceImpl::GetAddressByteBuffer(tempDevAddr, L":", deviceAddress);
701 // converts the UTF8 multibyte string to Unicode String
702 (void) StringUtil::Utf8ToString(pDiscoveryInfo->remote_name, convertedName);
704 _BluetoothDeviceImpl* pDevImpl = _BluetoothDeviceImpl::GetInstance(foundDevice);
706 pDevImpl->SetAddress(deviceAddress);
707 pDevImpl->SetName(convertedName);
708 pDevImpl->SetPaired(pDiscoveryInfo->is_bonded);
709 pDevImpl->SetRssi(pDiscoveryInfo->rssi);
710 pDevImpl->SetMajorDeviceClassType(_BluetoothAdapterUtility::ConvertToMajorDeviceClassType(
711 pDiscoveryInfo->bt_class.major_device_class));
712 pDevImpl->SetMinorDeviceClassType(_BluetoothAdapterUtility::ConvertToMinorDeviceClassType(
713 pDiscoveryInfo->bt_class.major_device_class, pDiscoveryInfo->bt_class.minor_device_class));
714 pDevImpl->SetServiceClassList(_BluetoothAdapterUtility::ConvertToServiceClassList(
715 pDiscoveryInfo->bt_class.major_service_class_mask));
716 pDevImpl->SetServiceList(_BluetoothAdapterUtility::ConvertToServiceList(
717 pDiscoveryInfo->service_uuid, pDiscoveryInfo->service_count));
718 pDevImpl->SetServiceUuidList(_BluetoothAdapterUtility::ConvertServiceUuidListN(
719 pDiscoveryInfo->service_uuid, pDiscoveryInfo->service_count), false);
721 _BluetoothAdapterUtility::PrintDeviceInfo(foundDevice);
723 while (pEnum->MoveNext() == E_SUCCESS)
725 pEnum->GetCurrent(pDevEventListener);
726 pDevEventListener->OnBluetoothRemoteDeviceFound(foundDevice);
731 case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
733 bool isCompleted = false;
735 if (status == BT_ERROR_NONE)
737 isCompleted = true; // isCompleted is false in other cases including BT_ERROR_CANCEL
740 while (pEnum->MoveNext() == E_SUCCESS)
742 pEnum->GetCurrent(pDevEventListener);
743 pDevEventListener->OnBluetoothDiscoveryDone(isCompleted);
756 _BluetoothGapSystemAdapter::OnServiceDiscoveryCompleted(int status, bt_device_sdp_info_s* pSdpInfo, void* pUserData)
758 result r = E_SUCCESS;
759 _IBluetoothDeviceEventListener* pDevEventListener = null;
760 IEnumeratorT<_IBluetoothDeviceEventListener*>* pEnum = null;
761 ByteBuffer deviceAddress;
762 deviceAddress.Construct(_BT_ADDRESS_LENGTH);
763 unsigned long serviceList = 0;
764 Tizen::Base::Collection::IList* pServiceUuidList= null;
765 _BluetoothGapSystemAdapter* pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
767 String tempDevAddr(pSdpInfo->remote_address);
768 r = _BluetoothDeviceImpl::GetAddressByteBuffer(tempDevAddr, L":", deviceAddress);
770 if (status != BT_ERROR_NONE)
776 pServiceUuidList = _BluetoothAdapterUtility::ConvertServiceUuidListN(
777 pSdpInfo->service_uuid, pSdpInfo->service_count);
778 if (pServiceUuidList == null)
784 serviceList = _BluetoothAdapterUtility::ConvertToServiceList(pSdpInfo->service_uuid, pSdpInfo->service_count);
785 SysLog(NID_NET_BT, "Searched Service List =================");
786 _BluetoothAdapterUtility::PrintServiceList(serviceList);
788 SysLog(NID_NET_BT, "Searched Service UUID List ============");
789 _BluetoothAdapterUtility::PrintServiceUuidList(pServiceUuidList);
793 pEnum = pGapAdapter->__devEvtListenerList.GetEnumeratorN();
795 while (pEnum->MoveNext() == E_SUCCESS)
797 pEnum->GetCurrent(pDevEventListener);
798 pDevEventListener->OnBluetoothServiceListReceived(deviceAddress, serviceList, pServiceUuidList, r);
802 // The ownership of UuId instances in the list is transferred to _BluetoothDeviceEventArg.
803 // Therefore, only the list except its elements is deleted here.
804 delete pServiceUuidList;
808 _BluetoothGapSystemAdapter::OnPairingCompleted(int status, bt_device_info_s* pDeviceInfo, void* pUserdata)
810 result r = E_SUCCESS;
811 _IBluetoothDeviceEventListener* pDevEventListener = null;
812 IEnumeratorT<_IBluetoothDeviceEventListener*>* pEnum = null;
813 BluetoothDevice pairingDevice;
814 _BluetoothGapSystemAdapter* pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
815 ByteBuffer deviceAddress;
816 deviceAddress.Construct(_BT_ADDRESS_LENGTH);
817 String convertedName;
819 SysTryReturnVoidResult(NID_NET_BT, pDeviceInfo != null, E_SYSTEM,
820 "[E_SYSTEM] the input param, DeviceInfo on callback is null.");
822 String tempDevAddr(pDeviceInfo->remote_address);
823 (void) _BluetoothDeviceImpl::GetAddressByteBuffer(tempDevAddr, L":", deviceAddress);
825 // converts the UTF8 multibyte string to Unicode String
826 (void) StringUtil::Utf8ToString(pDeviceInfo->remote_name, convertedName);
828 _BluetoothDeviceImpl* pDevImpl = _BluetoothDeviceImpl::GetInstance(pairingDevice);
830 pDevImpl->SetAddress(deviceAddress);
831 pDevImpl->SetName(convertedName);
832 pDevImpl->SetPaired(true);
833 pDevImpl->SetRssi(0);
834 pDevImpl->SetMajorDeviceClassType(_BluetoothAdapterUtility::ConvertToMajorDeviceClassType(
835 pDeviceInfo->bt_class.major_device_class));
836 pDevImpl->SetMinorDeviceClassType(_BluetoothAdapterUtility::ConvertToMinorDeviceClassType(
837 pDeviceInfo->bt_class.major_device_class, pDeviceInfo->bt_class.minor_device_class));
838 pDevImpl->SetServiceClassList(_BluetoothAdapterUtility::ConvertToServiceClassList(
839 pDeviceInfo->bt_class.major_service_class_mask));
840 pDevImpl->SetServiceList(_BluetoothAdapterUtility::ConvertToServiceList(
841 pDeviceInfo->service_uuid, pDeviceInfo->service_count));
842 pDevImpl->SetServiceUuidList(_BluetoothAdapterUtility::ConvertServiceUuidListN(
843 pDeviceInfo->service_uuid, pDeviceInfo->service_count), false);
850 case BT_ERROR_CANCELLED:
851 r = E_OPERATION_CANCELED;
853 case BT_ERROR_TIMED_OUT:
857 r = E_OPERATION_FAILED;
861 pEnum = pGapAdapter->__devEvtListenerList.GetEnumeratorN();
862 while (pEnum->MoveNext() == E_SUCCESS)
864 pEnum->GetCurrent(pDevEventListener);
865 pDevEventListener->OnBluetoothPaired(&pairingDevice, r);
871 _BluetoothGapSystemAdapter::OnPairedDeviceRemoved(int status, char* pRemoteAddress, void* pUserData)
873 _IBluetoothDeviceEventListener* pDevEventListener = null;
874 IEnumeratorT<_IBluetoothDeviceEventListener*>* pEnum = null;
875 _BluetoothGapSystemAdapter* pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
876 ByteBuffer deviceAddress;
877 deviceAddress.Construct(_BT_ADDRESS_LENGTH);
879 if (status == BT_ERROR_NONE)
881 String tempDevAddr(pRemoteAddress);
882 _BluetoothDeviceImpl::GetAddressByteBuffer(tempDevAddr, L":", deviceAddress);
884 pEnum = pGapAdapter->__devEvtListenerList.GetEnumeratorN();
886 while (pEnum->MoveNext() == E_SUCCESS)
888 pEnum->GetCurrent(pDevEventListener);
889 pDevEventListener->OnBluetoothUnpaired(deviceAddress);
896 SysLog(NID_NET_BT, "Error in removing the Paired Device is %d", status);
901 _BluetoothGapSystemAdapter::OnPairedDeviceFound(bt_device_info_s* pDeviceInfo, void* pUserdata)
903 ArrayList* pPairedDeviceList = static_cast<ArrayList*>(pUserdata);
905 ByteBuffer btAddress;
906 btAddress.Construct(_BT_ADDRESS_LENGTH);
907 BluetoothDevice* pPairedDevice = null;
908 String convertedName;
910 pPairedDevice = new (std::nothrow) BluetoothDevice();
911 SysTryReturn(NID_NET_BT, pPairedDevice != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
913 String tempDevAddr(pDeviceInfo->remote_address);
914 _BluetoothDeviceImpl::GetAddressByteBuffer(tempDevAddr, L":", btAddress);
916 // converts the UTF8 multibyte string to Unicode String
917 (void) StringUtil::Utf8ToString(pDeviceInfo->remote_name, convertedName);
919 SysLog(NID_NET_BT, "Paired Device - (name: %ls)", convertedName.GetPointer());
921 _BluetoothDeviceImpl* pDevImpl = _BluetoothDeviceImpl::GetInstance(*pPairedDevice);
923 pDevImpl->SetAddress(btAddress);
924 pDevImpl->SetName(convertedName);
925 pDevImpl->SetPaired(true);
926 pDevImpl->SetRssi(0);
927 pDevImpl->SetMajorDeviceClassType(_BluetoothAdapterUtility::ConvertToMajorDeviceClassType(
928 pDeviceInfo->bt_class.major_device_class));
929 pDevImpl->SetMinorDeviceClassType(_BluetoothAdapterUtility::ConvertToMinorDeviceClassType(
930 pDeviceInfo->bt_class.major_device_class, pDeviceInfo->bt_class.minor_device_class));
931 pDevImpl->SetServiceClassList(_BluetoothAdapterUtility::ConvertToServiceClassList(
932 pDeviceInfo->bt_class.major_service_class_mask));
933 pDevImpl->SetServiceList(_BluetoothAdapterUtility::ConvertToServiceList(
934 pDeviceInfo->service_uuid, pDeviceInfo->service_count));
935 pDevImpl->SetServiceUuidList(_BluetoothAdapterUtility::ConvertServiceUuidListN(
936 pDeviceInfo->service_uuid, pDeviceInfo->service_count), false);
938 pPairedDeviceList->Add(*pPairedDevice);
944 _BluetoothGapSystemAdapter::OnConnectionStateChanged(bool isConnected, const char* pRemoteAddress, void* pUserdata)
946 _IBluetoothDeviceEventListener* pDevEventListener = null;
947 IEnumeratorT<_IBluetoothDeviceEventListener*>* pEnum = null;
948 _BluetoothGapSystemAdapter* pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
949 ByteBuffer deviceAddress;
950 deviceAddress.Construct(_BT_ADDRESS_LENGTH);
952 String tempDevAddr(pRemoteAddress);
953 _BluetoothDeviceImpl::GetAddressByteBuffer(tempDevAddr, L":", deviceAddress);
955 pEnum = pGapAdapter->__devEvtListenerList.GetEnumeratorN();
957 while (pEnum->MoveNext() == E_SUCCESS)
959 pEnum->GetCurrent(pDevEventListener);
962 pDevEventListener->OnBluetoothDeviceConnected(deviceAddress);
966 pDevEventListener->OnBluetoothDeviceDisconnected(deviceAddress);
973 } } } // Tizen::Net::Bluetooth