OBEX - Object Exchange

If you want to transfer a file or other object using the standard service as used by Windows’ Wireless Link / Bluetooth File Transfer Wizard, Palm’s Beam, Nokia’s Send via Infrared, then use the OBEX protocol. On the client-side one uses class ObexWebRequest.

For Bluetooth one can use code like the following to send a file: (Note a failure is signalled by an exception).
Dim addr As BluetoothAddress = BluetoothAddress.Parse("002233445566")
Dim path As String = "HelloWorld.txt"
'
Dim req As New ObexWebRequest(addr, path)
req.ReadFile("Hello World.txt")
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse)
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode)
BluetoothAddress addr = BluetoothAddress.Parse("002233445566");
String path = "HelloWorld.txt";
//
var req = new ObexWebRequest(addr, path);
req.ReadFile("Hello World.txt");
ObexWebResponse rsp = (ObexWebResponse)req.GetResponse();
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode);

That constructor is only available in current repository code (it is not in 3.2) and also isn't available for other transports (TCP/IP, IrDA) so one has to create a Uri to provide the scheme, address, and path parameters. Thus use code like the following to send a file.
' The host part of the URI is the device address, e.g. IrDAAddress.ToString(),
' and the file part is the OBEX object name.
Dim addr As BluetoothAddress = ...
Dim addrStr As String = addr.ToString("N")
Dim uri As New Uri("obex://" & addrStr & "/HelloWorld.txt")
'
Dim req As New ObexWebRequest(uri)
req.ReadFile("Hello World.txt")
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse)
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode)

Or, to send locally generated content use something like the following.
' The host part of the URI is the device address, e.g. IrDAAddress.ToString(),
' and the file part is the OBEX object name.
Dim addr As String = "112233445566"
Dim uri As New Uri("obex://" & addr & "/HelloWorld2.txt")
'
Dim req As New ObexWebRequest(uri)
Using content As Stream = req.GetRequestStream()
   ' Using a StreamWriter to write text to the stream...
   Using wtr As New StreamWriter(content)
      wtr.WriteLine("Hello World GetRequestStream")
      wtr.WriteLine("Hello World GetRequestStream 2")
      wtr.Flush()
      ' Set the Length header value
      req.ContentLength = content.Length
   End Using
   ' In this case closing the StreamWriter also closed the Stream, but ...
End Using
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse) 
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode)

See also the ObexPushApplication and ObexPushVB sample programs.

The PUT operation is supported, and there is new support for GET, but there is no support for changing folders or getting a folder listing. In previous version there were some issue with handling file names that include non-English characters, and making connections to some device types failed. Both are fixed in the current version.

This class can only send one file, closing the connection after it does so. The Brecham.Obex library that we distribute can send more than one file without reconnecting. That depends on the peer server being allowing that too. See below for more information in this library.

Like the framework’s HttpWebResponse class etc, the ObexWebRequest signals an error by throwing a WebException and it includes the original error as the InnerException property. If the error occurred in the connect phase then the WebException.Status property will have value ConnectFailure and in the operation phase on the desktop CLR it will have value UnknownError.

The OBEX protocol is used to provide access to various types of service; in Bluetooth we have for instance Push (OPP), FTP, and PBAP “Phone Book Access” etc. Push normally allows PUT only and doesn’t require authentication whereas FTP allows general GET support and folder browsing but normally requires authentication. What service to connect to can be specified in the “scheme” of the URL passed to the ObexWebRequest constructor, see the class documentation.

Finally, note that the ObexWebRequest class itself makes the connection to the OBEX server, so don’t also call BluetoothClient.Connect(addr, BluetoothService.ObexObjectPush) directly or it will be blocked from doing so.

Behaviour From Servers
Server-side
One Active Server
Brecham.Obex

Last edited Mar 27, 2012 at 10:09 PM by alanjmcf, version 7

Comments

No comments yet.