Bluetooth Low Energy Blocks
Last updated
Last updated
Description
Bluetooth Low Energy, or BLE, is used to wirelessly connect two devices to one another. The BLE component allows you to find and connect to BLE devices and to communicate directly with them.
BLE functionality can't be tested with the Thunkable Live app on iOS. To test the BLE functionality in your app on iOS, please download your app.
To add a Bluetooth component to your app:
Go to your Blocks tab
Find the Advanced
drawer of blocks. Click the drop-down menu icon to show the Advanced invisible components
Click the ⊕ icon next to the Bluetooth
drawer
You will see a dialog to create your Bluetooth component. Click Submit to create the component, or Delete to dismiss the dialog without creating the component.
Once you have added at least one Bluetooth component to your app, you will be able to view all of your Bluetooth components under the Bluetooth
drawer in the Advanced section of the Blocks tab.
To delete a Bluetooth component, click on the ⚙ icon next to the component's name to bring up the properties dialog and click Delete.
Version 1 of the BLE component in Thunkable ✕ contains 5 functions, each of which are documented in greater detail below.
This function is used to scan for nearby BLE or Bluetooth enabled devices. Scanning happens for 10 seconds. Once scanning has successfully completed then a list of the available device ids and a list of available names will be returned. If the scanning function can not be completed then an error will be returned.
Device Ids
List
Returns a list of the IDs of the available devices
Device Names
List
Returns a list of the names of the available devices
Device RSSIs
List
Returns a list of the signal strengths (in dBm) for the available devices, measured relative to 1 milliwatt
error
Text
Returns an error message from the function
Example
The Location Sensor returns a list of names of the available BLE devices which can be connected to a Simple List,
The Connect to Device Id
function allows your app to connect to a BLE enabled device based in the Id of that device.
Device Id
Text
Expects a text string with the device id you want to connect to
Device Name
Text
Returns the name of the device you have just connected to
error
Text
If an error message is returned, returns the error. Else returns null
When you know the name of the BLE device that you want to connect to you can use the Connect to Device Name
block to connect directly to it. Please note that device names are not unique and it is relatively easy to change the name of a BLE device.
Device Name
Text
Expects a text block with the name of the device to connect to
Device Id
Text
Returns the ID of the device that you have just connected to
error
Text
If an error message is returned, returns the error. Else returns null
Example
Present the list of names of the available BLE devices in a simple list allowing users to connect on the device they click using the Connect to Device Name
block and connect it to the item
block.
To receive data from a BLE device, capable of sending data to another device as notifications, you need to use the Subscribe To Characteristics
function in your app. This function allows you to listen to updates from the peripheral device by subscribing to its characteristics and receiving data through the "notify" method.
To use this functionality, ensure your app is already connected to the desired Bluetooth device.
characteristic UUID
Text
Expects a text block with a 32-bit UUID for the BLE type, service or profile required
error
Text
If an error message is returned, returns the error. Else returns null
If you are connected to a BLE device that is capable of sending data to another device using the "notify" method and you have already subscribed to a characteristic then you need to use the Characteristic Change
function in your app. This block alerts you whenever the characteristic value changes on the peripheral (server) device. Data from the BLE device may be received in either String or Byte format.
Ensure that you have subscribed to the characteristic to use this block.
Set something to happen when the Characteristics Change.
data (string)
Text
Returns a string containing the information received from the BLE device
data (byte array)
array (list) of bytes
Returns a byte array containing the information received from the BLE device
error
Text
If an error message is returned, returns the error. Else returns null
Example
Connect to a specific device using Device ID. Subscribe to a characteristic using characteristic UUID and set the new data to labels when the characteristics change.
If connected to another Bluetooth device, this block will disconnect your app from the device.
If you have a BLE device that is capable of sending data to another device then you will need to use the Receive String
function in your app in order to listen to, or receive, data coming from the BLE device in String format.
You need to be connected to another Bluetooth device in order to use this block.
characteristic UUID
Text
Expects a text block with a 32-bit UUID for the BLE type, service or profile required
data (string)
Text
Returns a string containing the information received from the BLE device
error
Text
If an error message is returned, returns the error. Else returns null
If you have a BLE device that is capable of sending data to another device then you will need to use the Receive Byte Array
function in your app in order to listen to, or receive, data coming from the BLE device in Byte Array format.
You need to be connected to another Bluetooth device in order to use this block.
characteristic UUID
Text
Expects a text block with a 32-bit UUID for the BLE type, service or profile required
data (byte array)
array (list) of bytes
Returns a byte array containing the information received from the BLE device
error
Text
If an error message is returned, returns the error. Else returns null
This asynchronous function listens for when your device disconnects from a named Bluetooth device.
You can program some events to happen when your device disconnects from the named device in the then do
section of this block.
Device Id
Text
Unique ID of the bluetooth device to listen for disconnection with
error
Text
If an error message is returned, returns the error. Else returns null
Use this block to send data as a string of text to a BLE device.
You need to be connected to another Bluetooth device in order to use this block.
characteristic UUID
Text
Expects a text block with a 32-bit UUID for the BLE type, service or profile required
data
Text
Expects a text block with the message that you want to send to your BLE device
error
Text
If an error message is returned, returns the error. Else returns null
Use this block to send data as an array (list) of bytes to a BLE device.
You need to be connected to another Bluetooth device in order to use this block.
characteristic UUID
Text
Expects a text block with a 32-bit UUID for the BLE type, service or profile required
data
array (list) of bytes
Expects a text block with the message that you want to send to your BLE device
error
Text
If an error message is returned, returns the error. Else returns null
Use this block to send data in hexadecimal format to a BLE device.
You need to be connected to another Bluetooth device in order to use this block.
characteristic UUID
Text
Expects a text block with a 32-bit UUID for the BLE type, service or profile required
data (hexadecimal)
data in hexadecimal format
Expects a text block with the message that you want to send to your BLE device
error
Text
If an error message is returned, returns the error. Else returns null
Use this block to send data in base64 format to a BLE device.
You need to be connected to another Bluetooth device in order to use this block.
characteristic UUID
Text
Expects a text block with a 32-bit UUID for the BLE type, service or profile required
data
data in base64 format
Expects a text block with the message that you want to send to your BLE device
error
Text
If an error message is returned, returns the error. Else returns null
All values (string, HEX, Byte array, Base64) are being converted to a byte array before sending.