Tutorial: Building a ThingConnect IoT Application
Contents
Go Up to ThingConnect
This tutorial provides detailed steps for creating a new ThingConnect IoT (Internet of Things) application to connect to a Bluetooth LE device, and retrieve the data from its services.
To build a ThingConnect application, you need the following components:
- The
TBluetoothDeviceDiscoveryManagercomponent is used to set up the connection with the device. - The corresponding ThingConnect device component is used to retrieve the services and the characteristics data from the device.
- Note: The ThingConnect Device and the
TBluetoothDeviceDiscoveryManagercomponents are not available in the Tool Palette. You can download the ThingConnect components you want to use in your application through the GetIt Package Manager that is accessible via Tools > GetIt Package Manager. When you install a ThingConnect component through the GetIt Package Manager, theTBluetoothDeviceDiscoveryManagercomponent is installed as well.
This tutorial uses the TPolarHeartRateMonitor component to connect to the Polar H7 Heart Rate Sensor and retrieves the body location and the current heart rate values. By using this tutorial, you can:
- Connect to the Polar H7 heart rate sensor.
- Start monitoring the Heart Rate service:
- The Heart Rate Measurement (subscribing to data updates).
- The Body Sensor Location value.
- Stop monitoring the service characteristics (unsubscribing from the data updates).
- Disconnect from the Polar H7 heart rate sensor.
Creating a ThingConnect Application
- Check that you have the Polar H7 Heart Rate Monitor component installed in the IDE. To do so:
- Go to Tools > GetIt Package Manager.
- Navigate to the Internet of Things category.
- Search for the Polar H7 Heart Rate Monitor component. If it is not installed in your IDE, click the Install button.
- Create a new Multi-Device Application.
- For Delphi: Open File > New > Multi-Device Application - Delphi
- For C++: Open File > New > Multi-Device Application - C++Builder
- In the Tool Palette, search for a
TBluetoothDeviceDiscoveryManagercomponent and drop it on the form. - In the Tool Palette, drop a
TPolarHeartRateMonitorcomponent on the form. - In the Object Inspector, configure the
TPolarHeartRateMonitorparameters (see Working with ThingConnect Devices and Using Bluetooth LE IoT framework).
Creating the Application UI
- Drop a TMemo component to the form.
- In the Object Inspector, set the Align property to
Bottom.
- In the Object Inspector, set the Align property to
- Drop a TLabel component to the form. In the Object Inspector:
- Set the Align property to
Top. - Set the Text property to
Polar H7 Heart Rate Monitor.
- Set the Align property to
- Drop a TPanel component to the form. In the Object Inspector:
- Set the Align property to
Top.
- Set the Align property to
- Drop a TListBox component to the form. In the Object Inspector:
- Set the Align property to
Client.
- Set the Align property to
- Add three list box items. Right-click in the TListBox1, and select Add TListBoxItem.
- Change the name of the TListBoxItems.
- In the Object Inspector:
- Set the Name property to
BPM,LocationandStatus. - Clean the Text properties .
- Set the Name property to
- In the Object Inspector:
- Drop three TLabel components to the form. In the Object Inspector, change the following properties of each label:
Name Text First label lblBPMXX bpmSecond label lblBodyLocationBody locationThird label lblContactStatusContact status - In the Structure View:
- Drag the
lblBPMand drop it in theBPMlist item. - Drag the
lblBodyLocationand drop it in theLocationlist item. - Drag the
lblContactStatusand drop it in theStatuslist item. - Set the Align property of the labels to
Client.
- Drag the
- Add four TButtons to the
Panel1. In the Object Inspector, set the following properties of each button:Name Text First button ButtonConnectConnect Second button ButtonDisconnectDisconnect Third button ButtonStartMonitorStart Monitoring Forth button ButtonStopMonitorStop Monitoring
Connecting to and Disconnecting from the Polar H7 Heart Rate Sensor
- Create the
OnClickevent of theButtonConnectcomponent (double-click on it) and add the following code:- For Delphi:
procedure TForm1.ButtonConnectClick(Sender: TObject); begin {$IF Defined(WIN32) or Defined(WIN64)} BluetoothDeviceDiscoveryManager1.DiscoveryMethod := TDiscoveryMethod.Connect; {$ELSE} BluetoothDeviceDiscoveryManager1.DiscoveryMethod := TDiscoveryMethod.ScanResponse; {$ENDIF} BluetoothDeviceDiscoveryManager1.DiscoverDevices; Memo1.Lines.Add('Connecting...'); end;
- For C++:
void __fastcall TForm1::ButtonConnectClick(TObject *Sender) { #ifdef _Windows BluetoothDeviceDiscoveryManager1->DiscoveryMethod = TDiscoveryMethod::Connect; #else BluetoothDeviceDiscoveryManager1->DiscoveryMethod = TDiscoveryMethod::ScanResponse; #endif BluetoothDeviceDiscoveryManager1->DiscoverDevices(); Memo1->Lines->Add("Connecting..."); }
- Add
Iot.Family.BluetoothLE.GattTypesto the uses classes. - Create the
OnDeviceConnectedevent of thePolarHeartRateMonitor1component (double-click on it) and add the following code:- For Delphi:
procedure TForm1.PolarHeartRateMonitor1DeviceFound; begin Memo1.Lines.Add('New Polar H7 device found'); ButtonConnect.Text := 'Connected'; ButtonConnect.Enabled := False; ButtonDisconnect.Enabled := True; ButtonStartMonitor.Enabled := True; ButtonStopMonitor.Enabled := False; lblContactStatus.Text := 'Contact status: Connected'; end;
- For C++:
void __fastcall TForm1::PolarHeartRateMonitor1DeviceFound() { Memo1->Lines->Add("New Polar H7 device found"); ButtonConnect->Text = "Connected"; ButtonConnect->Enabled = False; ButtonDisconnect->Enabled = True; ButtonStartMonitor->Enabled = True; ButtonStopMonitor->Enabled = False; lblContactStatus->Text = "Conctact status: Connected"; }
- Create the
OnClickevent of theButtonDisconnectcomponent (double-click on it) and add the following code:- For Delphi:
procedure TForm1.ButtonDisconnectClick(Sender: TObject); begin PolarHeartRateMonitor1.Disconnect; end;
- For C++:
void __fastcall TForm1::ButtonDisconnectClick(TObject *Sender) { PolarHeartRateMonitor1->Disconnect(); }
- Create the
OnDeviceDisconnectevent of thePolarHeartRateMonitor1component (double-click on it in the Events tab) and add the following code:- For Delphi:
procedure TForm1.PolarHeartRateMonitor1DeviceDisconnect (const Sender: TCustomGeneratedBluetoothLEComponent); begin Memo1.Lines.Add('Polar H7 device disconnected'); ButtonConnect.Text := 'Connect'; ButtonConnect.Enabled := True; ButtonDisconnect.Enabled := False; ButtonStartMonitor.Enabled := False; ButtonStopMonitor.Enabled := False; lblContactStatus.Text := 'Contact status: Disconnected'; end;
- For C++:
void __fastcall TForm1::PolarHeartRateMonitor1DeviceDisconnect (TCustomGeneratedBluetoothLEComponent * const Sender) { Memo1->Lines->Add("Polar H7 device disconnected"); ButtonConnect->Text = "Connect"; ButtonConnect->Enabled = True; ButtonDisconnect->Enabled = False; ButtonStartMonitor->Enabled = False; ButtonStopMonitor->Enabled = False; lblContactStatus->Text = "Contact status: Disconnected"; }
Receiving Data Updates from the Polar H7 HRM Device
- Create the
OnClickevent of theButtonStartMonitorcomponent (double-click on it) and add the following code:- For Delphi:
procedure TForm1.ButtonStartMonitorClick(Sender: TObject); begin // Subscribing to the Service characteristics: PolarHeartRateMonitor1.SubscribeHeartRateMeasurement; Memo1.Lines.Add('Subscribing to HRM'); PolarHeartRateMonitor1.RefreshBodySensorLocation; Memo1.Lines.Add('Refreshing Body Sensor Location'); ButtonStartMonitor.Enabled := False; ButtonConnect.Enabled := False; ButtonDisconnect.Enabled := True; ButtonStartMonitor.Enabled := False; ButtonStopMonitor.Enabled := True; end;
- For C++:
void __fastcall TForm1::ButtonStartMonitorClick(TObject *Sender) { // Subscribing to the Service characteristics: PolarHeartRateMonitor1->SubscribeHeartRateMeasurement(); Memo1->Lines->Add("Subscribing to HRM"); PolarHeartRateMonitor1->RefreshBodySensorLocation(); Memo1->Lines->Add("Refreshing Body Sensor Location"); ButtonStartMonitor->Enabled = False; ButtonConnect->Enabled = False; ButtonDisconnect->Enabled = True; ButtonStartMonitor->Enabled = False; ButtonStopMonitor->Enabled = True; }
- Implement the event handler of the
OnBodySensorLocationUpdateevent:- For Delphi:
procedure TForm1.PolarHeartRateMonitor1BodySensorLocationUpdate(Sender: TObject; const Value: TGattBodySensorLocation); begin case Value of TGattBodySensorLocation.Unread: lblBodyLocation.Text := lblBodyLocation.Text+ ': Unread'; TGattBodySensorLocation.Other: lblBodyLocation.Text := lblBodyLocation.Text+ ': Other'; TGattBodySensorLocation.Chest: lblBodyLocation.Text := lblBodyLocation.Text+ ': Chest'; TGattBodySensorLocation.Wrist: lblBodyLocation.Text := lblBodyLocation.Text+ ': Wrist'; TGattBodySensorLocation.Finger: lblBodyLocation.Text := lblBodyLocation.Text+ ': Finger'; TGattBodySensorLocation.Hand: lblBodyLocation.Text := lblBodyLocation.Text+ ': Hand'; TGattBodySensorLocation.EarLobe: lblBodyLocation.Text := lblBodyLocation.Text+ ': EarLobe'; TGattBodySensorLocation.Foot: lblBodyLocation.Text := lblBodyLocation.Text+ ': Foot'; end; Memo1.Lines.Add('Body sensor location update...'); end;
- For C++:
void __fastcall TForm1::PolarHeartRateMonitor1BodySensorLocationUpdate (TObject *Sender, const TGattBodySensorLocation Value) { switch (Value) { case TGattBodySensorLocation::Unread: lblBodyLocation->Text = lblBodyLocation->Text + ": Unread"; break; case TGattBodySensorLocation::Other: lblBodyLocation->Text = lblBodyLocation->Text + ": Other"; break; case TGattBodySensorLocation::Chest: lblBodyLocation->Text = lblBodyLocation->Text + ": Chest"; break; case TGattBodySensorLocation::Wrist: lblBodyLocation->Text = lblBodyLocation->Text + ": Wrist"; break; case TGattBodySensorLocation::Finger: lblBodyLocation->Text = lblBodyLocation->Text + ": Finger"; break; case TGattBodySensorLocation::Hand: lblBodyLocation->Text = lblBodyLocation->Text + ": Hand"; break; case TGattBodySensorLocation::EarLobe: lblBodyLocation->Text = lblBodyLocation->Text + ": EarLobe"; break; case TGattBodySensorLocation::Foot: lblBodyLocation->Text = lblBodyLocation->Text + ": Foot"; break; default: lblBodyLocation->Text = lblBodyLocation->Text; } Memo1->Lines->Add("Body sensor location update..."); }
- Implement the event handler of the
OnHeartRateMeasurementUpdateevent:- For Delphi:
procedure TForm1.PolarHeartRateMonitor1HeartRateMeasurementUpdate (Sender: TObject; const Value: TGattHeartRateMeasurement); var val: TGattHeartRateMeasurement; begin val := Value; lblBPM.Text := IntToStr(val.HeartRateMeasurement) + 'bpm'; Memo1.Lines.Add('HRM update...'); end;
- For C++:
void __fastcall TForm1::PolarHeartRateMonitor1HeartRateMeasurementUpdate (TObject *Sender, const TGattHeartRateMeasurement &Value) { TGattHeartRateMeasurement val = Value; lblBPM->Text = IntToStr(val.HeartRateMeasurement) + "bpm"; Memo1->Lines->Add("HRM update..."); }
- Create the
OnClickevent of theButtonStopMonitorcomponent (double-click on it) and add the following code:- For Delphi:
procedure TForm1.ButtonStopMonitorClick(Sender: TObject); begin PolarHeartRateMonitor1.UnSubscribeHeartRateMeasurement; Memo1.Lines.Add('UnSubscribing from HRM'); ButtonConnect.Enabled := False; ButtonDisconnect.Enabled := True; ButtonStartMonitor.Enabled := True; ButtonStopMonitor.Enabled := False; end;
- For C++:
void __fastcall TForm1::ButtonStopMonitorClick(TObject *Sender) { PolarHeartRateMonitor1->UnSubscribeHeartRateMeasurement(); Memo1->Lines->Add("UnSubscribing from HRM"); ButtonConnect->Enabled = False; ButtonDisconnect->Enabled = True; ButtonStartMonitor->Enabled = True; ButtonStopMonitor->Enabled = False; }
Running the Application
To run the application in your device, just click Run or click F9.
- Click the Connect button to connect to the Polar H7 heart rate monitor.
- Click the Start Monitoring button to start receiving data updates from the measurements from the device.
- Click the Stop Monitoring button to stop receiving data updates from the measurements from the device (unsubscribing from the characteristic).
- Click the Disconnect button to disconnect from the Polar H7 heart rate device.
