title: Taro.createBLEConnection(option)
sidebar_label: createBLEConnection
Connects to a BLE device.
If the Mini Program has previously discovered a Bluetooth device and successfully connected to it, you can directly pass the deviceId obtained previously to connect to the device, without the need to perform a search operation.
Notes
- Ensure the calls to the createBLEConnection and
closeBLEConnection
APIs are paired. On an Android device, if you callcreateBLEConnection
multiple times, the system may maintain multiple connection instances for the same device. Therefore, callingcloseBLEConnection
will not truly disconnect from the device. - Bluetooth connections may be dropped at any time, so you should listen on the
Taro.onBLEConnectionStateChange
callback event. If the connection with a Bluetooth device is lost, you can reconnect it as needed. - If you call data read/write APIs for an unconnected or a disconnected device, a 10006 error is returned. We recommend you reconnect the device in such cases.
Type
(option: Option) => Promise<Promised>
Parameters
Promised
Property | Type | Description |
---|---|---|
errMsg | string |
success: ok,fail: error message |
Option
Property | Type | Required | Description |
---|---|---|---|
deviceId | string |
Yes | Device ID |
timeout | number |
No | The timeout time (in ms). If it is left empty, there is no timeout. |
complete | (res: any) => void |
No | The callback function used when the API call completed (always executed whether the call succeeds or fails) |
fail | (res: any) => void |
No | The callback function for a failed API call |
success | (res: Result) => void |
No | The callback function for a successful API call |
Sample Code
Taro.createBLEConnection({
// This deviceId must have been used to connect the appropriate device via createBLEConnection.
deviceId,
success: function (res) {
console.log(res)
}
})
API Support
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
Taro.createBLEConnection | ✔️ |