Bluetooth LE support in RAD Studio XE7

by Sep 4, 2014

RAD Studio XE7's integrated wireless support (Bluetooth and Wifi) works with thousands of consumer and industry specific devices. RAD Studio's approach makes it easy to integrate virtually any device into the app user experience, and even support multiple device vendors with the same code.

In RAD Studio XE7, we have a new Bluetooth LE component for connecting to many different Bluetooth Smart Devices that work with the different GATT profiles.

We ship several Bluetooth demos with the product that you can look at. For Bluetooth LE, we have a heartrate monitor demo that displays your current heart rate by connecting to a heart rate sensor.
You can find the demo under: C:UsersPublicDocumentsEmbarcaderoStudio15.0SamplesObject PascalMobile SamplesDevice Sensors and ServicesBluetoothHeartRateMonitor

I am using the Zephyr heartrate monitor for this, but have also used Polar's H7 heartrate monitor.

I extended the demo slightly by adding a Listbox for logging the heartrate. Also, for the iPhone version, I customized my iPhone view using the new FireUI Multi-Device Designer to display a + icon instead of the 'Save' text for logging my heart rate since I am working with less screen real estate.

 

heartrate2.jpg

bluetooth_iPod.jpg   bluetooth2.jpg

Below are the necessary UUIDs we need to connect to our heart rate sensor:

const
  HRDeviceName = 'Cardiosport HRM';
  ServiceUUID = '';
  CharactUUID = '';

  // From the Bluetooth website Home > GATT Specifications > Services > ServiceViewer
// Exposes heart rate and other data from a Heart Rate Sensor
  HRSERVICE: TBluetoothUUID = '{0000180D-0000-1000-8000-00805F9B34FB}';

  // from the Bluetooth website under Home > GATT Specifications > Characteristics > CharacteristicViewer
  HRMEASUREMENT_CHARACTERISTIC: TBluetoothUUID  = '{00002A37-0000-1000-8000-00805F9B34FB}';
  BODY_SENSOR_LOCATION_CHARACTERISTIC: TBluetoothUUID  = '{00002A38-0000-1000-8000-00805F9B34FB}';

  //building Sensor Location Characteristic value used to index the array to display the string
  BodySensorLocations : array[0..6] of string = ('Other', 'Chest', 'Wrist', 'Finger', 'Hand', 'Ear Lobe', 'Foot');

  HR_VALUE_FORMAT_MASK = $1;
  SENSOR_CONTACT_STATUS_MASK = $6;
  ENERGY_EXPANDED_STATUS_MASK = $8;
  RR_INTERVAL_MASK = $10;

 

Here is the btnScanClick code:

procedure TfrmHeartMonitor.DoScan;
begin
  ClearData;
  lblDevice.Text := '';
  lblBodyLocation.Text := '';
  lblContactStatus.Text := '';
  BluetoothLE1.DiscoverDevices(2500, [HRSERVICE]);
end;

We also set up several additional events:

procedure TfrmHeartMonitor.BluetoothLE1EndDiscoverDevices(const Sender: TObject;
  const ADeviceList: TBluetoothLEDeviceList);
var
  I: Integer;
begin
  // log
  Memo1.Lines.Add(ADeviceList.Count.ToString +  ' devices discovered:');
  for I := 0 to ADeviceList.Count - 1 do Memo1.Lines.Add(ADeviceList[I].DeviceName);

  if BluetoothLE1.DiscoveredDevices.Count > 0 then
  begin
    FBLEDevice := BluetoothLE1.DiscoveredDevices.First;
    lblDevice.Text := HRDeviceName;
    if BluetoothLE1.GetServices(FBLEDevice).Count = 0 then
    begin
      Memo1.Lines.Add('No services found!');
      lblBPM.Font.Size := 26;
      lblBPM.Text := 'No services found!';
    end
    else
      GetServiceAndCharacteristics;
  end
  else
    lblDevice.Text := 'Device not found';
end;

 

 Here is a video of our Bluetooth LE support in action: 

{"video":"https://www.youtube.com/watch?v=oeyGzuC_QqU","width":"480","height":"360"}