---
url: /api-proxy/device/bluetooth-ble/createBLEConnection.md
---
mpx.createBLEConnection(Object object)

连接蓝牙低功耗设备。

若小程序在之前已有搜索过某个蓝牙设备，并成功建立连接，可直接传入之前搜索获取的 deviceId 直接尝试连接该设备，无需再次进行搜索操作。

支持情况： 微信、支付宝

[参考文档](https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-ble/wx.createBLEConnection.html)

### 参数 {#parameters}

**Object object**

| 属性 | 类型 | 默认值 | 必填 | 说明 | 支付宝 |
| --- | --- | --- | --- | --- | --- |
| deviceId | string |  | 是 | 蓝牙设备 id | **✓** |
| timeout | number |  | 否 | 超时时间，单位 ms，不填表示不会超时 | **✗** |
| success | function |  | 否 | 接口调用成功的回调函数 | **✓** |
| fail | function |  | 否 | 接口调用失败的回调函数 | **✓** |
| complete | function |  | 否 | 接口调用结束的回调函数（调用成功、失败都会执行） | **✓** |

> RN 平台未传 `deviceId` 时进入 `fail` 回调，`errMsg` 为 `createBLEConnection:fail parameter error`，随后以同一结果调用 `complete`。

### 错误 {#error}

| 错误码 | 错误信息 | 说明 | 支付宝 |
| --- | --- | --- | --- |
| 0 | ok | 正常 | **✗** |
| -1 | already connect | 已连接 | **✗** |
| 10000 | not init | 未初始化蓝牙适配器 | **✓** |
| 10001 | not available | 当前蓝牙适配器不可用 | **✓** |
| 10002 | no device | 没有找到指定设备 | **✓** |
| 10003 | connection fail | 连接失败 | **✓** |
| 10004 | no service | 没有找到指定服务 | **✓** |
| 10005 | no characteristic | 没有找到指定特征 | **✓** |
| 10006 | no connection | 当前连接已断开 | **✓** |
| 10007 | property not support | 当前特征不支持此操作 | **✓** |
| 10008 | system error | 其余所有系统上报的异常 | **✓** |
| 10009 | system not support | Android 系统特有，系统版本低于 4.3 不支持 BLE | **✓** |
| 10012 | operate time out | 连接超时 | **✓** |
| 10013 | invalid\_data | 连接 deviceId 为空或者是格式不正确 | **✓** |

### 示例代码 {#example-code}

```js
mpx.createBLEConnection({
  deviceId,
  success (res) {
    console.log(res)
  }
})
```
