1.OBEX客户端:
连接是由OBEX客户端向OBEX服务端发起的,下面的命令可以用来建立和释放这个连接。
Connect方法:在OBEX层上由客户端发起向服务器端请求建立一个连接。在这之前可以先调用IObex::EnumDevices方法,得到指向OBEX服务器设备的指针。 每一个设备都向外界提供了一个Connect的接口。如果这个设备没有提供OBEX服务,则发向这个设备的Connect请求会失败。用户看不到传输的详细信息,只有name和address是可见的。
Disconnect方法: 释放客户和服务器端的连接和所占用的资。
当一个连接成功建立后,用户可以使用下面的方法:
Put方法: 从当地向服务器发送数据。
Get方法:如果服务器提供了这个方法,就可以从服务器上得到数据。
Abort方法:中断PUT和GET方法。
SetPath方法:设置在服务器上浏览的当前路径,和在服务器上创建路径。
下面是几个方法使用的例子。见MSDN。
Connecting to an OBEX Server
Disconnecting from an OBEX Server
Requesting a Data Object from the OBEX Server
Sending a Data Object to the OBEX Server
Aborting a Request
Setting the Target Folder Path on the OBEX Server
See Also
Device Discovery
Object Exchange Interfaces
Connection Points
Use of Streams
Object Exchange Protocol
1.1Device Discovery设备发现
设备发现可以让客户端发现进入范围内其它的有OBEX服务的设备,调用IObex::EnumDevices 方法可以得到这些设备的列表。还有一种方法是应用IConnectionPointContainer和IObexSink 。然后在IObexSink中接收设备发现的通知。具体做法如下:
IObex->QueryInterface 查询得到IConnectionPointContainer (pCPContainer) 的指针。
pCPContainer->FindConnectionPoint(&pCP)
IObexSink pSink;
pCP->Advise(pSink) IObexSink
pObex->StartDeviceEnum
代码如下:
IObex* m_pObex;
CObexSink m_ObexSink;
IConnectionPointContainer *m_pCont;
IConnectionPoint *m_pConPt;
DWORD m_dwConnectCookie;
if(!SUCCEEDED(m_pObex->QueryInterface(IID_IConnectionPointContainer, (LPVOID *)&m_pCont)))
return FALSE;
if(!(SUCCEEDED(m_pCont->FindConnectionPoint(IID_IObexSink, &m_pConPt))))
return FALSE;
if(!(SUCCEEDED(m_pConPt->Advise((IUnknown *)(&m_ObexSink), &m_dwConnectCookie))))
return FALSE;
其中class CObexSink : public IObexSink应该也可以直接声明一个IObexSink。在CObexSink中重写了
Notify(OBEX_EVENT Event, IUnknown * pUnk1, IUnknown * pUnk2)。
Device Capability设备的能力:
设备能力是OBEX服务器所提供服务的一个提示。当OBEX服务器被客户端发现时,传输层组装所提供的服务的标志到字节域中。然后客户端可以根据这些标志位来判断服务器提供了哪些服务。这些标志并不是意味着服务器提供了这些服务,这只是一个提示。为了验证服务,客户端要向服务器端发送一个连接请求,如果服务器端没出息提供所要求连接的服务,则连接会失败。
通常情况下会提供以下的服务:Push,File Browsing ,Synchronization。
对于红外设备,则是连接线外端口。
对于蓝牙设备,则是提示了远端服务器的一个标识符。
1.2Object Exchange Interfaces对象交换接口
对象交换接口可以让应用程序通过OBEX服务协议来交换数据。WINCE提供了对OBEX的支持,你可以在两个配对的设备上运行多个OBEX服务程序。
1.3Connection Points连接点
OBEX支持连接点的用户通告。连接点运用了标准的COM方法在对象上定义了一个回调函数。服务器和客户端通常不调用回调函数,这样连接请求和连接释放时就会得不到消息。服务器和客户端都是通过这个方法完成了回调。
1.4 Use of Streams
obex函数在发送和接收OBEX头Body和EndOfBody头时要把这些数据压到一个对象中。BODY部分则是通过一个模块化的COM的IStream 接口。在文件传输时,调用OpenStreamOnFile函数获得这个文件的stream接口。然后就可以用来COPY从OBEXAPI得到的stream。发送数据,可以调用IStream 的Read和Write方法。大多情况下,OBEX层都会把数据压到Body和EndOfBody头中。
1.5 Connecting to an OBEX Server
connect请求是可选的,如果在调用不着Get和Put前没有调用Connect则会先发送一个255字节的数据包。
CONNEXT:调用IObexDevice::Connect()服务器然后回应一个属性包。
发送的数据包的长度取决于所包含的请求头的数目和大小。
在发送CONNECT请求时可以使用Target头,用来指定服务。如果没有指定则为inbox服务。
Authenticate Challenge is used by the client when authenticating with a server.
Authenticate Response is sent by the client when responding to a server challenge.
The OBEX version is 1.2 and the flags are always set to 0.
下面的用在CONNECT回应包中:
Who contains the same information specified in the request target header.
Authenticate Challenge is sent by the server when a client is authenticating.
Authenticate Response is sent by the server to indicate a successful challenge/response.
A successful response to the Connect request is a 0xA0 response code, followed by the ConnectionId field and any other optional fields. If another code is received, it indicates a failure to make the connection. A fail response includes the version, flags, and packet size information, and a description header, if applicable.
OBEX和蓝牙开发
最新推荐文章于 2025-05-20 15:40:36 发布