OBEX and programming techniques for infrared ports, Bluetooth
We live in a world where wireless devices gradually creep into the life of every family. From beautiful mobile phones, multi-purpose personal support devices (Pocket PC, Palm .) to home entertainment devices such as speaker systems, DVD players, televisions, all Both are connected together without a cord. If you've ever asked: " Can you program to transmit image and sound data to your mobile phone via infrared or Bluetooth? " Then this article will help you figure out how and how to solve the problem. on.
WHAT IS OBEX?
OBEX (OBject EXchange) is a data exchange protocol between infrared devices that was first introduced by IrDA (Infrared Data Association) in 1997. Initially, this protocol was limited to devices only. using infrared light environment, but very soon it was put on most Bluetooth devices by Bluetooth Special Interest Group (Bluetooth SIG).
1. Location OBEX in OSI model
Like other protocols, OBEX protocol is built on the OSI (Open Systems Interconnection) model which consists of two main components:
• OBEX session protocol (OBEX session protocol) : describes the packet structure in the session between two devices.
• OBEX application framework : set of OBEX services provided for terminal applications such as file transfer, photo printing .
Oh
Application floor OBEX Framework OBEX Session Floor session Presentation stage Tiny TP RFCOMM IrLMP transport floor L2CAP IrLAP Link network floorManager Floor links IrPHY Baseband IrDA Bluetooth OSI physical floor
Figure 1: OBEX protocol in OSI model
2. Packet structure in OBEX session protocol
OBEX protocol is mainly used in "push" (Push) or "pull" (Pull) applications, allowing clients to "push" data to servers (servers) or "pull" data. Data from server down. To do this, packets exchanged between the client and server must strictly follow the proposed structure. Here are some of the structures used during file transfer between the client and server (please refer to IrOBEX1.3 documentation on http://www.hitekgroup.net website).
2.1 Required packets
All required packets have the following structure:
Byte 0
Byte 1, 2 Bytes 3 to nopcode
packet length
HeadersOpcode : The code for each request (Table 1). The highest bit is called the Final bit.
Packet length : Length of the packet
Header : The first information has the following structure:
Table 1 : Command code required Command code Type
Describe
0x80 CONNECT Setting up the session 0x81 DISCONNECT Stop the session 0x02 (0x82) PUT Sending data to the server 0x03 (0x83) GET Retrieving data from server 0xFF ABORT Cancel the sessionTable 2 : First information Identifier Name Description 0x01 NAME File name (Unicode code) 0xC3 LENGTH File size in bytes 0x48 BODY Data segment of file 0x49 END OF BODY The last piece of data of the file
2.2 Package reply
Like the required packet, the reply packet has the following structure:
Some response opcode are common:
Table 3 : Answer code Answer code Description 0x10 (0x90) Resume request or reply 0x20 (0xA0) Confirm request or answer 0x40 (0xC0) Request error 0x41 (0xC1) Error due to no rights 0x43 (0xC3) Transaction session cancel 0x44 (0xC4) No file found3. Let's solve it together
The file exchange process between client and server is divided into 3 phases:
• Set up session : CONNECT
• Receive / send file : GET / PUT
• Stop the session : DISCONNECT
3.1 Setting up the session (CONNECT)
CONNECT packet has the following structure:
OBEX version number : The OBEX protocol version includes major number stored in 4 high bits, the minor number stored at 4 low bits. The current version is 1.0, so this value is 0x10.
Flags : This value is always 0x00 in the current version.
Maximum OBEX packet length : The maximum value of the packet in the OBEX protocol that the device can receive or send. This value on the client and server may vary. Therefore, when setting up the session, the client needs to send this value to the server to check if the maximum packet size that the server can receive or send is?
Optional headers : The first information is optional according to the purpose of each session. In the example below, this value can be ignored.
Answer from server Answer code 0xA0
SUCCESS
0x0007 Packet length = 7 bytes 0x10 OBEX version 1.0 0x00 Flags for the current version 0x0200 The maximum size of the packet that the server can receive or send is 512 bytes3.2 Send file (PUT)
Unlike CONNECT packets, PUT packets have some additional early information:
NAME : File name information
LENGTH : Information about file size
BODY : File data segment
END OF BODY : The last piece of data for the file
Below is an example of sending a hello.gif file that is 721 bytes in size from the client (PC) to the server (mobile phone). Because the file size is larger than the largest packet size the server can receive (with Sony Ericsson T610 being 512 bytes), the client will split the file into two packets to send. Package 1 has a PUT request code of 0x02 (no Final bit set). Pack 2 has a PUT request code of 0x82 (Final bit setting).
3.3 Receive file (GET)
Unlike PUT packets, GET packets only have the first information NAME. The following example describes the process of receiving a hello.gif file that is 721 bytes in size from the server. First, the client (PC) will send a GET request to the server with the first information NAME is the file name. Because the file size requires larger than 512 bytes (with Sony Ericsson T610), the first answer packet has a reply code of 0x90 (CONTINUE) along with a data section of the file. When receiving the answer code CONTINUE, the client knows that this is not the final piece of the file, so continue sending the request (no first information required from NAME) until the answer code is 0xA0 (SUCCESS) .
3.4 Termination (DISCONNECT)
To end the session, the client needs to send the DISCONNECT packet to the server.
OBEX in mobile phones
Today, mobile phones are not only a means of communication but also an entertainment device with many functions such as listening to music, taking photos, playing games. This means that users always need to update good music, favorite games or save memorable moments on their phones. Below I would like to introduce the two most common types of connections on most newer mobile phones today are infrared and Bluetooth; at the same time, instructing how to program data exchange between PC and mobile phone using OBEX protocol through these two types of connections.
1. Connect via infrared with C #
OBEX and programming techniques for infrared ports, Bluetooth Picture 1
The IrDA protocol was first introduced by the IrDA association in 1994 with the aim of enhancing wireless connectivity between devices via infrared light. With an operating range of up to 1 m, an opening angle of between 15 and 30 degrees, the speed can reach 4Mbps, the infrared port is quickly included in most wireless devices such as cell phones, PDAs .
Previously, programming with infrared was a barrier for those unfamiliar with Windows' API (Application Programming Interface), and today, with the version of .NET 2.0, Microsoft has included the This framework is an IrDA library class, allowing programmers to write code more easily and quickly. Like the TCP / IP protocol, the client can establish an IrDA connection to the server by specifying the server's address (similar to the IP address) and the service name on the server (similar to TCP Port).
Each mobile phone has a unique address corresponding to the infrared port on it. The following code allows defining this address:
using System.Net.Sockets;
void Form1_Load (object sender, EventArgs e)
{
/ * Initializing the client * /
IrDAClient irClient = new IrDAClient ();
/ * Search up to 2 devices * /
IrDADeviceInfo [] irDevices = irClient.DiscoverDevices (2);
/ * Print the message when no device is found * /
if (irDevices.Length == 0) {
Console.WriteLine ("No infrared device found");
}
else {
/ * Print the name and address of each device found * /
for (int i = 0; i
Console.WriteLine ("Device Name: {0}", irDevices [i] .DeviceName);
Console.WriteLine ("Device ID: {0}", irDevices [i] .DeviceID);
}
}
}
Built-in IrDA services in mobile phones can vary from manufacturer to manufacturer. However, most mobile phones currently offer two main services: IrDA: IrCOMM and IrDA: OBEX. In this article, I only mention IrDA: OBEX service, which allows PC and mobile phones to exchange data via OBEX protocol. Connecting to this service is done through the following code:
OBEX and programming techniques for infrared ports, Bluetooth Picture 2 using System.Net;
using System.Net.Sockets;
void Form1_Load (object sender, EventArgs e)
{
.
/ * EndPoint setting * /
IrDAEndPoint irEndPoint = new IrDAEndPoint (irDevices [0] .DeviceID, "IrDA: OBEX");
/ * Initialize socket * /
Socket irSocket = new Socket (AddressFamily.Irda, SocketType.Stream, ProtocolType.Unspecified);
/ * Connect to mobile phone via OBEX service * /
irSocket.Connect (irEndPoint);
}
Thus, from now on we can exchange data between PC and cellphone via irSocket by pushing / pulling appropriate OBEX packets as mentioned above.
2. Connect via Bluetooth with VC ++
OBEX and programming techniques for infrared ports, Bluetooth Picture 3 In 1994, the world's leading telecommunications equipment provider, Ericsson, successfully researched wireless technology to allow mobile phones to connect accessories such as headsets and microphones via radio waves. Four years later, the Bluetooth SIG was established (including Ericsson, Intel, IBM, Nokia and Toshiba) officially released the specification for the 1.0A version of Bluetooth technology in 1999. Bluetooth is also available. The IEEE 802.15.1 name works at 2.4 GHz, the coverage range is up to 100 m (Class 1), the speed can reach 3Mpbs for the 2.0 + EDR (Enhanced Data Rate) version.
Since Windows XP SP1, Microsoft has included its operating system with a Microsoft Bluetooth Stack programming model that allows connecting to Bluetooth devices via Bluetooth socket. One problem is that not all Bluetooth chipsets support Microsoft Bluetooth Stack, but most have a built-in driver as well as a separate SDK for Software Development Kit. We can find Bluetooth chipsets that support Microsoft Bluetooth Stack on SONY VAIO laptops, while IBM series supports Widcomm Bluetooth Stack (the SDK costs up to 1400 USD).
To program with Bluetooth socket we must install the SDK for Windows XP SP2 (http://www.microsoft.com/msdownload/platformsdk/sdkupdate/XPSP2FULLInstall.htm). This SDK will provide some header files as well as the necessary libraries during programming. Since .NET 2.0 version does not yet support Bluetooth, coding must be completely done on the basis of Windows' built-in API library.
The approach to Bluetooth devices is exactly the same as infrared devices, but only, instead of using the built-in .NET functions, you have to move on your own. Below is a code written in VC ++ 2005 that allows searching Bluetooth devices, and printing the name and address of each device found:
#include
#include
#include
#pragma comment (lib, "ws2_32.lib")
#pragma comment (lib, "irprops.lib")
/ * compile with: / clr * /
using namespace System;
void main ()
{
WORD wVersionRequested = 0x202;
WSADATA m_data;
/ * Initializing Windows Socket * /
if (WSAStartup (wVersionRequested, & m_data) == 0) {
/ * Set search parameters * /
WSAQUERYSET querySet;
memset (& querySet, 0, sizeof (querySet));
querySet.dwSize = sizeof (querySet);
/ * Setting the search range to be Bluetooth devices * /
querySet.dwNameSpace = NS_BTH;
HANDLE hLookup;
/ * Set up return information * /
DWORD flags = LUP_RETURN_NAME | LUP_CONTAINERS | LUP_RETURN_ADDR | LUP_FLUSHCACHE | LUP_RETURN_BLOB;
/ * Search up to 10 devices * /
int maxDevices = 10;
/ * Start the search process * /
int result = WSALookupServiceBegin (& querySet, flags, & hLookup);
while (count
BYTE buffer [1000];
OBEX and programming techniques for infrared ports, Bluetooth Picture 4
Figure 2: Select Bluetooth connection
DWORD bufferLength = sizeof (buffer);
WSAQUERYSET * pResults = (WSAQUERYSET *) & buffer;
result = WSALookupServiceNext (hLookup, flags, & bufferLength, pResults);
/ * Print the name and address of each device found * /
if (result == 0) {
CSADDR_INFO * pCSAddr = (CSADDR_INFO *) pResults-> lpcsaBuffer;
SOCKADDR_BTH * bts = (SOCKADDR_BTH *) pCSAddr-> RemoteAddr.lpSockaddr;
Console :: WriteLine (pResults-> lpszServiceInstanceName);
Console :: WriteLine ("Device ID: {0}", bts-> btAddr);
count ++;
}
}
/ * End the search process * /
result = WSALookupServiceEnd (hLookup);
WSACleanup ();
}
}
On mobile phones today, Bluetooth services are increasingly rich to fully meet the needs of users' wireless connections such as file transfer, photo printing, headphones . These services all have a unique identifier. (UUID) and defined in the bthdef.h header file. In which OBEX Object Push service has the same role as IRDA: OBEX of infrared device. Below is the code that allows connecting to this service:
#include
void main ()
{
.
/ * Initializing Windows Socket * /
if (WSAStartup (wVersionRequested, & m_data) == 0) {
/ * Initialize Bluetooth socket * /
SOCKET s = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
SOCKADDR_BTH sine;
sin.addressFamily = AF_BTH;
/ * Device address found above * /
sin.btAddr = bts-> btAddr;
/ * OBEX Object Push service identifier * /
sin.serviceClassId = OBEXObjectPushServiceClass_UUID;
sin.port = 0;
/ * Connect to OBEX Object Push * /
int result = connect (s, (SOCKADDR *) & sin, sizeof (sin));
WSACleanup ();
}
}
So by using the send () and recv () functions together with the OBEX protocol, we can easily send and receive files between PC and mobile phone.
OBEX and programming techniques for infrared ports, Bluetooth Picture 5
Figure 3: Files in the phone directory
EPILOGUE
Above, I have just introduced you to the two most popular ways to connect wirelessly today to serve the needs of data exchange between PC and mobile phone. For the convenience of developing .NET-based applications, I have written a Bluetooth library class that makes it easy to connect to Bluetooth devices similar to Microsoft's IrDA class library (available for download at www.hitekgroup website). .net). The demo program was written specifically for the Sony Ericsson T610 mobile phone, so it can work inefficiently on other models. Hopefully, through this article, you can build your own file management application suitable for your phone.
Demo materials and programs can be downloaded at www.hitekgroup.net or http://hitekgroup.cabspace.com/.
Nguyen Duc Thang
thangnd@hitekgroup.net
You should read it
- How secure is Bluetooth?
- Desktop without Bluetooth? This is the solution
- What is Bluetooth 5.0? What benefits does it have for our lives?
- Instructions for using Bluetooth on devices
- Do you know what Bluetooth on TV is for?
- Top 6 USB Bluetooth for the best PC
- How to Connect Speaker to iPhone via Bluetooth
- The 'Bluetooth 4.2' technology standard was released
- Learn about Bluetooth technology
- How to Use USB Bluetooth
- How to set up and manage Bluetooth devices in Windows
- How to use Bluetooth on the simplest iPhone