📄 bluetoothpmpexampleengine.cpp
字号:
// or no devices are offering the requested service.
ShowMessageL(KNoConns, EFalse);
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::SendMessageL()
//
// send a message to all connected devices. user will be prompted to enter
// the message text.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::SendMessageL()
{
// prompt for text
TBuf<KTwenty> msgtext;
CAknTextQueryDialog * dlg = CAknTextQueryDialog::NewL(msgtext);
dlg->SetMaxLength(KTwenty);
dlg->SetPromptL(KMessage);
dlg->ExecuteLD(R_BLUETOOTHEXAMPLE_MESSAGEINPUT);
// explicitly convert from 16bit to 8bit character set
TBuf8<KTwenty> buf;
TPtr8 msgtext8((TUint8*)buf.Ptr(), msgtext.Size());
CnvUtfConverter::ConvertFromUnicodeToUtf8(msgtext8, msgtext);
TBuf<KEighty> msg;
if ( iIsSlave )
{
// slave sending data
iListener->SendDataL(msgtext8);
msg.Format(KFormatStr3, &msgtext);
ShowMessageL(msg, EFalse);
}
else
{
// master sending data
for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
{
if ( iConnectedDevices[idx])
{
iConnectedDevices[idx]->SendDataL(msgtext8);
THostName name;
name=iConnectedDevices[idx]->iName;
msg.Format(KFormatStr2, &name, &msgtext);
ShowMessageL(msg, EFalse);
}
}
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleListenerDataReceivedL(TDesC& aData)
//
// display data the slave listener receives from the master. this is a
// callback that CListener class will invoke when it receives new data.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleListenerDataReceivedL(const TDesC& aData)
{
// display received message
TBuf<KEighty> msg;
msg.Format(KFormatStr1, &aData);
ShowMessageL(msg, EFalse);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleListenerConnectedL()
//
// a callback received from CListener to indicate that it has been connected to
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleListenerConnectedL()
{
ShowMessageL(KConnMsg, ETrue);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleListenerDisconnectedL()
//
// a callback received from CListener to indicate that it has been disconnected
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleListenerDisconnectedL()
{
if ( !iIsSlave )
return;
// listener has already been stopped, request advertiser to stop as well
iServiceAdvertiser->StopAdvertiserL();
ShowMessageL(KDisconMsg, ETrue);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleConnectorDataReceivedL(THostName aName,
// TDesC& aData)
//
// display data the master receives from a connected slave. this is a
// callback that CConnector class will invoke when it receives data from slave.
// Also echo the message to other slaves.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleConnectorDataReceivedL(THostName aName,
const TDesC& aData)
{
// display received message
TBuf<KEighty> msg;
msg.Format(KFormatStr, &aName, &aData);
ShowMessageL(msg, EFalse);
// echo the message to other slaves
_LIT8(KSeparator, ":");
TBuf8<KEighty> buf; //should use HBufC so the size will be big enough
TPtr8 msgtext8((TUint8*)buf.Ptr(), aData.Size()+KSeparator().Length() + aName.Size());
CnvUtfConverter::ConvertFromUnicodeToUtf8(msgtext8, aData);
//convert name to UTF8 so other slaves see
//the sender name
TBuf8<KEighty> bufName;
TPtr8 name8((TUint8*)bufName.Ptr(), aName.Size());
CnvUtfConverter::ConvertFromUnicodeToUtf8(name8, aName);
//add the separator and name in the beginning;
msgtext8.Insert(0, KSeparator );
msgtext8.Insert(0, name8);
for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
{
if ( iConnectedDevices[idx])
{
THostName name;
name=iConnectedDevices[idx]->iName;
//echo to other slaves than the sender
if( name.Compare(aName) != 0)
iConnectedDevices[idx]->SendDataL(msgtext8);
}
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleDeviceDiscoveryCompleteL()
//
// a callback received from device discoverer to indicate that the device
// discovery has completed.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleDeviceDiscoveryCompleteL()
{
iEndTime.HomeTime();
TTimeIntervalSeconds seconds;
iEndTime.SecondsFrom(iStartTime, seconds);
TInt time = seconds.Int();
TBuf<KTwelve> temp = KTimeTxt();
temp.AppendNum(time);
temp.Append(KSecTxt);
ShowMessageL(temp,EFalse);
// iterate and display found devices, if any
if ( iDevDataList.Count()> 0 )
{
TBuf<KTwenty> count = KFoundTxt();
count.AppendNum( iDevDataList.Count() );
count.Append( KDevices );
ShowMessageL(count);
}
else
{
// no devices found
ShowMessageL(KNoDevFound, EFalse);
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleServiceDiscoveryCompleteL()
//
// a callback received from service discoverer to indicate that the service
// discovery has completed.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleServiceDiscoveryCompleteL()
{
iEndTime.HomeTime();
TTimeIntervalSeconds seconds;
iEndTime.SecondsFrom(iStartTime, seconds);
TInt time = seconds.Int();
TBuf<KTwelve> temp = KTimeTxt();
temp.AppendNum(time);
temp.Append(KSecTxt);
ShowMessageL(temp,ETrue);
TInt count=0;
// display devices with service we can use
for ( TInt idx=0; idx<(iDevDataList.Count()); idx++ )
{
TDeviceData *dev = iDevDataList[idx];
if ( dev->iDeviceServicePort>0 )
{
THostName name = dev->iDeviceName;
name.Append(KNewLine);
ShowMessageL(name, EFalse);
count++;
}
}
if ( count==0 )
{
ShowMessageL(KNoServiceFound, EFalse);
}
else
{
ShowMessageL(KServiceFound, EFalse);
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HasConnections()
//
// returns true if master has any connections to slave(s)
// ----------------------------------------------------------------------------
TBool CBluetoothPMPExampleEngine::HasConnections()
{
return ( iConnectedDeviceCount>0 );
}
//a callback to indicate that a device has been found
//main reason for this is that the UI can react so user
//knows that something is going on and the app is not "frozen"
void CBluetoothPMPExampleEngine::DeviceDiscoveredL(const TDeviceData &aDevice)
{
TBuf<KForty> name = aDevice.iDeviceName;
name.Trim();
if( name.Length() == 0 )
name.Append(KDeviceWithNoName);
ShowMessageL(name, EFalse);
}
void CBluetoothPMPExampleEngine::ReportServiceDiscoveryErrorL(TInt aError)
{
TBuf<KThirty> discError = KServiceDiscoveryError();
discError.AppendNum(aError);
iAppUi.Container().ShowMessageL(discError);
}
//Uses the Notifier API to ask the user to turn on Bluetooth
//if it's not on already.
void CBluetoothPMPExampleEngine::TurnBtOnL()
{
//the constant is from btnotifierapi.h which is not in all SDKs
//so it's hard coded here
const TUid KPowerModeSettingNotifierUid = {0x100059E2};
//const TUid KBTPowerStateNotifierUid = {0x101F808E}; //S80 and 7710
RNotifier notifier;
User::LeaveIfError( notifier.Connect() );
TPckgBuf<TBool> dummy(ETrue);
TPckgBuf<TBool> reply(EFalse);
TRequestStatus stat;
notifier.StartNotifierAndGetResponse(stat, KPowerModeSettingNotifierUid, dummy, reply);
User::WaitForRequest(stat);
notifier.CancelNotifier(KPowerModeSettingNotifierUid);
notifier.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -