> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rebellapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# my.connectBLEDevice

> Connect to a Bluetooth Low Energy device.

Use this API to establish a connection to a Bluetooth Low Energy (BLE) device.

## Parameters

| Property | Type     | Required | Description                      |
| -------- | -------- | -------- | -------------------------------- |
| deviceId | String   | Yes      | Device identifier from discovery |
| success  | Function | No       | Callback on success              |
| fail     | Function | No       | Callback on failure              |
| complete | Function | No       | Callback that always executes    |

If the specified Bluetooth device is already connected, a repeated connection request returns success directly.

## Code Example

### Basic Usage

```javascript theme={null}
my.connectBLEDevice({
  deviceId: 'XX:XX:XX:XX:XX:XX',
  success() {
    console.log('Connected to device');
  },
  fail(err) {
    console.error('Connection failed:', err);
  }
});
```

### Complete Connection Flow

```javascript theme={null}
Page({
  data: {
    connectedDevice: null
  },

  connectToDevice(device) {
    my.showLoading({ content: 'Connecting...' });

    my.connectBLEDevice({
      deviceId: device.deviceId,
      success: () => {
        my.hideLoading();
        this.setData({ connectedDevice: device });
        my.showToast({ content: `Connected to ${device.name}` });
        this.discoverServices(device.deviceId);
      },
      fail: (err) => {
        my.hideLoading();
        my.alert({
          title: 'Connection Failed',
          content: 'Could not connect to device. Please try again.'
        });
      }
    });
  },

  discoverServices(deviceId) {
    my.getBLEDeviceServices({
      deviceId: deviceId,
      success: (res) => {
        console.log('Services:', res.services);
      }
    });
  }
});
```

## Error Codes

| Code  | Description                              |
| ----- | ---------------------------------------- |
| 10000 | The Bluetooth adapter is not initialized |
| 10001 | The Bluetooth adapter is not available   |
| 10002 | Device not found                         |
| 10003 | Connection failed                        |
| 10006 | Connection lost                          |
| 10015 | Timeout                                  |

<Warning>
  Ensure the device is in range and advertising before attempting to connect. Use discovery to verify the device is available.
</Warning>

## Related APIs

<CardGroup cols={2}>
  <Card title="my.disconnectBLEDevice" icon="link-slash" href="/jsapi/device/bluetooth/my.disconnectBLEDevice">
    Disconnect from device
  </Card>

  <Card title="my.onBluetoothDeviceFound" icon="magnifying-glass" href="/jsapi/device/bluetooth/my.onBluetoothDeviceFound">
    Discover devices
  </Card>
</CardGroup>
