00001
00002
00003
00004
00005
00006 #include <aknquerydialog.h>
00007 #include <utf.h>
00008 #include "Common.h"
00009 #include "BluetoothPMPExampleEngine.h"
00010 #include "DeviceDiscoverer.h"
00011 #include "ServiceAdvertiser.h"
00012 #include "ServiceDiscoverer.h"
00013 #include "Listener.h"
00014 #include "Connector.h"
00015 #include <BtPmpEx.rsg>
00016
00017 CBluetoothPMPExampleEngine* CBluetoothPMPExampleEngine::NewL(
00018 CBluetoothPMPExampleAppUi& aAppUi)
00019 {
00020 CBluetoothPMPExampleEngine* self =
00021 CBluetoothPMPExampleEngine::NewLC(aAppUi);
00022 CleanupStack::Pop(self);
00023 return self;
00024 }
00025
00026
00027 CBluetoothPMPExampleEngine* CBluetoothPMPExampleEngine::NewLC(
00028 CBluetoothPMPExampleAppUi& aAppUi)
00029 {
00030 CBluetoothPMPExampleEngine* self =
00031 new (ELeave) CBluetoothPMPExampleEngine(aAppUi);
00032 CleanupStack::PushL(self);
00033 self->ConstructL();
00034 return self;
00035 }
00036
00037
00038
00039 void CBluetoothPMPExampleEngine::ConstructL()
00040 {
00041
00042 User::LeaveIfError(iSocketServ.Connect());
00043
00044
00045 iListener = CListener::NewL(*this, iSocketServ);
00046
00047 iDeviceDiscoverer = CDeviceDiscoverer::NewL(iSocketServ, *this);
00048
00049 #ifdef ENABLE_LIAC
00050
00051 iLIAC = ETrue;
00052
00053 iDeviceDiscoverer->SetLIAC( iLIAC );
00054 #endif
00055
00056 iServiceAdvertiser = CServiceAdvertiser::NewL();
00057
00058 iServiceDiscoverer = CServiceDiscoverer::NewL(*this);
00059
00060
00061 for ( TInt idx=0; idx<KMaxConnectedDevices; idx++ )
00062 {
00063 iConnectedDevices[idx] = NULL;
00064 }
00065
00066 #ifdef ENABLE_LIAC
00067
00068
00069
00070 TInt attErr = iProperty.Define(KPropertyUidBluetoothControlCategory,
00071 KPropertyKeyBluetoothSetLimitedDiscoverableStatus,RProperty::EInt);
00072 if (attErr != KErrNone && attErr != KErrAlreadyExists)
00073 {
00074 User::Leave(attErr);
00075 }
00076
00077
00078
00079 SetLIAC(iLIAC);
00080 #endif
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090 CBluetoothPMPExampleEngine::CBluetoothPMPExampleEngine(
00091 CBluetoothPMPExampleAppUi& aAppUi):
00092 iIsSlave(EFalse),
00093 iAppUi(aAppUi)
00094 {
00095
00096 }
00097
00098
00099
00100
00101
00102
00103 CBluetoothPMPExampleEngine::~CBluetoothPMPExampleEngine()
00104 {
00105 #ifdef ENABLE_LIAC
00106 SetLIAC(EFalse);
00107 iProperty.Close();
00108 #endif
00109
00110
00111 DisconnectDevices();
00112
00113
00114 delete iServiceAdvertiser;
00115 iServiceAdvertiser=NULL;
00116
00117 delete iListener;
00118 iListener=NULL;
00119
00120 iDeviceDiscoverer->Cancel();
00121 delete iDeviceDiscoverer;
00122 iDeviceDiscoverer = NULL;
00123
00124 delete iServiceDiscoverer;
00125 iServiceDiscoverer=NULL;
00126
00127 iSocketServ.Close();
00128
00129
00130 iDevDataList.ResetAndDestroy();
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 void CBluetoothPMPExampleEngine::ShowMessageL(const TDesC& aMsg,
00143 TBool aDrawLine=EFalse)
00144 {
00145 if (iAppUi.Container())
00146 {
00147 if( aDrawLine )
00148 iAppUi.Container()->DrawLineL();
00149
00150 iAppUi.Container()->ShowMessageL(aMsg);
00151 }
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161 void CBluetoothPMPExampleEngine::DiscoverDevicesL()
00162 {
00163
00164 iStartTime.HomeTime();
00165 ShowMessageL(KDiscDevicesTxt, ETrue);
00166 TRAPD(err,iDeviceDiscoverer->DiscoverDevicesL(&iDevDataList));
00167 if (err)
00168 {
00169 HandleDeviceDiscoveryComplete(err);
00170 }
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 void CBluetoothPMPExampleEngine::DiscoverServicesL()
00180 {
00181 iStartTime.HomeTime();
00182 ShowMessageL(KDiscServicesTxt, ETrue);
00183 iServiceDiscoverer->DiscoverServicesL(&iDevDataList);
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193 void CBluetoothPMPExampleEngine::StartSlaveL()
00194 {
00195 if ( iIsSlave )
00196 return;
00197
00198 ShowMessageL(KSlaveInitTxt, ETrue);
00199
00200 TInt channel;
00201 iListener->StartListenerL(channel);
00202 TBuf<KThirty> msg;
00203 msg.AppendFormat(KListeningTxt, channel);
00204 ShowMessageL(msg, EFalse);
00205
00206 iServiceAdvertiser->StartAdvertiserL(channel);
00207 ShowMessageL(KSlaveInitCompTxt, EFalse);
00208 iIsSlave=ETrue;
00209 }
00210
00211
00212
00213
00214
00215
00216
00217 void CBluetoothPMPExampleEngine::StopSlaveL()
00218 {
00219 if ( !iIsSlave )
00220 return;
00221
00222 ShowMessageL(KNullDesC, ETrue);
00223
00224 iListener->StopListener();
00225 iServiceAdvertiser->StopAdvertiserL();
00226 iIsSlave=EFalse;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235 void CBluetoothPMPExampleEngine::DisconnectDevices()
00236 {
00237 for ( TInt idx=0; idx<KMaxConnectedDevices; idx++ )
00238 {
00239 if ( iConnectedDevices[idx] )
00240 {
00241 delete iConnectedDevices[idx];
00242 iConnectedDevices[idx]=NULL;
00243 }
00244 }
00245 ShowMessageL(KDisconnDevicesTxt, ETrue);
00246 iIsMaster = EFalse;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255 void CBluetoothPMPExampleEngine::ConnectDevicesL()
00256 {
00257
00258 DisconnectDevices();
00259
00260 iIsMaster = ETrue;
00261 ShowMessageL(KConnectingTxt, ETrue);
00262
00263 for ( TInt idx=0; idx<(iDevDataList.Count()); idx++ )
00264 {
00265 if ( iConnectedDeviceCount>=KMaxConnectedDevices )
00266 return;
00267
00268 TDeviceData *dev = iDevDataList[idx];
00269
00270
00271
00272 if ( dev->iDeviceServicePort>0 )
00273 {
00274 CConnector* connector = CConnector::NewLC(*this, iSocketServ);
00275 if ( (connector->ConnectL(dev->iDeviceName,
00276 dev->iDeviceAddr,
00277 dev->iDeviceServicePort))==KErrNone )
00278 {
00279 iConnectedDevices[iConnectedDeviceCount] = connector;
00280 iConnectedDeviceCount++;
00281 CleanupStack::Pop(connector);
00282 }
00283 else
00284 {
00285
00286 CleanupStack::PopAndDestroy();
00287 }
00288 }
00289 }
00290
00291 ShowConnectedDevicesL();
00292 }
00293
00294
00295
00296
00297
00298
00299 void CBluetoothPMPExampleEngine::ShowConnectedDevicesL()
00300 {
00301 TInt count=0;
00302 ShowMessageL(KConnDevicesTxt, ETrue);
00303 for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
00304 {
00305 if ( iConnectedDevices[idx] )
00306 {
00307 THostName name;
00308 name = iConnectedDevices[idx]->iName;
00309 name.Append(KNewLine);
00310 ShowMessageL(name, EFalse);
00311 count++;
00312 }
00313 }
00314 if ( count==0 )
00315 {
00316
00317
00318
00319 ShowMessageL(KNoConns, EFalse);
00320 }
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330 void CBluetoothPMPExampleEngine::SendMessageL()
00331 {
00332 iMsgtext.Zero();
00333 CAknTextQueryDialog* dlg = CAknTextQueryDialog::NewL(iMsgtext);
00334 if(!dlg->ExecuteLD(R_BLUETOOTHEXAMPLE_MESSAGEINPUT))
00335 {
00336
00337 return;
00338 }
00339
00340
00341 CnvUtfConverter::ConvertFromUnicodeToUtf8(iMsgtext8, iMsgtext);
00342
00343 TBuf<KEighty> msg;
00344 if ( iIsSlave )
00345 {
00346
00347 iListener->SendData(iMsgtext8);
00348 msg.Format(KFormatStr3, &iMsgtext);
00349 ShowMessageL(msg, EFalse);
00350 }
00351 else
00352 {
00353
00354 for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
00355 {
00356 if ( iConnectedDevices[idx])
00357 {
00358 iConnectedDevices[idx]->SendData(iMsgtext8);
00359 THostName name;
00360 name=iConnectedDevices[idx]->iName;
00361 msg.Format(KFormatStr2, &name, &iMsgtext);
00362 ShowMessageL(msg, EFalse);
00363 }
00364 }
00365 }
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375 void CBluetoothPMPExampleEngine::HandleListenerDataReceivedL(const TDesC& aData)
00376 {
00377
00378 TBuf<KEighty> msg;
00379 msg.Format(KFormatStr1, &aData);
00380 ShowMessageL(msg, EFalse);
00381 }
00382
00383
00384
00385
00386
00387
00388
00389 void CBluetoothPMPExampleEngine::HandleListenerConnectedL()
00390 {
00391 ShowMessageL(KConnMsg, ETrue);
00392 }
00393
00394
00395
00396
00397
00398
00399
00400 void CBluetoothPMPExampleEngine::HandleListenerDisconnectedL()
00401 {
00402 if ( !iIsSlave )
00403 return;
00404
00405
00406
00407 StopSlaveL();
00408 ShowMessageL(KDisconMsg, ETrue);
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 void CBluetoothPMPExampleEngine::HandleConnectorDataReceivedL(THostName aName,
00421 const TDesC& aData)
00422 {
00423
00424 TBuf<KEighty> msg;
00425 msg.Format(KFormatStr, &aName, &aData);
00426 ShowMessageL(msg, EFalse);
00427
00428
00429 _LIT8(KSeparator, ":");
00430
00431 TBuf8<KEighty> buf;
00432 TPtr8 msgtext8((TUint8*)buf.Ptr(), aData.Size()+KSeparator().Length() + aName.Size());
00433 CnvUtfConverter::ConvertFromUnicodeToUtf8(msgtext8, aData);
00434
00435
00436
00437 TBuf8<KEighty> bufName;
00438 TPtr8 name8((TUint8*)bufName.Ptr(), aName.Size());
00439 CnvUtfConverter::ConvertFromUnicodeToUtf8(name8, aName);
00440
00441
00442 msgtext8.Insert(0, KSeparator );
00443 msgtext8.Insert(0, name8);
00444
00445 for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
00446 {
00447 if ( iConnectedDevices[idx])
00448 {
00449 THostName name;
00450 name=iConnectedDevices[idx]->iName;
00451
00452
00453 if( name.Compare(aName) != 0)
00454 iConnectedDevices[idx]->SendData(msgtext8);
00455 }
00456 }
00457 }
00458
00459
00460
00461
00462
00463 void CBluetoothPMPExampleEngine::HandleConnectorErrorL(THostName aName, TInt aError)
00464 {
00465 if (aError)
00466 {
00467
00468 TBuf<KEighty> msg;
00469 msg.Format(KDeviceDisconMsg, &aName);
00470 ShowMessageL(msg, EFalse);
00471
00472 for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
00473 {
00474 if ( iConnectedDevices[idx])
00475 {
00476 THostName name;
00477 name=iConnectedDevices[idx]->iName;
00478
00479
00480 if( name.Compare(aName) == 0)
00481 {
00482 delete iConnectedDevices[idx];
00483 iConnectedDevices[idx] = NULL;
00484 }
00485 }
00486 }
00487
00488
00489 iIsMaster = EFalse;
00490 for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
00491 {
00492 if ( iConnectedDevices[idx])
00493 {
00494 iIsMaster = ETrue;
00495 break;
00496 }
00497 }
00498 }
00499 }
00500
00501
00502
00503
00504
00505
00506
00507 void CBluetoothPMPExampleEngine::HandleDeviceDiscoveryComplete(TInt aError)
00508 {
00509 if (aError)
00510 {
00511 iIsMaster = EFalse;
00512 }
00513 else
00514 {
00515 iIsMaster = ETrue;
00516 }
00517
00518 iEndTime.HomeTime();
00519
00520 TTimeIntervalSeconds seconds;
00521 iEndTime.SecondsFrom(iStartTime, seconds);
00522
00523 TInt time = seconds.Int();
00524 TBuf<KTwelve> temp = KTimeTxt();
00525 temp.AppendNum(time);
00526 temp.Append(KSecTxt);
00527 TRAPD(err,ShowMessageL(temp,EFalse));
00528
00529
00530 if ( iDevDataList.Count()> 0 )
00531 {
00532 TBuf<KTwenty> count = KFoundTxt();
00533 count.AppendNum( iDevDataList.Count() );
00534 count.Append( KDevices );
00535 TRAP(err,ShowMessageL(count));
00536 }
00537 else
00538 {
00539
00540 TRAP(err,ShowMessageL(KNoDevFound, EFalse));
00541 }
00542 }
00543
00544
00545
00546
00547
00548
00549
00550 void CBluetoothPMPExampleEngine::HandleServiceDiscoveryCompleteL()
00551 {
00552 iEndTime.HomeTime();
00553
00554 TTimeIntervalSeconds seconds;
00555 iEndTime.SecondsFrom(iStartTime, seconds);
00556
00557 TInt time = seconds.Int();
00558 TBuf<KTwelve> temp = KTimeTxt();
00559 temp.AppendNum(time);
00560 temp.Append(KSecTxt);
00561 ShowMessageL(temp,ETrue);
00562
00563 TInt count=0;
00564
00565 for ( TInt idx=0; idx<(iDevDataList.Count()); idx++ )
00566 {
00567 TDeviceData *dev = iDevDataList[idx];
00568 if ( dev->iDeviceServicePort>0 )
00569 {
00570 THostName name = dev->iDeviceName;
00571 name.Append(KNewLine);
00572 ShowMessageL(name, EFalse);
00573 count++;
00574 }
00575 }
00576 if ( count==0 )
00577 {
00578 ShowMessageL(KNoServiceFound, EFalse);
00579 }
00580 else
00581 {
00582 ShowMessageL(KServiceFound, EFalse);
00583 }
00584 }
00585
00586
00587
00588
00589
00590
00591
00592 TBool CBluetoothPMPExampleEngine::HasConnections()
00593 {
00594 TBool exists = EFalse;
00595 for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
00596 {
00597 if ( iConnectedDevices[idx])
00598 {
00599 exists = ETrue;
00600 break;
00601 }
00602 }
00603 return exists;
00604 }
00605
00606
00607
00608
00609
00610
00611
00612
00613 void CBluetoothPMPExampleEngine::DeviceDiscovered(const TDeviceData &aDevice)
00614 {
00615 TBuf<KForty> name = aDevice.iDeviceName;
00616 name.Trim();
00617
00618 if( name.Length() == 0 )
00619 name.Append(KDeviceWithNoName);
00620
00621 TRAPD(err,ShowMessageL(name, EFalse));
00622 }
00623
00624
00625
00626
00627 void CBluetoothPMPExampleEngine::ReportServiceDiscoveryErrorL(TInt aError)
00628 {
00629 TBuf<KThirty> discError = KServiceDiscoveryError();
00630 discError.AppendNum(aError);
00631 iAppUi.Container()->ShowMessageL(discError);
00632 }
00633
00634
00635
00636
00637
00638
00639 void CBluetoothPMPExampleEngine::TurnBtOnL()
00640 {
00641
00642
00643 const TUid KPowerModeSettingNotifierUid = {0x100059E2};
00644
00645
00646 RNotifier notifier;
00647 User::LeaveIfError( notifier.Connect() );
00648 TPckgBuf<TBool> dummy(ETrue);
00649 TPckgBuf<TBool> reply(EFalse);
00650 TRequestStatus stat;
00651 notifier.StartNotifierAndGetResponse(stat, KPowerModeSettingNotifierUid, dummy, reply);
00652 User::WaitForRequest(stat);
00653 notifier.CancelNotifier(KPowerModeSettingNotifierUid);
00654 notifier.Close();
00655 }
00656
00657 #ifdef ENABLE_LIAC
00658
00659
00660
00661 void CBluetoothPMPExampleEngine::SetLIAC( TBool aState )
00662 {
00663 TInt err = KErrNone;
00664
00665
00666 err = iProperty.Set(KPropertyUidBluetoothControlCategory,
00667 KPropertyKeyBluetoothSetLimitedDiscoverableStatus,
00668 aState);
00669
00670 TBuf<KEighty> myMessage;
00671
00672 myMessage.Zero();
00673
00674 if ( KErrNone == err )
00675 {
00676 if ( aState )
00677 {
00678 myMessage.Append( KLIACOn );
00679 }
00680 else
00681 {
00682 myMessage.Append( KLIACOff );
00683 }
00684 iLIAC = aState;
00685
00686 iDeviceDiscoverer->SetLIAC( iLIAC );
00687
00688 }
00689 else
00690 {
00691 myMessage.AppendFormat( KLIACError, err );
00692 }
00693
00694 TRAP( err, ShowMessageL( myMessage, ETrue ));
00695 }
00696
00697
00698
00699
00700 TBool CBluetoothPMPExampleEngine::LIAC()
00701 {
00702 return iLIAC;
00703 }
00704 #endif
00705
00706
00707
00708
00709 TBool CBluetoothPMPExampleEngine::IsDiscoveryActive()
00710 {
00711 return iDeviceDiscoverer->IsActive();
00712 }
00713
00714
00715
00716
00717 void CBluetoothPMPExampleEngine::StopDiscovery()
00718 {
00719 if ( IsDiscoveryActive() )
00720 {
00721 iDeviceDiscoverer->StopDiscovery();
00722 }
00723 iIsMaster = EFalse;
00724 }