Checking the Bluetooth power state in 3rd Ed
Article Metadata
Headers Required:
#include <centralrepository.h> // for CRepository
#include <BTServerSDKCRKeys.h> // for KCRUidBluetoothPowerState, KBTPowerState
#include <BtnotifierAPI.h> // for KPowerModeSettingNotifierUid
Library Required:
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();
}
Related Links
Note: The code has been taken from BTFileSend, an open source project in SourceForge.net.


(no comments yet)