Skip to content

TWebBluetooth

TWebBluetooth is a component wrapping the web Bluetooth API for communicating from the browser with Bluetooth devices.

Bluetooth communications are setup via a Bluetooth device using a Bluetooth service that can read/write values via Bluetooth Characteristics.

Therefore, the TWebBluetooth class permits to make a connection to a device that can be accessed via the class TBluetoothDevice. Via the TBluetoothDevice, access to a service, made available via the TBluetoothService class, can be obtained. Values can be read or written using a characteristic, exposed via the class TBluetoothCharacteristic.

TWebBluetooth class

Public methods

Property Description
function HasBluetooth: boolean Returning whether the browser supports or does not support Bluetooth
function GetDevice: boolean Try to establish a connection to a device and return an instance
function GetDevice(proc: TBTRefProc): boolean; Function with anonymous method to establish a connection to a device
property Device: TBluetoothDevice Access to the last connected device object

Published properties / events

Property Description
DeviceName Sets the name of the Bluetooth device when connection to only a specific device is wanted. Leave empty when a connection to just any Bluetooth device can be made
FilterService Stringlist holding one or more services a Bluetooth device must offer before a connection to it can be made
OnDeviceObject Event triggered when a device is connected, returing the device object
OnDeviceError Event triggered when an error in the communication with the device is encountered.

TWebBluetoothDevice class

Public methods

Property Description
function HasBluetooth: boolean Returning whether the browser supports or does not support Bluetooth
function GetService: boolean Try to obtain a service object reference from the device. The service is returned via the OnService event
function GetService(proc: TBTRefProc): boolean; Function with anonymous method to get a service object
function GetServices; Try to query for all services the device exposes. Services are returned via the OnServices event.
procedure Connect Make a connection to the device. When successful, the OnConnect event is triggered.
procedure Connect(proc: TBTRefProc) Make a connection to the device using an anonymous
procedure DisconnectDevice Disconnect from the device
procedure ReConnectDevice Try to establish a new connection to the device
function Connected: boolean Returns true when a connection to the device could be established
Property Service: TBluetoothService Reference to the last retrieved service object

Published properties / events

Property Description
OnConnect Event triggered when a connection to the device could be established
OnDisconnect Event triggered upon disconnect
OnService Event triggered when a device service is retrieved
OnServices Event triggered when the list of supported services by the device is returned

Example:

This code snippet shows how a service can be obtained from a device

WebBluetooth.Device.GetService(tempservice,
  procedure(AService: TBluetoothService)
  begin
    myservice := AService;
  end
);

TWebBluetoothService class

Public methods

Property Description
procedure GetCharacteristic(uuid: string); Retrieve a characteristic with ID UUID from a service. When available, the characteristic is returned via the OnCharacteristic event.
procedure GetCharacteristic(uuid: string; proc:TBTCharacteristicProc); Retrieve a characteristic with ID UUID from a service. When available, the characteristic is returned via an anonymous method.
procedure GetCharacteristics; Query all characteristics offered by the service. The list of available services is returned via the event OnCharacteristics

Published properties / events

Property Description
UUID The UUID of the service
OnCharacteristic Event triggered when a characteristic is requested
OnCharacteristics Event triggered when the list of characteristics is requested

Example:

This code snippet shows how a characteristic is retrieved from a service:

AService.GetCharacteristic(tempcharval,
  procedure(AChar: TBluetoothCharacteristic)
  begin
    btchartempvalue := AChar;
  end
);

TWebBluetoothCharacteristic class

Public methods

Method Description
procedure StartNotify Method to start the notify mechanism. When started, the Bluetooth device will send a message (and trigger the OnNotifyXXX) event when a value of a characteristic changes.
procedure StopNotify Stops the notify mechanism of the Bluetooth device
procedure ReadXXX Read a value from the Bluetooth characteristic. The default Read performs a read on an integer value. For other types, XXX stands for different types:
Byte
Int
SmallInt
Single
Double
String
Array
The result of the read is returned via the matching OnReadXXX event.
procedure ReadXXX(proc: TBTReadValueProc) Read a value from the Bluetooth characteristic and the result is returned via an anonymous method.
XXX stands for different types: Byte
Int
SmallInt
Single
Double
String
Array
Procedure WriteXXX() Write a value to a Bluetooth characteristic. XXX stands for different types:
Byte
Int
SmallInt
Single
Double
String
Array

Published properties / events

Property Description
UUID The UUID of the characteristic
OnReadXXX Event triggered returning the result of a read operation. There are different variants of the read event for different data types
OnNotifyXXX Event triggered when a new characteristic value is available when the notification mechanism was enabled. There are different variants of the notify event for different data types

Example

This example shows how to read an

  btchartempvalue.Read(
    procedure(AValue: integer)
    begin
      ReadCharacteristic(Self, AValue);
    end
  );