To get some first impressions and experiences in Bluetooth and BlueZ programming, two prototype applications are needed.
These prototypes are made for data exchange between two Linux PCs with Bluetooth dongles and simulate the Bluetooth communication in our final application.
The prototypes are
sends a file - in our example a picture - over the Bluetooth dongle to another bluetooth device.
This is done with a RFCOMM stream socket that creates an ACL (Asynchronous Connectionless Link). The ACL then only exchangs raw binary data and closes the socket afterwards.
The source file can be divided in four parts:
- The header files for socket and bluetooth programming are included.
Also, the remote bluetooth address and the RFCOMM channel for the data transfer are defined.
- The first function "getSizeFromFile" reads the size of a given file. This is important to create the file buffer later on.
- Function "getDataFromFile" reads data from a file and stores them in the file buffer.
- The "main" function is the program itself. First the path for the file is read. Then both functions above are used to create the buffer with the raw data. After that a RFCOMM stream socket with the needed connection parameters is opened. As soon as the connection is established the file can be sent by pressing "Enter". Sending is done via the socket function "send()".
Receives a file via the Bluetooth Dongle that is sent with the sendFile.c program. It is the counterpart to the example above but a bit easier. It also includes some header files and opens a RFCOMM stream socket, then listens to it on RFCOMM Channel 4. A file path must be entered where the target file is stored later on. As soon as incoming data on Channel 4 arrives the Blocking socket function "recv()" receives it and writes it in the target file. After that the socket is closed and so is the connection.