检测蓝牙是否开启
这个是比较经典的方法
#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();
}
下面的是另外的方法::
在3rd之前的版本:
TBool GetBluetoothOn() // BTONOFF
{
// Create and initialise an RHostResolver
RHostResolver hr;
TInt res = false;
TBool ret;
{
// Create and initialise an RHostResolver
RHostResolver hr;
TInt res = false;
TBool ret;
RSocketServ socketServer;
User::LeaveIfError(socketServer.Connect());
CleanupClosePushL(socketServer);
User::LeaveIfError(socketServer.Connect());
CleanupClosePushL(socketServer);
TProtocolDesc pInfo;
//_LIT(KL2Cap, "BTLinkManager");
User::LeaveIfError(socketServer.FindProtocol(_L("BTLinkManager"),pInfo));
//_LIT(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;
}
// if not success, this means bluetooth is not on
ret = EFalse;
}
else
{
ret = ETrue; // Bluetooth is already ON
hr.Close();
}
{
ret = ETrue; // Bluetooth is already ON
hr.Close();
}
CleanupStack::PopAndDestroy(1);
return ret;
}
}
或者
RSharedDataClient sd;
sd.Connect(0);
sd.Assign( TUid::Uid( 0x10005952 ) );
sd.Connect(0);
sd.Assign( TUid::Uid( 0x10005952 ) );
在3rd里可以这样做:
CRepository* cr = CRepository::NewLC( KCRUidBluetoothPowerState );
TInt value;
cr->Get( KBTPowerState, value );
if(value==EBTPowerOn)
aPower=1;
CleanupStack::PopAndDestroy( cr );
TInt value;
cr->Get( KBTPowerState, value );
if(value==EBTPowerOn)
aPower=1;
CleanupStack::PopAndDestroy( cr );
注意:一定要在IDE中设置好3rd API的相关权限,否则会得到-46的错误代码,表示没有权限调用Get函数。
开启蓝牙
1. 弹出用户确认对话框的方式
头文件中:
RNotifier iNotifier;
TBTDeviceResponseParamsPckg iNotifierResult;
TPckgBuf<TBool> pckg;
TBTDeviceResponseParamsPckg iNotifierResult;
TPckgBuf<TBool> pckg;
CPP中:
User::LeaveIfError( iNotifier.Connect() );
iNotifier.StartNotifierAndGetResponse( iStatus,
KPowerModeSettingNotifierUid,
pckg,
iNotifierResult );
iNotifier.StartNotifierAndGetResponse( iStatus,
KPowerModeSettingNotifierUid,
pckg,
iNotifierResult );
iState = EOpeningBT;
SetActive();
SetActive();
2.不经过用户确认,自动打开蓝牙
对SDK 1.2中的bteng.llib进行reverse engineering,然后使用其中的CBTMCMSettings类
// turn on Bluetooth in case it was off
CBTMCMSettings settings= CBTMCMSettings::NewL( (MBTMCMSettingsCB*)NULL );
settings->SetPowerStateL(ETrue,EFalse ); // on, off is (EFalse,EFalse)
settings->SetPowerStateL(ETrue,EFalse ); // on, off is (EFalse,EFalse)
在3rd中,还有一种办法,理论上也可以开启蓝牙,但由于权限不够,在真机上没有测试成功。
CRepository* cr = CRepository::NewLC( KCRUidBluetoothPowerState );
TInt err = cr->Set(KBTPowerState,EBTPowerOn);
TInt err = cr->Set(KBTPowerState,EBTPowerOn);
此时在真机上err的值为-46表示没有权限。