Checks whether Bluetooth is enabled.

Source: Internet
Author: User

 

Checks whether Bluetooth is enabled. This is a classic method.
#include <centralrepository.h> // for CRepository
#include <BTServerSDKCRKeys.h> // for KCRUidBluetoothPowerState, KBTPowerState
#include <BtnotifierAPI.h> // for KPowerModeSettingNotifierUid

Library required:(Required link)

LIBRARY        centralrepository.lib  //CRepository

Source file:

//-----------------------------------------------------------------------------------
//CBTEngine::CheckBluetoothPowerStateL()
//This will check the Bluetooth power state and if switched off
//then ask to switch it on.
//------------------------------------------------------------------------------------
 
TBool CBTEngine::CheckBluetoothPowerStateL()
{
if(BluetoothPowerStateL())
{
return ETrue;
}
else
{
return TurnBluetoothOnL();
}
}
 
// ----------------------------------------------------------------------------
// CBTEngine::BluetoothPowerStateL()
// Get the Bluetooth power state of the device.
// ----------------------------------------------------------------------------
//
TBool CBTEngine::BluetoothPowerStateL()
{
CRepository* crep = CRepository::NewLC(KCRUidBluetoothPowerState);
TInt value = 0;
User::LeaveIfError(crep->Get(KBTPowerState, value));
CleanupStack::PopAndDestroy(crep);
 
return !(value == 0);
}
 
// ----------------------------------------------------------------------------
// CBTEngine::TurnBluetoothOnL()
// Uses the Notifier API to ask the user to turn on Bluetooth
// if it's not on already.
// ----------------------------------------------------------------------------
//
TBool CBTEngine::TurnBluetoothOnL()
{
RNotifier notifier;
User::LeaveIfError( notifier.Connect() );
TPckgBuf<TBool> dummy(ETrue);
TPckgBuf<TBool> reply(EFalse);
TRequestStatus status;
notifier.StartNotifierAndGetResponse(status,
KPowerModeSettingNotifierUid, dummy, reply);
User::WaitForRequest(status);
notifier.CancelNotifier(KPowerModeSettingNotifierUid);
notifier.Close();
 
return reply();
}
 
The following is another method ::
Versions earlier than 3rd: tbool get‑thon () // btonoff
{
// Create and initialise an fig
Rhostresolver hr;
Tint res = false;
Tbool ret; rsocketserv socketserver;
User: leaveiferror (socketserver. Connect ());
Cleanupclosepushl (socketserver); tprotocoldesc pinfo;
// _ Increment (kl2cap, "btlinkmanager ");
User: leaveiferror (socketserver. findprotocol (_ L ("btlinkmanager"), pinfo); Res = HR. Open (socketserver, kbtaddrfamily, pinfo. iprotocol); If (kerrnone! = Res ){
// If not success, this means Bluetooth is not on
Ret = efalse;
} Else
{
Ret = etrue; // Bluetooth is already on
HR. Close ();
} Cleanupstack: popanddestroy (1); return ret;
} Or rshareddataclient SD;
SD. Connect (0 );
SD. Assign (tuid: UID (0x10005952); in 3rd, you can do this: crepository * Cr = crepository: newlc (kcruid1_thpowerstate );
Tint value;
Cr-> get (kbtpowerstate, value );
If (value = ebtpoweron)
Apower = 1;
Cleanupstack: popanddestroy (CR ); Note: You must set the related permissions of 3rd API in IDE. Otherwise, you will get the error code-46, indicating that you are not authorized to call the get function.  Enable Bluetooth1. The rnotifier inotifier header file is displayed in the pop-up dialog box;
Tbtdeviceresponseparamspckg inotifierresult;
Tpckgbuf <tbool> pckg; in CPP: User: leaveiferror (inotifier. Connect ());
Inotifier. startnotifierandgetresponse (istatus,
Kpowermodesettingnotifieruid,
Pckg,
Inotifierresult); istate = eopeningbt;
Setactive (); 2. without confirmation from the user, the bteng In SDK 1.2 is automatically enabled. llib reverse engineering, and then use the cbtmcmsettings class // turn on Bluetooth in case it was offcbtmcmset?settings = cbtmcmset=:: newl (mbtmcmsettingscb *) null );
Settings-> setpowerstatel (etrue, efalse); // On, off is (efalse, efalse) in 3rd, there is also a way to enable Bluetooth theoretically, however, the test was not successful on the real machine due to insufficient permissions. Crepository * Cr = crepository: newlc (kcruid1_thpowerstate );
Tint err = Cr-> set (kbtpowerstate, ebtpoweron); in this case, the value of err on the real machine is-46, indicating that no permission is granted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.