# Bluetooth Low Energy Blocks

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.

{% hint style="info" %}
BLE functionality can't be tested with the [Thunkable Live](https://docs.thunkable.com/get-started/live-test) app on iOS. \
To test the BLE functionality in your app on iOS, please [download](https://docs.thunkable.com/download#download-and-install-ios-app) your app.
{% endhint %}

## Add a Bluetooth component to Thunkable

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

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FyWBmIeifoqQCTHK2aUQn%2FAdd%20a%20Bluetooth%20component%20to%20Thunkable.jpg?alt=media&#x26;token=b2131d34-1290-429b-84cb-465d10abe484" alt=""><figcaption></figcaption></figure>

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.

![](https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-MJWUdLAYO2elchAS1Nq%2F-MJWVQMOMnhetamkex-L%2Fadd-ble.png?alt=media\&token=838b3c8a-d34c-43bd-a462-15fa77b625ec)

### Deleting your Bluetooth 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.&#x20;

![](https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-MMGsCOQmypJUYH0PDNJ%2F-MMH1Vm-HKxEp--0fjt8%2Fbluetooth-blicks.png?alt=media\&token=35ea06c7-1aa1-4e6b-8d6c-1a83f996da19)

To delete a Bluetooth component, click on the ⚙ icon next to the component's name to bring up the properties dialog and click Delete.

## Functions

Version 1 of the BLE component in Thunkable ✕ contains 5 functions, each of which are documented in greater detail below.

