RTL.SensorTag

From RAD Studio Code Examples
Jump to: navigation, search

Location

You can find the SensorTag sample project at:

Description

This application uses the Bluetooth API to search for nearby SensorTag devices, connect to them, and display the data from their sensors.

The application uses the following controls:

  • BluetoothLE1: The TBluetoothLE component.
  • Button1: Fires the Button1Click event handler.
  • Button2: Fires the Button2Click event handler.
  • ListBox1: Displays the information about the discovered SensorTag devices.
  • EdAccelX: TEdit control that displays the data for the X-axis of the accelerometer.
  • EdAccelY: TEdit control that displays the data for the Y-axis of the accelerometer.
  • EdAccelZ: TEdit control that displays the data for the Z-axis of the accelerometer.
  • EdAmbient: TEdit control that displays the data for the ambient temperature of the IR temperature sensor.
  • EdHumidity:TEdit control that displays the data for the humidity.
  • EdTarget: TEdit control that displays the data for the target temperature of the IR temperature sensor.
  • Label1 - Label8: The labels for the TEdit controls.

How to Use the Sample

  1. Navigate to the location given above and open: Delphi: SensorTag.dproj.
  2. Press F9 or choose Run > Run.
  3. Click the Find devices button to start searching for SensorTag devices.
  4. Wait until the discovery completes.
  5. Click the Subscribe to data button to subscribe to the services that the SensorTag device offers.

The application displays the discovered SensorTag devices in the ListBox1 control. If you subscribe to receive data from a SensorTag device, the application displays the following:

  • Temperature
    • Ambient: The ambient temperature.
    • Target: The calculated temperature of the target of the IRtemperature sensor.
  • Accelerometer
    • X: The acceleration of the X-axis of the accelerometer.
    • Y: The acceleration of the Y-axis of the accelerometer.
    • Z: The acceleration of the Z-axis of the accelerometer.
  • Humidity: The humidity.

If you do not select a device from the list and you click the Subscribe to data button, the application shows the alert: "Please select a device from the list".

Implementation

This application uses a TBluetoothLE component to:

  • Search for nearby SensorTag devices.
  • Search for the services that a SensorTag device offers, and subscribe to those services.
  • Display the data received from the SensorTag device.

This application implements custom event handlers for OnEndDiscoverServices, OnCharacteristicRead, and OnEndDiscoverDevices events.

Main Form

  • The application defines the following event handlers:
    • Button1Click: Calls the DiscoverDevices method of the Bluetooth component.
    • Button2Click: Calls the DiscoverServices method of the Bluetooth component. The parameter that it passes to the method is the currently selected device from the list.
    • BluetoothLE1EndDiscoverDevices: Occurs when the discovery of devices finishes. This method filters the discovered devices by name and displays all The devices named "SensorTag" or "TI BLE Sensor Tag" on the Listbox1 control.
    • BluetoothLE1EndDiscoverServices: This method scans for services that the SensorTag device offers and subscribes to those services. Specifically, it subscribes to "UUID_IRT_SERV" (the IR temperature service), "UUID_ACC_SERV" (the accelerometer service)) and "UUID_HUM_SERV" (the humidity service).
    • BluetoothLE1CharacteristicRead: Occurs when a SensorTag device send new data for any service that you subscribed. This method extracts and converts the data so that it is suitable for display in the corresponding TEdit control.
  • The application also defines the following methods:
    • shortSignedAtOffset: This method returns the provided integer as signed.
    • shortUnsignedAtOffset: This method returns the provided integer as unsigned..
    • extractAmbientTemperature: Extracts and returns the value for ambient temperature from the data provided by the SensorTag device.
    • extractTargetTemperature: Extracts and returns the value for target temperature from the data provided by the SensorTag device.

The methods listed above are based on the code snippets published on the official SensorTag documentation: SensorTag User Guide.

Uses

See Also