⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bluetoothtransport.h

📁 Symbian OS C++ for Mobile Phones Volume 3 源码
💻 H
字号:
// Copyright (c) 2004 - 2007, Symbian Software Ltd. All rights reserved.


#ifndef BLUETOOTHTRANSPORT_H
#define BLUETOOTHTRANSPORT_H

#include <bt_sock.h>
#include <btsdp.h>
#include <btextnotifiers.h>

#include "transport.h"


class CBluetoothTransport : public CTransport, public MBluetoothSocketNotifier
/**
	Exchanges payloads over an L2CAP or an RFCOMM connection.  Uses a
	socket-based (CBluetoothSocket) interface.
 */
	{
public:
	static CBluetoothTransport* NewL(TAny* aTransportCreateInfo);

protected:
	CBluetoothTransport(MTransportObserver& aObserver);
	void ConnectToSocketServerL();
	
	virtual ~CBluetoothTransport();
	void FreeDataSocket();

private:
	static CBluetoothTransport* New2L(MTransportObserver& aObserver, const TDesC& aProtocolName, TBool aInitListen);

	// implement CActive
	virtual void DoCancel();

	// implement CTransport
	virtual void DoSendPayloadL();
	virtual void DoLaunchRead();
	
	// implement MBluetoothSocketNotifier
	virtual void HandleShutdownCompleteL(TInt aErr);
	virtual void HandleSendCompleteL(TInt aErr);
	virtual void HandleReceiveCompleteL(TInt aErr);
	virtual void HandleIoctlCompleteL(TInt aErr);
	virtual void HandleActivateBasebandEventNotifierCompleteL(TInt aErr, TBTBasebandEventNotification& aEventNotification);
	virtual void HandleAcceptCompleteL(TInt aErr);
	virtual void HandleConnectCompleteL(TInt aErr);

protected:
	/** The OandX service identifier. */
	const TUUID iServiceUuid;
	/**
		Holds a narrow version of the payload text which was sent;
		stores the received payload.
	 */
	TBuf8<KPayloadLen> iPayload8;
	/** Session to socket server, required to load BT protocols. */
	RSocketServ iSocketServ;
	/** Connection to remote device. */
	CBluetoothSocket* iBtSocket;
	};


class CBtServerToClient : public CBluetoothTransport
/**
	Implements connection from a Bluetooth server device to a client.
	This class manages reserving an L2CAP or RFCOMM channel; advertising
	the service; and accepting the incoming connection.
 */
	{
public:
	static CBtServerToClient* NewL(MTransportObserver& aObserver, const TDesC& aProtocolName);
	virtual ~CBtServerToClient();

private:
	CBtServerToClient(MTransportObserver& aObserver);
	void ConstructL(const TDesC& aProtocolName);
	
	TInt CreateListenChannelL(const TDesC& aProtocolName);
	void BuildSdpRecordL(TUint aProtocol, TInt aChannel);
	CSdpAttrValueDES* BuildL2CapProtocolDescriptorListLC(TInt aChannel);
	CSdpAttrValueDES* BuildRfcommProtocolDescriptorListLC(TInt aChannel);
	
	// partially implement MBluetoothSocketNotifier, override CBluetoothTransport
	virtual void HandleAcceptCompleteL(TInt aErr);

private:
	/** Listen socket assigns incoming connection to iBtSocket, which is the accept socket. */
	CBluetoothSocket* iListenSocket;
	/** Sesion to SDP database. */
	RSdp iSdp;
	/** Subsession to SDP database, used to construct record. */
	RSdpDatabase iSdpDb;
	/** OandX service record's handle.  Identifies record in SDP database. */
	TSdpServRecordHandle iServRecHandle;
	/** Error which occurred when accepting incoming connection. */
	TInt iAcceptError;
	};


class CBtClientToServer : public CBluetoothTransport, public MSdpAgentNotifier, public MSdpAttributeValueVisitor
/**
	Implements connection from a Bluetooth client to a server.
	This class asks the user to select a device; searches for the
	service; and connects to the server.
 */
	{
public:
	static CBtClientToServer* NewL(MTransportObserver& aObserver);
	virtual ~CBtClientToServer();

private:
	CBtClientToServer(MTransportObserver& aObserver);
	void ConstructL();

	void AskUserToSelectHostL(TBTDevAddr& aDevAddr);
	void FindProtocolDescriptionL(const TBTDevAddr& aDevAddr);
	void FinishedSearching(TInt aError);

	// implement MSdpAgentNotifier
	virtual void NextRecordRequestComplete(TInt aError, TSdpServRecordHandle aHandle, TInt aTotalRecordsCount);
	virtual void AttributeRequestResult(TSdpServRecordHandle aHandle, TSdpAttributeID aAttrID, CSdpAttrValue *aAttrValue);
	virtual void AttributeRequestComplete(TSdpServRecordHandle aHandle, TInt aError);

	// implement MSdpAttributeValueVisitor
	virtual void VisitAttributeValueL(CSdpAttrValue& aValue, TSdpElementType aType);
	virtual void StartListL(CSdpAttrValueList& aList);
	virtual void EndListL();

	// partially implement MBluetoothSocketNotifier, override CBluetoothTransport
	virtual void HandleConnectCompleteL(TInt aErr);

private:
	/** Agent used to search for OandX service on remote device. */
	CSdpAgent* iSdpAgent;
	/** Error which occurred when searching for OandX service. */
	TInt iSdpError;
	/** Protocol used to connect to remote device - "RFCOMM" or "KL2CAP". */
	const TDesC* iProtocolName;
	/** Channel (PSM or RFCOMM channel) used to connect to remote device. */
	TInt iProtocolChannel;
	/** Error which occured when connecting to remote service. */
	TInt iConnectError;
	};


// -------- SDP record --------

/** Identifies OandX service.  The final 96 bits of the Symbian UUID are appended to this value. */
const TUid KOandXServiceUid = {0xE04E4143};

/** OandX service name, as advertised by SDP. */
_LIT(KOandXServiceName, "OandX");

/** OandX descriptive name, as advertised by SDP. */
_LIT(KOandXServiceDesc, "OandX service");


// -------- debugging --------

#ifdef _DEBUG

enum TBttPanicReason
	{
	ETransHacUnexpected,
	ETraceHccUnexpected,
	ESvToClCtlUnrecProto
	};

void BttPanic(TBttPanicReason aReason);

#endif	// #ifdef _DEBUG

#define __BTT_ASSERT(___cond, ___reason)	__ASSERT_DEBUG(___cond, BttPanic(___reason))

#endif	// #ifndef BLUETOOTHTRANSPORT_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -