Sign in Join
Wiki copytemporary

Bluetooth: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Bluetooth

OldNewLine
1+## About
2+
3+The bluetoothlaz package provides bindings and functions to access bluetooth devices under various platforms.
4+
5+It contains examples, like accessing the Wii Remote.
6+
7+## Author
8+
9+Mattias Gaertner
10+
11+## License
12+
13+LGPL (please contact the author if the LGPL doesn't work with your project licensing)
14+
15+## Download
16+
17+The latest stable release can be found on the [Lazarus CCR Files page](http://sourceforge.net/projects/lazarus-ccr/files/Bluetooth/).
18+
19+## Status
20+
21+**Alpha**
22+
23+At the moment the package only supports Linux. Eventually it will support Windows, Mac OS X and other platforms and will get some platform independent Wrapper functions/classes.
24+
25+## Dependencies / System Requirements
26+
27+### Linux
28+
29+The BlueZ libraries plus their development files must be installed.
30+
31+#### Ubuntu/Debian
32+
33+Install the **libbluetooth-dev** package:
34+
35+``` bash
36+sudo apt-get install libbluetooth-dev
37+```
38+
39+#### Mandriva/Redhat
40+
41+Install the **libbluez-devel** package.
42+
43+## Installation
44+
45+Download and unpack the package to a directory of your choice. In the Lazarus IDE use Package / Open package file. A file dialog will appear. Choose the bluetooth/bluetoothlaz.lpk and open it. That's all. The IDE now knows the package.
46+
47+## Examples
48+
49+There are two examples for the Wii Remote. One that demonstrates how to connect to a Wii Remote and shows the infrared sensors and one that demonstrates VR headtracking with the Wii Remote and the 3D package Asmoday.
50+
51+### Identifying reachable Bluetooth devices
52+
53+This is a simple example that will identify one Bluetooth device in the vicinity.
54+
55+The code uses the following modules:
56+
57+``` pascal
58+ uses Bluetooth, ctypes, Sockets;
59+```
60+
61+This part can be called from a button click to write Bluetooth information to the console:
62+
63+``` pascal
64+ procedure TForm1.FindBlueToothClick(Sender: TObject);
65+ var
66+ device_id, device_sock: cint;
67+ scan_info: array[0..127] of inquiry_info;
68+ scan_info_ptr: Pinquiry_info;
69+ found_devices: cint;
70+ DevName: array[0..255] of Char;
71+ PDevName: PCChar;
72+ RemoteName: array[0..255] of Char;
73+ PRemoteName: PCChar;
74+ i: Integer;
75+ timeout1: Integer = 5;
76+ timeout2: Integer = 5000;
77+ begin
78+ // get the id of the first bluetooth device.
79+ device_id := hci_get_route(nil);
80+ if (device_id < 0) then
81+ raise Exception.Create('FindBlueTooth: hci_get_route')
82+ else
83+ writeln('device_id = ',device_id);
84+
85+ // create a socket to the device
86+ device_sock := hci_open_dev(device_id);
87+ if (device_sock < 0) then
88+ raise Exception.Create('FindBlueTooth: hci_open_dev')
89+ else
90+ writeln('device_sock = ',device_sock);
91+
92+ // scan for bluetooth devices for 'timeout1' seconds
93+ scan_info_ptr:=@scan_info[0];
94+ FillByte(scan_info[0],SizeOf(inquiry_info)*128,0);
95+ found_devices := hci_inquiry_1(device_id, timeout1, 128, nil, @scan_info_ptr, IREQ_CACHE_FLUSH);
96+
97+ writeln('found_devices (if any) = ',found_devices);
98+
99+ if (found_devices < 0) then
100+ raise Exception.Create('FindBlueTooth: hci_inquiry')
101+ else
102+ begin
103+ PDevName:=@DevName[0];
104+ ba2str(@scan_info[0].bdaddr, PDevName);
105+ writeln('Bluetooth Device Address (bdaddr) DevName = ',PChar(PDevName));
106+
107+ PRemoteName:=@RemoteName[0];
108+ // Read the remote name for 'timeout2' milliseconds
109+ if (hci_read_remote_name(device_sock,@scan_info[0].bdaddr,255,PRemoteName,timeout2) < 0) then
110+ writeln('No remote name found, check timeout.')
111+ else
112+ writeln('RemoteName = ',PChar(RemoteName));
113+ end;
114+
115+ c_close(device_sock);
116+ end;
117+```
118+
119+### Communicating using RFCOMM
120+
121+RFCOMM is a simple set of transport protocols, made on top of the L2CAP protocol, providing emulated RS-232 serial ports .
122+
123+RFCOMM is sometimes called serial port emulation and the Bluetooth serial port profile is based on this protocol. RFCOMM provides a simple reliable data stream to the user, similar to TCP. Many Bluetooth applications use RFCOMM because of its widespread support and publicly available API on most operating systems. Applications that use a serial port to communicate can often be quickly ported to use RFCOMM.
124+
125+This example attempts to connect to a remote device identified by Bluetooth address 34:8A:7B:02:04:4D which is listening on channel 7 and sends a simple message if successful. It can be used with the Android example application [BluetoothChat](https://android.googlesource.com/platform/development/+/eclair-passion-release/samples/BluetoothChat).
126+
127+``` pascal
128+procedure TForm1.ConnectRFCOMMClick(Sender: TObject);
129+var
130+ loc_addr: sockaddr_rc;
131+ opt: longint;
132+ s, status: unixtype.cint;
133+ bd_addr: bdaddr_t;
134+ channel: byte;
135+ bt_addr: array[0..17] of char;
136+ bt_message: array[0..10] of char;
137+begin
138+ opt := SizeOf(loc_addr);
139+
140+ //remote device address
141+ bt_addr := '34:8A:7B:02:04:4D';
142+
143+ //allocate socket
144+ s := fpsocket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
145+
146+ //channel that destination device is listening on
147+ channel := 7;
148+
149+ loc_addr.rc_family := AF_BLUETOOTH;
150+ loc_addr.rc_bdaddr := bd_addr;
151+ loc_addr.rc_channel := channel;
152+ str2ba(@bt_addr, @bd_addr);
153+ loc_addr.rc_bdaddr := bd_addr;
154+ writeln('Trying connection to ', bt_addr);
155+
156+ status := fpconnect(s, @loc_addr, opt);
157+ writeln('channel: ', channel, ' result: ', status);
158+ if status = 0 then
159+ begin
160+ bt_message := 'Hello there';
161+ fpsend(s, @bt_message, 11, 0);
162+ // close socket after message is sent
163+ fpshutdown(s, 0);
164+ end;
165+end;
166+```
167+
168+## ToDo: Usage/Tutorial/Reference
169+
170+The package is quite new and not yet complete. Documentation will be written, when some more platforms are supported and the API has stabilized.
171+
172+The HCI documentation for hci_* functions found in bluetooth.pas can be found in the BLUETOOTH SPECIFICATION PDF files linked at this [Wikipedia section](http://en.wikipedia.org/wiki/Bluetooth#Specifications_and_features), specifically at this: [Wikipedia reference](http://en.wikipedia.org/wiki/Bluetooth#cite_note-bluetooth_specs-31).
173+
174+---
175+*From the Free Pascal wiki, page "Bluetooth", as it stood on 2020-05-29 after a change by Mgpoole, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
176+Category: Components
177+Category: Lazarus-CCR

Back to history