quick_recipes (779892), страница 53
Текст из файла (страница 53)
You willusually use RFCOMM or L2CAP as Bluetooth transport protocols.4.9.2.3Create a Simple OBEX Server over BluetoothAmount of time required: 30 minutesLocation of example code: \Connectivity\ObexSrvApp296SYMBIAN C++ RECIPESRequired library(s): bluetooth.lib, btmanclient.lib,sdpagent.lib, sdpdatabase.lib, esock.libRequired header file(s): obex.h, btsdp.h, es_sock.hRequired platform security capability(s): LocalServicesProblem: You want to create a simple OBEX server communicating overBluetooth.Solution: In order to implement a basic OBEX Bluetooth server, you haveto perform a few steps even before you get to the actual OBEX code.
Thisinvolves setting up a Bluetooth subsystem ready to listen to incomingconnections. This is described below.1.Create a socket and open it:RSocketServ socketServ;socketServ.Connect();RSocket listen;listen.Open(socketServ, KRFCOMMDesC);2.Set up RFCOMM port number and Bluetooth security settings:TBTSockAddr addr;addr.SetPort(KRfcommPassiveAutoBind);TBTServiceSecurity serviceSecurity;serviceSecurity.SetUid(KUidServiceSDP);serviceSecurity.SetAuthentication(EFalse);serviceSecurity.SetEncryption(EFalse);serviceSecurity.SetAuthorisation(EFalse);serviceSecurity.SetDenied(EFalse);addr.SetSecurity(serviceSecurity);3.Set up, create and start OBEX server:TObexBluetoothProtocolInfo info;info.iAddr.SetPort(listen.LocalPort());info.iTransport = KObexRfcommProtocol;CObexServer *server = CObexServer::NewL(info)User::LeaveIfError(server->Start (this));Discussion: The parameter passed to the server’s Start() method isa pointer to an implementation of MObexServerNotify or MObexServerNotifyAsync (available only in Symbian OS v9.2 onwards).Typically, you will have an observer class which will be notified at anappropriate stage.
Here is the stripped down declaration of this mixinclass:CONNECTIVITY297class MObexServerNotify{public:virtual void ErrorIndication(TInt aError) =0;virtual void TransportUpIndication() =0;virtual void TransportDownIndication() =0;virtual void ObexConnectIndication(const TObexConnectInfo& aRemoteInfo,const TDesC8& aInfo) =0;virtual void ObexDisconnectIndication(const TDesC8& aInfo) =0;virtual void PutRequestIndication() =0;virtual TInt PutPacketIndication() =0;virtual void PutCompleteIndication() =0;virtual void GetRequestIndication(CObexBaseObject* aRequiredObject) =0;virtual TInt GetPacketIndication() =0;virtual void GetCompleteIndication() =0;virtual void SetPathIndication(const CObex::TSetPathInfo& aPathInfo,const TDesC8& aInfo) =0;virtual void AbortIndication() =0;virtual void CancelIndicationCallback() =0;};As you can see, it has dedicated methods for the status of OBEXtransport, PUT or GET operation arrivals, and so forth.
This is a placewhere you can handle all these events in terms of UI or other means.4.9.3 Advanced Recipes4.9.3.1Advertise Bluetooth ServicesAmount of time required: 30 minutesLocation of example code: \Connectivity\BTDeviceSample\ServiceAdvertiserRequired library(s): bluetooth.lib, btmanclient.lib,sdpagent.lib, sdpdatabase.lib, esock.libRequired header file(s): btmanclient.h, btdevice.h, bt_sock.h,btsdp.h, bttypes.h, es_sock.hRequired platform security capability(s): LocalServicesProblem: You want to advertise Bluetooth server services.Solution: After another Bluetooth device has found the listening Bluetoothserver, it is necessary to check whether this server actually provides therequired services. It would be useless if we connect to a device and thendiscover that it doesn’t even supply the service that we require.
The codelisted below shows how to add FTP to the service discovery protocoldatabase (refer to the comments within the code for a more detailedexplanation).298SYMBIAN C++ RECIPES// Assuming RSdp and RSdpDatabase object have been declared.// To make use of sdp the developer will need to include btsdp.h// as a header and sdpdatabase.lib as a library in the MMP file// Connect to the sdp objectUser::LeaveIfError(sdp.Connect());// Open database passing RSdp objectUser::LeaveIfError(sdpdb.Open(sdp));// Create a record handleTSdpServRecordHandle ftphandle;// Create new service, in this case we wish to create a new FTP record,// the UUIID for FTP is 0x1106, if the developer wish to use another// they should refer to the Bluetooth specification.sdpdb.CreateServiceRecordL(TUUID(0x1106), ftphandle);// Update the attribute of the FTP handle to add a textual description.// The 0x100 parameter in this function call is of type TSdpAttributeID,// 0x100 is the attribute ID for the service description// if the reader wishes to update any other attributes their// corresponding ID can be found at www.bluetooth.orgsdpdb.UpdateAttributeL(ftphandle, 0x100, _L8("File transfer server"));// New CSdpAttrValueDES objectprotDescList = CSdpAttrValueDES::NewDESL(0);// Add L2CAP, RFCOMM and OBEX to CSdpAttrValueDES object to be able to// use those protocols with FTPprotDescList->StartListL()->BuildDESL()->StartListL()->BuildUUIDL(TUUID(TUint16(KL2CAPUUID)))->EndListL()->BuildDESL()->StartListL()->BuildUUIDL(TUUID(TUint16(KRFCommUUID)))//set the port to port that RSocket (listen) was bound to->BuildUintL(TSdpIntBuf<TInt8>(listen.LocalPort()))->EndListL()->BuildDESL()->StartListL()->BuildUUIDL(TUUID(TUint16(KObexProtocolUUID)))->EndListL()->EndListL();// Attribute value of Protocol listconst TInt KProtocolDescList = 4;// Update database with CSdpAttrValueDESsdpdb.UpdateAttributeL(ftphandle, KProtocolDescList, *iProtDescList);delete protDescList;Discussion: The code listed in the solution may look a bit cryptic at firstglance, especially the part where an addition of services to the servicediscovery protocol database is taking place.
You can consider this technique in the same manner as stream classes in the Standard TemplateLibrary. Every method in a sequence of calls such as StartListL(),BuildDESL() and so forth returns a pointer to the MSdpElementBuilder interface. This process can be conveniently structured as youmight see above, so it is easy to follow and maintain at later stages.LOCATION-BASED SERVICES2994.10 Location-Based ServicesLocation-based services (LBS) offer the opportunity to extend applications,including, for example:• location-stamping artefacts such as photographs and notes,• finding nearby services and facilities,• navigation,• route recording.Location-based services are enabled by the use of global positioning.The global positioning system (GPS) is a global network of at least 24satellites orbiting Earth 24 hours a day.
It provides positioning, navigationand timing (PNT) services for military and civil needs and is available allover the world.GPS consists of three segments:• Satellite constellation – the group of satellites that send one-way datasignals, containing the satellite location and time.• Ground control and monitoring network – a number of control stationsthat provide path correction and other control.• Consumer GPS receiving equipment – devices that process the information received from the satellites.GPS relies on retrieving information from at least three satellites, andaffords an accuracy of location of approximately 2–10 meters.
GPSprovides 3D coordinates if there are more than three satellites available,so you can also learn the user’s current height. The distance to the satelliteis calculated by measuring the signal delivery time delay. A number ofEarth stations sending additional data (Differential GPS) can also supportGPS.The accuracy of location can depend on the weather, surroundingbuildings and a number of other factors. GPS has some major restrictions,such as limited coverage in urban environments and inside buildings dueto the requirement that mobile users be in ‘view’ of the satellites, and aslow location acquisition time (in the region of 10 to 60 seconds).On Symbian smartphones, the prime location technology is expectedto be integrated assisted GPS (A-GPS).
A-GPS systems overcome someof the limitations of GPS position acquisition by using the fixed mobilenetwork infrastructure to reduce acquisition time to less than 5 seconds,and may provide indoor accuracy to within 50 meters. However, as300SYMBIAN C++ RECIPESwith infrastructure time of arrival (TOA) schemes, multipath interferenceand the lack of a line of sight measurement can degrade performance,so A-GPS can be less accurate than GPS under certain conditions. Fornetwork operators, the main benefit from integration of A-GPS is theability to charge users for positioning information, because it uses thenetwork infrastructure.The Symbian OS LBS subsystem allows local and network-based applications to determine the current location of the handset.
The subsystemsupport integrated A-GPS hardware, but there is no specific support forother possible positioning modules (like external Bluetooth GPS devices,for example). The same universal API is used with any available modulessuch as A-GPS, Bluetooth GPS or network-based A-GPS.As the primary purpose of having GPS integrated on the phone isto allow the user to be located during an emergency, the architecturesupports the handling of network requests in low-memory conditions.This helps to ensure that these requests always succeed.LBS API available for third-party applications splits into the followingmain groups:• location information API• positioning technology module information API• positioning technology module selection criteria API.Let’s briefly outline the common terminology and these three APIgroups.
Further information is available in the Symbian Developer Librarydocumentation, found in the SDK or online on the Symbian DeveloperNetwork.The location information API allows you to obtain various positioning information (for instance, latitude and longitude values). Its use isdemonstrated in Recipe 4.10.2.1. The API requires a positioning technology module, which refers to the technique used to get the locationinformation.
You can retrieve the characteristics of that module usingthe positioning technology module information API. The LBS subsystemhas the default positioning module, which is used to perform locationrequests. You can either explicitly specify the required module or usethe module selection criteria API to specify the criteria the positioningmodule has to satisfy to be selected during the location request. TheLBS subsystem will try to identify all such modules and select the mostsuitable one.The following recipes discuss the LBS API and demonstrate how to useit to fulfill different programming tasks. The recipes mention and describein more detail a number of the various classes you would use duringthe development. Let us briefly overview them here in order to make therecipes easier to follow.LOCATION-BASED SERVICES301Client applications use the RPositionServer and RPositionerclasses as part of the location information API to get position updates.RPositionServer provides the means to manage a session with thelocation server and to get positioning technology module information.RPositioner manages a subsession with the location server andallows getting position updates.
The position information is returnedin the set of T classes, like TPositionInfo, TPosition, and soon. Depending on the positioning module capabilities, extended location information may be returned. This could include information aboutthe course and the satellite, and is provided using an extended set ofclasses: TPositionCourseInfo/TCourse and TPositionSatelliteInfo/TSatelliteData. Extended location information is discussed further in Recipe 4.10.2.2.The positioning technology module information API and the positioning technology module selection criteria API are represented by the setof T classes that describe different aspects of the LBS subsystem. Some ofthem are listed below:• TPositionModuleInfo• TPositionModuleStatus• TPositionModuleStatusEvent• TPositionCriteria• TPositionQuality• TPositionSelectionOrder.As you go through the recipes, you will see them in action, to makeit clearer how to use them.