* [Scan](#scan)
* [Connect to Device Id](#connect-to-device-id)
* [Connect to Device Name](#connect-to-device-name)
* [Disconnect](#disconnect)
* [Receive String](#receive-string)
* [Receive Byte Array](#receive-byte-array)
* [Listen for Disconnection](#listen-for-disconnection)
* [Transmit String](#transmit-string)
* [Transmit Byte Array](#transmit-byte-array)
* [Transmit Hexadecimal](https://docs.thunkable.com/bluetooth-low-energy#transmit-hexadecimal)

### Scan

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.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FEbM7gJPpwVgvpR0R7z6v%2Fimage.png?alt=media&#x26;token=0ae18dd5-4bae-4610-8359-e30d4e12a431" alt=""><figcaption></figcaption></figure>

#### Outputs

| Output Name  | Data Type | Description                                                                                                 |
| ------------ | --------- | ----------------------------------------------------------------------------------------------------------- |
| 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**

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2F6JlMFpR0MyK63dQ4PSAf%2Fimage.png?alt=media&#x26;token=267c3ecf-3b37-47ff-9ea9-25653a8f1391" alt=""><figcaption></figcaption></figure>

The Location Sensor returns a list of names of the available BLE devices which can be connected to a Simple List,

### Connect to Device Id

The `Connect to Device Id` function allows your app to connect to a BLE enabled device based in the Id of that device.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FUlJgfu9gZMnXWCDaKs2X%2Fconnect_id.png?alt=media&#x26;token=1c1a8a40-c3be-41da-a470-0e86c35ca463" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name | Data Type | Description                                                     |
| ---------- | --------- | --------------------------------------------------------------- |
| Device Id  | Text      | Expects a text string with the device id you want to connect to |

#### Outputs

| Output Name | Data Type | Description                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| 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` |

### Connect to Device Name

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.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FawDC7iOwXFE5a9jtXDbV%2Fconnect_name.png?alt=media&#x26;token=7272c5d3-c8e1-4406-8027-56d667fa901d" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name  | Data Type | Description                                                    |
| ----------- | --------- | -------------------------------------------------------------- |
| Device Name | Text      | Expects a text block with the name of the device to connect to |

#### Outputs

| Output Name | Data Type | Description                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| 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**

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FIXQ7NGKOrWqEaIFI6nkB%2Fimage.png?alt=media&#x26;token=55f24b93-59bf-4abe-aef6-98ab0a349fbb" alt=""><figcaption></figcaption></figure>

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.

{% hint style="info" %}
It is not possible to connect to a BLE device using the device name without using the Scan block.&#x20;
{% endhint %}

### Subscribe To Characteristics

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.&#x20;

To use this functionality, ensure your app is already connected to the desired Bluetooth device.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FnBmBzSxQM1QV2mzWIJdh%2Fsubscribe%20to%20characteristics.jpg?alt=media&#x26;token=6897c056-0e98-4b44-b842-fb7d72b080d1" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name          | Data Type | Description                                                                           |
| ------------------- | --------- | ------------------------------------------------------------------------------------- |
| characteristic UUID | Text      | Expects a text block with a 32-bit UUID for the BLE type, service or profile required |

#### Outputs

| Output Name | Type | Description                                                             |
| ----------- | ---- | ----------------------------------------------------------------------- |
| error       | Text | If an error message is returned, returns the error. Else returns `null` |

### Characteristics Change

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.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FGsx7qpK6xA0aox5sAGww%2Fwhen%20characteristics%20changes%20notify.jpg?alt=media&#x26;token=b1146456-999e-44aa-880c-8f4b83c4bcea" alt=""><figcaption></figcaption></figure>

Set something to happen when the Characteristics Change.

#### Outputs

| Output Name       | Type                                                                    | Description                                                                  |
| ----------------- | ----------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| data (string)     | Text                                                                    | Returns a string containing the information received from the BLE device     |
| data (byte array) | array ([list](https://docs.thunkable.com/blocks/blocks/lists)) 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**

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FqU81fUa0ZKzWH1VMB0Ab%2Fexample%20notify.png?alt=media&#x26;token=acf8fa73-85aa-4bbb-af60-526e81d08e41" alt=""><figcaption></figcaption></figure>

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.

### Disconnect

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FLk7t8yJ94xoKETssHOsN%2Fdisconnect.png?alt=media&#x26;token=9c8f308b-5842-4bf8-9a15-159f5ba431da" alt=""><figcaption></figcaption></figure>

If connected to another Bluetooth device, this block will disconnect your app from the device.

### Receive String

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.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FW1j0UznPdybUK3CfHYCT%2Freceive_string.png?alt=media&#x26;token=1deca2ff-8b13-4825-84a7-a2477f16ced0" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name          | Data Type | Description                                                                           |
| ------------------- | --------- | ------------------------------------------------------------------------------------- |
| characteristic UUID | Text      | Expects a text block with a 32-bit UUID for the BLE type, service or profile required |

#### Outputs

| Output Name   | Type | Description                                                              |
| ------------- | ---- | ------------------------------------------------------------------------ |
| 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`  |

### Receive Byte Array

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.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FJo4iFV2ZLzkrKYo9QoLU%2Freceive_byte_array.png?alt=media&#x26;token=cb63acef-350c-45a5-b8c1-a55ed79e8dff" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name          | Data Type | Description                                                                           |
| ------------------- | --------- | ------------------------------------------------------------------------------------- |
| characteristic UUID | Text      | Expects a text block with a 32-bit UUID for the BLE type, service or profile required |

#### Outputs

| Output Name       | Data Type                                                               | Description                                                                  |
| ----------------- | ----------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| data (byte array) | array ([list](https://docs.thunkable.com/blocks/blocks/lists)) 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`      |

### Listen for Disconnection

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.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2F0NTkMlB51gRS38TEQ9So%2Flisten_for_disconnection.png?alt=media&#x26;token=873e0b2b-680d-40d5-9454-6e58d5c7e43e" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name | Data Type | Description                                                        |
| ---------- | --------- | ------------------------------------------------------------------ |
| Device Id  | Text      | Unique ID of the bluetooth device to listen for disconnection with |

#### Outputs

| Output Name | Data Type | Description                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| error       | Text      | If an error message is returned, returns the error. Else returns `null` |

### Transmit String

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.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FELjpiJfvUqvHzSIIehS2%2Ftransmit_string.png?alt=media&#x26;token=22024411-db72-4f2b-bf89-2210000be739" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name          | Data Type | Description                                                                               |
| ------------------- | --------- | ----------------------------------------------------------------------------------------- |
| 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            |

#### Outputs

| Output Name | Data Type | Description                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| error       | Text      | If an error message is returned, returns the error. Else returns `null` |

### Transmit Byte Array

Use this block to send data as an array ([list](https://docs.thunkable.com/blocks/blocks/lists)) of bytes to a BLE device.

You need to be connected to another Bluetooth device in order to use this block.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2F8KJxNOHitVNe0u4a32T5%2Ftransmit_byte_array.png?alt=media&#x26;token=735c56c8-e3b2-464d-ac42-f8fa4457c794" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name          | Data Type                                                               | Description                                                                           |
| ------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| characteristic UUID | Text                                                                    | Expects a text block with a 32-bit UUID for the BLE type, service or profile required |
| data                | array ([list](https://docs.thunkable.com/blocks/blocks/lists)) of bytes | Expects a text block with the message that you want to send to your BLE device        |

#### Outputs

| Output Name | Data Type | Description                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| error       | Text      | If an error message is returned, returns the error. Else returns `null` |

### Transmit Hexadecimal

Use this block to send data in [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) format to a BLE device.

You need to be connected to another Bluetooth device in order to use this block.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FKEVheYfJk25YMXLRQGPg%2Fhex.png?alt=media&#x26;token=f3f41b3f-22e2-43d7-8c77-f50bbc702477" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name          | Data Type                                                               | Description                                                                           |
| ------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| 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](https://en.wikipedia.org/wiki/Hexadecimal) format | Expects a text block with the message that you want to send to your BLE device        |

#### Outputs

| Output Name | Data Type | Description                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| error       | Text      | If an error message is returned, returns the error. Else returns `null` |

### Transmit Base64

Use this block to send data in [base64](https://en.wikipedia.org/wiki/Base64) format to a BLE device.

You need to be connected to another Bluetooth device in order to use this block.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2Fjg9ldHwWsnlWoqYBEJxk%2FScreenshot%202024-11-21%20024745.png?alt=media&#x26;token=765cc91c-d31b-42be-8cd7-4d744e05bbf1" alt=""><figcaption></figcaption></figure>

#### Inputs

| Input Name          | Data Type                                                     | Description                                                                           |
| ------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| characteristic UUID | Text                                                          | Expects a text block with a 32-bit UUID for the BLE type, service or profile required |
| data                | data in [base64](https://en.wikipedia.org/wiki/Base64) format | Expects a text block with the message that you want to send to your BLE device        |

#### Outputs

| Output Name | Data Type | Description                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| error       | Text      | If an error message is returned, returns the error. Else returns `null` |

{% hint style="info" %}
All values (string, HEX, Byte array, Base64) are being converted to a byte array before sending.
{% endhint %}
