> For the complete documentation index, see [llms.txt](https://docs.thunkable.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.thunkable.com/~/revisions/A81b2aVlSXGVVaRtdFDr/bluetooth-low-energy.md).

# Bluetooth Low Energy

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

![](https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-MMGsCOQmypJUYH0PDNJ%2F-MMH0gFW8yFJbjVG9Ipx%2Fadvanced-components.png?alt=media\&token=ee091119-5faf-43dd-844f-a7308d9ce0dd)

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](/~/revisions/A81b2aVlSXGVVaRtdFDr/bluetooth-low-energy.md#scan)
* [Connect to Device Id](/~/revisions/A81b2aVlSXGVVaRtdFDr/bluetooth-low-energy.md#connect-to-device-id)
* [Connect to Device Name](/~/revisions/A81b2aVlSXGVVaRtdFDr/bluetooth-low-energy.md#connect-to-device-name)
* [Disconnect](/~/revisions/A81b2aVlSXGVVaRtdFDr/bluetooth-low-energy.md#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%2FUwbG73RWrsoHo1en2wB1%2Fscan.png?alt=media&amp;token=c9a35d96-fe2c-47bb-9598-177e150f0d83" 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 |
| error        | Text      | Returns an error message from the function           |

### 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&amp;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&amp;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` |

### 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&amp;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&amp;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&amp;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](/~/revisions/A81b2aVlSXGVVaRtdFDr/lists.md)) 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&amp;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&amp;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](/~/revisions/A81b2aVlSXGVVaRtdFDr/lists.md)) 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&amp;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](/~/revisions/A81b2aVlSXGVVaRtdFDr/lists.md)) 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&amp;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` |
