Delphi IoT and BLE

One of the most important communication protocols for IoT is Bluetooth. In
fact, there are two different Bluetooth technologies. The classic Bluetooth
and the newer, BLE, sometimes called smart Bluetooth; Bluetooth LE, or just
BLE for short. The classic Bluetooth is useful for data streaming and
provides data transfer rates up to 2 Mbps. It is commonly used in cars,
especially in hands-free headsets. It has much higher data transfer rates as
compared to BLE, but also much higher energy consumption.

The Bluetooth LE GATT profile describes a number of services. Each service
is a collection of characteristics. Each characteristic includes different
attributes. A GATT profile defines if a certain characteristic is mandatory or
optional. For example, in the Heart Rate Service, the Heart Rate
Measurement characteristic is mandatory, while Body Sensor Location and
Heart Rate Control Point are optional. Each characteristic is basically data
that you can write, read, or subscribe to in a BLE device of a certain kind. In
BLE, one party participating in communications acts as a GATT server and
another as a GATT client. The server exposes a set of attributes to a client
that are accessible using the attribute protocol. An attribute is the actual data.
It could be anything. It could be a heart rate value measured in beats per
minute, the state of a light switch, temperature, or something else.
Not every version of mobile operating systems and not every mobile device
type supports BLE and the specification itself is evolving too. That is why it
is important to pay attention if your combination of software and hardware
supports what you want to build.

ble

The key types and classes for working with Bluetooth in Delphi are defined
in the System.Bluetooth unit. With Delphi, it is not only possible to build
GATT clients, but also to implement GATT servers. Similar to other
frameworks in Delphi, there are two levels of Bluetooth support. You can do
things in code, with types defined in System.Bluetooth, or you can use
reusable components, TBluetooth and TBluetoothLE, defined in the
System.Bluetooth.Components unit, that can make your life easier and act
as a wrapper to non-visual classes. In this chapter, we are going to focus
primarily on BLE, because this is the key protocol for IoT.

In the OnChange event of the checkbox, add the following line of code that
will enable the BLE component and start the discovery for available heart
rate monitors:

const
 HRM_SERVICE: TBluetoothUUID =
'{0000180D-0000-1000-8000-00805F9B34FB}';
 HR_MEASUREMENT_CHARACTERISTIC: TBluetoothUUID =
'{00002A37-0000-1000-8000-00805F9B34FB}';
A_TIMEOUT = 3000;
 procedure TFormHRM.chkbxHRMChange(Sender: TObject);
begin
 BluetoothLE1.Enabled := chkbxHRM.IsChecked;
  if BluetoothLE1.Enabled then
  BluetoothLE1.DiscoverDevices(A_TIMEOUT, [HRM_SERVICE]);
end;

The first parameter to the StartDiscovery call is the timeout in milliseconds.
If we skipped the second parameter, we would find all BLE devices visible to
our app. That could be useful, if we would like to build a generic BLE
scanner, but in most cases we are just interested in connecting to a particular
device and getting from it a particular data.

Una risposta a “Delphi IoT and BLE”

  1. Hey! I know this is somewhat off topic but I was wondering which
    blog platform are you using for this site? I’m getting sick and tired of WordPress
    because I’ve had problems with hackers and I’m looking
    at alternatives for another platform. I would
    be awesome if you could point me in the direction of a good platform.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.