×
Namespaces

Variants
Actions

让用户通过RNotifier选择蓝牙手机

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 18 Jul 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS000910


设备, 软件 版本: S60 3rd Edition, FP1


  • 详细描述

下列代码片段描述了S60 UI平台提供的蓝牙手机查找机制。当用户需要交互时我们可以通过使用RNotifier类来处理手机查找发现。通过使用TBTDeviceSelectionParams类我们可以限制搜索指定类型的手机。


下述代码通过自签名即可执行。


Contents

MMP

需要下列链接库

LIBRARY bluetooth.lib
LIBRARY btextnotifiers.lib
LIBRARY btdevice.lib


头文件

#ifndef BTDEVICEFINDER_H_
#define BTDEVICEFINDER_H_
 
#include <e32base.h>
#include <BTExtNotifiers.h> //TBTDeviceSelectionParamsPckg,TBTDeviceResponseParamsPckg
class MBTDeviceFinderNotify
{
public:
virtual void BTDeviceSelected() = 0;
};
 
class CBTDeviceFinder: public CActive
{
public:
static CBTDeviceFinder* NewL(const TInt aPriority,
MBTDeviceFinderNotify& aNotify);
~CBTDeviceFinder();
public:
void DiscoverDevicesL();
void SetSelectionParams(const TBTDeviceSelectionParams& aFilter);
const TBTDeviceResponseParams& ResponseParams();
protected:
void RunL();
void DoCancel();
private:
CBTDeviceFinder(const TInt aPriority,MBTDeviceFinderNotify& aNotify);
void ConstructL(void);
private:
RNotifier iSelectorNotifier;
MBTDeviceFinderNotify& iNotify;
TBTDeviceSelectionParamsPckg iSelectParamsBuf;
TBTDeviceResponseParamsPckg iResponseParamsBuf;
TBool iConnected;
};
#endif /*BTDEVICEFINDER_H_*/


源文件

#include "BTDeviceFinder.h"
 
CBTDeviceFinder::CBTDeviceFinder(const TInt aPriority,
MBTDeviceFinderNotify& aNotify)
:CActive(aPriority),iNotify(aNotify),iConnected(EFalse)
{
}
 
CBTDeviceFinder::~CBTDeviceFinder()
{
Cancel();
if (iConnected)
{
iSelectorNotifier.CancelNotifier( KDeviceSelectionNotifierUid );
iSelectorNotifier.Close();
}
}
 
CBTDeviceFinder* CBTDeviceFinder::NewL(const TInt aPriority,
MBTDeviceFinderNotify& aNotify)
{
CBTDeviceFinder* self = new (ELeave) CBTDeviceFinder(aPriority,aNotify);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop();
return self;
}
 
void CBTDeviceFinder::ConstructL(void)
{
CActiveScheduler::Add(this);
}
 
void CBTDeviceFinder::DoCancel()
{
iSelectorNotifier.CancelNotifier( KDeviceSelectionNotifierUid );
}
 
void CBTDeviceFinder::RunL()
{
//the user has selected some device
if(iStatus.Int() == KErrNone)
{
iNotify.BTDeviceSelected();
}
else //an error has occurred
{
User::Leave(iStatus.Int());
}
}
 
void CBTDeviceFinder::DiscoverDevicesL()
{
if (!iConnected)
{
User::LeaveIfError(iSelectorNotifier.Connect());
iConnected = ETrue;
}
 
iSelectorNotifier.StartNotifierAndGetResponse(iStatus,
KDeviceSelectionNotifierUid,
iSelectParamsBuf,
iResponseParamsBuf);
SetActive();
}
 
void CBTDeviceFinder::SetSelectionParams(const TBTDeviceSelectionParams& aFilter)
{
iSelectParamsBuf = aFilter;
}
 
const TBTDeviceResponseParams& CBTDeviceFinder::ResponseParams()
{
return iResponseParamsBuf();
}


使用CBTDeviceFinder类

  • 在头文件中有
class CBTDeviceFinder;
 
class CTestingAppUi : public CAknAppUi, MBTDeviceFinderNotify
{
public:
//...
//from MBTDeviceFinderNotify
void BTDeviceSelected();
//...
private:
//...
CBTDeviceFinder* iFinder;
};


  • 在源文件中有:
#include "btdevicefinder.h"
#include <btdevice.h> //TBTDeviceClass, TBTDeviceName
#include <bttypes.h> //TBTDevAddr
 
void CTestingAppUi::ConstructL()
{
CActive::TPriority priority(CActive::EPriorityUserInput);
iFinder = CBTDeviceFinder::NewL(priority, *this);
}
 
CTestingAppUi::~CTestingAppUi()
{
delete iFinder;
}
 
void CTestingAppUi::SelectDeviceL()
{
/* search only within a specific subset of devices
TBTDeviceSelectionParams selectionFilter;
TBTDeviceClass deviceFilter(EMajorServiceObjectTransfer, EMajorDevicePhone,
EMinorDevicePhoneUnclassified |
EMinorDevicePhoneCellular |
EMinorDevicePhoneCordless |
EMinorDevicePhoneSmartPhone |
EMinorDevicePhoneWiredModem |
EMinorDevicePhoneCommonISDNAccess );
 
selectionFilter.SetDeviceClass(deviceFilter);
iFinder->SetSelectionParams(selectionFilter);
*/
 
iFinder->DiscoverDevicesL();
}
 
void CTestingAppUi::BTDeviceSelected()
{
//Do something when BT device is selected
//e.g. get details of the device the user selected
//TBTDeviceResponseParams response = iFinder->ResponseParams();
//TBTDevAddr deviceAddress = response.BDAddr();
//TBTDeviceName deviceName = response.DeviceName();
//TBTDeviceClass deviceClass = response.DeviceClass();
}

后继条件

通过系统通知框架用户将看到蓝牙手机选择框的出现。

This page was last modified on 15 December 2011, at 08:53.
54 page views in the last 30 days.
×