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