examples/sfexamples/RockPaperScissorsGameSourceCode_S60/RPS/src/BluetoothDeviceDiscoverer.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */ 
00029 
00030 
00031 // INCLUDE FILES
00032 #include <btextnotifiers.h>
00033 #include "BluetoothDeviceDiscoverer.h"
00034 #include "common.hrh"
00035 /*
00036 ============================================================================
00037 CBluetoothDeviceDiscoverer's two stage constructor
00038 ============================================================================
00039 */
00040 CBluetoothDeviceDiscoverer* CBluetoothDeviceDiscoverer::NewL(MBluetoothDeviceDiscovererObserver& aObs)
00041         {
00042     CBluetoothDeviceDiscoverer* self = new (ELeave) CBluetoothDeviceDiscoverer(aObs);
00043     CleanupStack::PushL(self);
00044     self->ConstructL();
00045     CleanupStack::Pop();
00046     return self;
00047         }
00048 
00049 /*
00050 ============================================================================
00051 CBluetoothDeviceDiscoverer's second phase constructor 
00052 ============================================================================
00053 */
00054 void CBluetoothDeviceDiscoverer::ConstructL()
00055         {
00056         //Create an RNotifier object, and connect it with RNotifier::Connect()
00057         User::LeaveIfError(iBTDevicesNotifier.Connect());       
00058         }
00059 
00060 /*
00061 ============================================================================
00062 CBluetoothDeviceDiscoverer's constructor 
00063 ============================================================================
00064 */
00065 CBluetoothDeviceDiscoverer::CBluetoothDeviceDiscoverer(MBluetoothDeviceDiscovererObserver& aObs) :
00066         CActive(CActive::EPriorityStandard), iObserver(aObs)
00067         {
00068         CActiveScheduler::Add(this);
00069         }
00070 
00071 /*
00072 ============================================================================
00073 CBluetoothDeviceDiscoverer's destructor
00074 ============================================================================
00075 */
00076 CBluetoothDeviceDiscoverer::~CBluetoothDeviceDiscoverer()
00077         {
00078         Cancel();
00079         //After using a RNotifier object we need to close it.   
00080         iBTDevicesNotifier.Close();
00081         }
00082         
00083 /*
00084 ============================================================================
00085 DoCancel is called as part of the active object's Cancel().
00086 Cancel the notifier.
00087 ============================================================================
00088 */
00089 void CBluetoothDeviceDiscoverer::DoCancel()
00090         {
00091         //If the user dismisses the RNotifier's dialog without selecting any devices then we call
00092         //RNotifier::CancelNotifier with the same dialog UID (KDeviceSelectionNotifierUid) we used
00093         //in RNotifier::StartNotifierAndGetResponse.
00094         iBTDevicesNotifier.CancelNotifier(KDeviceSelectionNotifierUid);
00095         }
00096 
00097 
00098 /*
00099 ============================================================================
00100 Displays the BT devices in range for the player to choose from
00101 ============================================================================
00102 */
00103 void CBluetoothDeviceDiscoverer::DiscoverAndSelectDeviceL(const TBTDeviceSelectionParamsPckg& aSelectionFilter)
00104         {
00105         if(IsActive())
00106                 {       
00107                 User::Leave(KErrInUse);
00108                 }
00109         else
00110                 {               
00111                 //We ask RNotifier to start the search for other BT devices in range. Seraching for BT devices is done
00112                 //asynchronously by calling RNotifier::StartNotifierAndGetResponse. Application can filter the search
00113                 //of BT devices by passing a TBTDeviceSelectionParams wrapped in a TBTDeviceSelectionParamsPckg
00114                 //pckg buffer. In our example we passed the UID of the authorisation plug-in (KDeviceSelectionNotifierUid)
00115                 //that invokes the BT device selection dialog, the device and/or service classes by which to filter
00116                 //suitable remote devices (RPS's service class KRPS_BTServiceID), a Bluetooth address object
00117                 //that on completion of the dialog will hold information about the device the user selected in
00118                 //a packaged TBTDeviceResponseParams.
00119                 iBTDevicesNotifier.StartNotifierAndGetResponse(iStatus, KDeviceSelectionNotifierUid, aSelectionFilter, iResponse);
00120                 SetActive();
00121                 }
00122         }
00123 
00124 /*
00125 ============================================================================
00126 Handles CBluetoothConnector's completion events
00127 ============================================================================
00128 */
00129 void CBluetoothDeviceDiscoverer::RunL()
00130         {
00131         //We pass on to the engine the BT device address the user selected otherwise we pass on the error
00132         if(iStatus.Int() == KErrNone)
00133                 {
00134                 iObserver.OnDeviceDiscoveryComplete(iResponse);
00135                 }
00136         else
00137                 {       
00138                 iObserver.OnDeviceDiscoveryErr(iStatus.Int());  
00139                 }
00140         }
00141 
00142 /*
00143 ============================================================================
00144 Handles a leave occurring in the request completion event handler RunL().
00145 Reports the error to the observer
00146 ============================================================================
00147 */
00148 TInt CBluetoothDeviceDiscoverer::RunError(TInt aError)
00149         {
00150         iObserver.OnDeviceDiscoveryErr(aError);
00151         return KErrNone;
00152         }

Generated by  doxygen 1.6.2