Bluetooth

From Free Pascal wiki
Jump to: navigation, search

English (en) español (es)

About

The bluetoothlaz package provides bindings and functions to access bluetooth devices under various platforms.

It contains examples, like accessing the Wii Remote.

Author

Mattias Gaertner

License

LGPL (please contact the author if the LGPL doesn't work with your project licensing)

Download

The latest stable release can be found on the Lazarus CCR Files page.

Status

Alpha

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.

Dependencies / System Requirements

Linux

The BlueZ libraries plus their development files must be installed.

Ubuntu/Debian

Install the libbluetooth-dev package:

sudo apt-get install libbluetooth-dev

Mandriva/Redhat

Install the libbluez-devel package.

Installation

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.

Examples

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.

Identifying reachable Bluetooth devices

This is a simple example that will identify one Bluetooth device in the vicinity.

The code uses the following modules:

uses Bluetooth, ctypes, Sockets;

This part can be called from a button click to write Bluetooth information to the console:

procedure TForm1.FindBlueToothClick(Sender: TObject);
var
 device_id, device_sock: cint;
 scan_info: array[0..127] of inquiry_info;
 scan_info_ptr: Pinquiry_info;
 found_devices: cint;
 DevName: array[0..255] of Char;
 PDevName: PCChar;
 RemoteName: array[0..255] of Char;
 PRemoteName: PCChar;
 i: Integer;
 timeout1: Integer = 5;
 timeout2: Integer = 5000;
begin
 // get the id of the first bluetooth device.
 device_id := hci_get_route(nil);
 if (device_id < 0) then
   raise Exception.Create('FindBlueTooth: hci_get_route')
 else
   writeln('device_id = ',device_id);

 // create a socket to the device
 device_sock := hci_open_dev(device_id);
 if (device_sock < 0) then
   raise Exception.Create('FindBlueTooth: hci_open_dev')
 else
   writeln('device_sock = ',device_sock);

 // scan for bluetooth devices for 'timeout1' seconds
 scan_info_ptr:=@scan_info[0];
 FillByte(scan_info[0],SizeOf(inquiry_info)*128,0);
 found_devices := hci_inquiry_1(device_id, timeout1, 128, nil, @scan_info_ptr, IREQ_CACHE_FLUSH);

 writeln('found_devices (if any) = ',found_devices);

 if (found_devices < 0) then
   raise Exception.Create('FindBlueTooth: hci_inquiry')
 else
     begin
       PDevName:=@DevName[0];
       ba2str(@scan_info[0].bdaddr, PDevName);
       writeln('Bluetooth Device Address (bdaddr) DevName = ',PChar(PDevName));

       PRemoteName:=@RemoteName[0];
       // Read the remote name for 'timeout2' milliseconds
       if (hci_read_remote_name(device_sock,@scan_info[0].bdaddr,255,PRemoteName,timeout2) < 0) then
         writeln('No remote name found, check timeout.')
       else
         writeln('RemoteName = ',PChar(RemoteName));
     end;

 c_close(device_sock);
end;

ToDo: Usage/Tutorial/Reference

The package is quite new and not yet complete. Documentation will be written, when some more platforms are supported and the API has stabilized.

The HCI documentation for hci_* functions found in bluetooth.pas can be found in the BLUETOOTH SPECIFICATION PDF files linked at this Wikipedia section, specifically at this: Wikipedia reference.