This API is used via wx.writeBLECharacteristicValue(Object object).
Attribute | Type | Required | Description |
deviceId | string | Yes | Bluetooth Device ID |
serviceId | string | Yes | UUID corresponding to the Bluetooth characteristic service |
characteristicId | string | Yes | Bluetooth characteristic UUID |
value | ArrayBuffer | Yes | The binary data of the characteristic value from the Bluetooth device. |
writeType | string | No | Write mode setting for Bluetooth feature value, there are two modes, iOS priority write and Android priority writeNoResponse. legal value is: write: Mandatory write response, reporting error when unsupported. 'writeNoResponse': Mandatory write without response, reporting error when unsupported. |
success | function | No | Callback Function of Successful Interface Call |
fail | function | No | Callback Function of Successful Interface Call |
complete | function | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Error code | Error Message | Description |
0 | ok | Normal |
-1 | already connect | Connected |
10000 | not init | Bluetooth Adapter Not Initialized |
10001 | not available | The current Bluetooth adapter is unavailable. |
10002 | no device | Specified Device Not Found |
10003 | connection fail | Connection failed. |
10004 | no service | Specified Service Not Found |
10005 | no characteristic | Specified characteristic not found |
10006 | no connection | The current connection has been disconnected. |
10007 | property not support | The current characteristic does not support this operation. |
10008 | system error | Exceptions Reported by All Other Systems |
10009 | system not support | Exclusive to Android system, and BLE is not supported on versions earlier than 4.3. |
10012 | operate time out | Connection timeout |
10013 | invalid_data | The connection deviceId is either null or incorrectly formatted. |
// Transmit a hexadecimal data of 0x00 to the Bluetooth device.let buffer = new ArrayBuffer(1)let dataView = new DataView(buffer)dataView.setUint8(0, 0)wx.writeBLECharacteristicValue({// The deviceId here must be obtained through the getBluetoothDevices or onBluetoothDeviceFound interfaces.deviceId,// The ServiceId here must be obtained from the wx.getBLEDeviceServices interface.serviceId,// The CharacteristicId Here must be obtained from the getBLEDeviceCharacteristics interface.characteristicId,// The value here is of ArrayBuffer type.value: buffer,success (res) {console.log('writeBLECharacteristicValue success', res.errMsg)}})
This API is used via wx.readBLECharacteristicValue(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth Device ID |
serviceId | string | - | Yes | UUID corresponding to the Bluetooth characteristic service |
characteristicId | string | - | Yes | Bluetooth characteristic UUID |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Error code | Error Message | Description |
0 | ok | Normal |
-1 | already connect | Connected |
10000 | not init | Bluetooth Adapter Not Initialized |
10001 | not available | The current Bluetooth adapter is unavailable. |
10002 | no device | Specified Device Not Found |
10003 | connection fail | Connection failed. |
10004 | no service | Specified Service Not Found |
10005 | no characteristic | Specified characteristic not found |
10006 | no connection | The current connection has been disconnected. |
10007 | property not support | The current characteristic does not support this operation. |
10008 | system error | Exceptions Reported by All Other Systems |
10009 | system not support | Exclusive to Android system, and BLE is not supported on versions earlier than 4.3. |
10012 | operate time out | Connection timeout |
10013 | invalid_data | The connection deviceId is either null or incorrectly formatted. |
// Retrieval is only possible in this callback.wx.onBLECharacteristicValueChange(function(characteristic) {console.log('characteristic value comed:', characteristic)})wx.readBLECharacteristicValue({// The deviceId here must have already established a link with the corresponding device through createBLEConnection.deviceId,// The ServiceId here must be obtained from the wx.getBLEDeviceServices interface.serviceId,// The CharacteristicId Here Must be Retrieved from the getBLEDeviceCharacteristics InterfacecharacteristicId,success (res) {console.log('readBLECharacteristicValue:', res.errCode)}})
This API is used via wx.setBLEMTU(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth Device ID |
mtu | number | - | Yes | Maximum Transmission Unit. The setting range is within the interval (22,512), measured in bytes. |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Attribute | Type | Description |
mtu | number | The final negotiated MTU value, which is consistent with the parameter passed in. It is supported from Android client 8.0.9 onwards. |
This API is used via wx.getBLEMTU(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth Device ID |
writeType | string | write | No | Write mode (specific to iOS). Valid values are: write: Write with response. writeNoResponse: Write without response. |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Successful Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Attribute | Type | Description |
mtu | number | Maximum Transmission Unit |
Error code | Error Message | Description |
0 | ok | Normal |
-1 | already connect | Connected |
10000 | not init | Bluetooth Adapter Not Initialized |
10001 | not available | The current Bluetooth adapter is unavailable. |
10002 | no device | Specified Device Not Found |
10003 | connection fail | Connection failed. |
10004 | no service | Specified Service Not Found |
10005 | no characteristic | Specified characteristic not found |
10006 | no connection | The current connection has been disconnected. |
10007 | property not support | The current characteristic does not support this operation. |
10008 | system error | Exceptions Reported by All Other Systems |
10009 | system not support | Exclusive to Android system, and BLE is not supported on versions earlier than 4.3. |
10012 | operate time out | Connection timeout |
10013 | invalid_data | The connection deviceId is either null or incorrectly formatted. |
wx.getBLEMTU({deviceId: '',writeType: 'write',success (res) {console.log(res)}})
This API is used via wx.onBLEMTUChange(function listener).
Attribute | Type | Description |
deviceId | string | Bluetooth Device ID |
mtu | number | Maximum Transmission Unit |
wx.onBLEMTUChange(function (res) {console.log('bluetooth mtu is', res.mtu)})
This method is used via wx.offBLEMTUChange(function listener).
const listener = function (res) { console.log(res) }wx.onBLEMTUChange(listener)wx.offBLEMTUChange(listener) // The same function object as the listener must be passed in.
This API is used via wx.onBLEConnectionStateChange(function listener).
Attribute | Type | Description |
deviceId | string | Bluetooth Device ID |
connected | boolean | Whether it is in connected status. |
wx.onBLEConnectionStateChange(function(res) {// This method callback can be used to handle exceptional situations such as unexpected disconnections.console.log(device $
{
res
.
deviceId
}
state has changed
,
connected
:
$
{
res
.
connected
}
)})
This API is used via wx.offBLEConnectionStateChange(function listener).
const listener = function (res) { console.log(res) }wx.onBLEConnectionStateChange(listener)wx.offBLEConnectionStateChange(listener) // The same function object as the listener must be passed in.
This API is used via wx.onBLECharacteristicValueChange(function listener).
Attribute | Type | Description |
deviceId | string | Bluetooth Device ID |
serviceId | string | UUID corresponding to the Bluetooth characteristic service |
characteristicId | string | Bluetooth characteristic UUID |
value | ArrayBuffer | The latest characteristic value |
// Example of converting ArrayBuffer to hexadecimal stringfunction ab2hex(buffer) {let hexArr = Array.prototype.map.call(new Uint8Array(buffer),function(bit) {return ('00' + bit.toString(16)).slice(-2)})return hexArr.join('');}wx.onBLECharacteristicValueChange(function(res) {console.log(characteristic $
{
res
.
characteristicId
}
has changed
,
now is $
{
res
.
value
}
)console.log(ab2hex(res.value))})
This API is used via wx.offBLECharacteristicValueChange().
wx.offBLECharacteristicValueChange()
This API is used via wx.notifyBLECharacteristicValueChange(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth Device ID |
serviceId | string | - | Yes | UUID corresponding to the Bluetooth characteristic service |
characteristicId | string | - | Yes | Bluetooth characteristic UUID |
state | boolean | - | Yes | Whether to enable "notify" |
type | string | indication | No | Set the characteristic subscription type, whose valid values include "notification" and "indication". |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Error code | Error Message | Description |
0 | ok | Normal |
-1 | already connect | Connected |
10000 | not init | Bluetooth Adapter Not Initialized |
10001 | not available | The current Bluetooth adapter is unavailable. |
10002 | no device | Specified Device Not Found |
10003 | connection fail | Connection failed. |
10004 | no service | Specified Service Not Found |
10005 | no characteristic | Specified characteristic not found |
10006 | no connection | The current connection has been disconnected. |
10007 | property not support | The current characteristic does not support this operation. |
10008 | system error | Exceptions Reported by All Other Systems |
10009 | system not support | Exclusive to Android system, and BLE is not supported on versions earlier than 4.3. |
10012 | operate time out | Connection timeout |
10013 | invalid_data | The connection deviceId is either null or incorrectly formatted. |
// Transmit a hexadecimal data of 0x00 to the Bluetooth device.let buffer = new ArrayBuffer(1)let dataView = new DataView(buffer)dataView.setUint8(0, 0)wx.writeBLECharacteristicValue({// The deviceId here must be obtained through the getBluetoothDevices or onBluetoothDeviceFound interfaces.deviceId,// The ServiceId here must be obtained from the wx.getBLEDeviceServices interface.serviceId,// The CharacteristicId Here Must be Retrieved from the getBLEDeviceCharacteristics InterfacecharacteristicId,// The Value Here is of ArrayBuffer Typevalue: buffer,success (res) {console.log('writeBLECharacteristicValue success', res.errMsg)}})
This API is used via wx.getBLEDeviceServices(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth device id. A connection must have already been established through wx.createBLEConnection. |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Attribute | Type | Description |
services | Array.<Object> | Device service list |
Attribute | Type | Description |
uuid | string | Bluetooth device service UUID |
isPrimary | boolean | Whether the service is a primary service. |
wx.getBLEDeviceServices({// The deviceId here must have already established a link with the corresponding device through wx.createBLEConnection.deviceId,success (res) {console.log('device services:', res.services)}})
This API is used via wx.getBLEDeviceRSSI(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth Device ID |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Attribute | Type | Description |
RSSI | Number | Signal strength, measured in dBm |
This API is used via wx.getBLEDeviceCharacteristics(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth device id. A connection must have already been established through wx.createBLEConnection. |
serviceId | string | - | Yes | |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Attribute | Type | Description |
characteristics | Array.<Object> | Device characteristic value list |
Structure attributes | Type | Description |
uuid | string | Bluetooth device characteristic UUID |
properties | Object | Operation types supported by this characteristic |
Attribute | Type | Description |
read | boolean | Whether this characteristic supports the read operation. |
write | boolean | Whether this characteristic supports the write operation. |
notify | boolean | Whether this characteristic supports the notify operation. |
indicate | boolean | Whether this characteristic supports the indicate operation. |
writeNoResponse | boolean | Whether this characteristic supports the writeNoResponse operation. |
writeDefault | boolean | Whether this characteristic supports the writeDefault operation. |
Error code | Error Message | Description |
0 | ok | Normal |
-1 | already connect | Connected |
10000 | not init | Bluetooth Adapter Not Initialized |
10001 | not available | The current Bluetooth adapter is unavailable. |
10002 | no device | Specified Device Not Found |
10003 | connection fail | Connection failed. |
10004 | no service | Specified Service Not Found |
10005 | no characteristic | Specified characteristic not found |
10006 | no connection | The current connection has been disconnected. |
10007 | property not support | The current characteristic does not support this operation. |
10008 | system error | Exceptions Reported by All Other Systems |
10009 | system not support | Exclusive to Android system, and BLE is not supported on versions earlier than 4.3. |
10012 | operate time out | Connection timeout |
10013 | invalid_data | The connection deviceId is either null or incorrectly formatted. |
wx.getBLEDeviceCharacteristics({// The deviceId here needs to have already established a link with the corresponding device through wx.createBLEConnectiondeviceId,// The ServiceId here must be obtained from the wx.getBLEDeviceServices interface.serviceId,success (res) {console.log('device getBLEDeviceCharacteristics:', res.characteristics)}})
This API is used via wx.createBLEConnection(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth Device ID |
timeout | number | - | No | Timeout period, in milliseconds (ms). If left blank, it indicates that there will be no timeout. |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Error code | Error Message | Description |
0 | ok | Normal |
-1 | already connect | Connected |
10000 | not init | Bluetooth Adapter Not Initialized |
10001 | not available | The current Bluetooth adapter is unavailable. |
10002 | no device | Specified Device Not Found |
10003 | connection fail | Connection failed. |
10004 | no service | Specified Service Not Found |
10005 | no characteristic | Specified characteristic not found |
10006 | no connection | The current connection has been disconnected. |
10007 | property not support | The current characteristic does not support this operation. |
10008 | system error | Exceptions Reported by All Other Systems |
10009 | system not support | Exclusive to Android system, and BLE is not supported on versions earlier than 4.3. |
10012 | operate time out | Connection timeout |
10013 | invalid_data | The connection deviceId is either null or incorrectly formatted. |
wx.createBLEConnection({deviceId,success (res) {console.log(res)}})
This API is used wx.closeBLEConnection(Object object).
Attribute | Type | Default value | Required | Description |
deviceId | string | - | Yes | Bluetooth Device ID |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Error code | Error Message | Description |
0 | ok | Normal |
-1 | already connect | Connected |
10000 | not init | Bluetooth Adapter Not Initialized |
10001 | not available | The current Bluetooth adapter is unavailable. |
10002 | no device | Specified Device Not Found |
10003 | connection fail | Connection failed. |
10004 | no service | Specified Service Not Found |
10005 | no characteristic | Specified characteristic not found |
10006 | no connection | The current connection has been disconnected. |
10007 | property not support | The current characteristic does not support this operation. |
10008 | system error | Exceptions Reported by All Other Systems |
10009 | system not support | Exclusive to Android system, and BLE is not supported on versions earlier than 4.3. |
10012 | operate time out | Connection timeout |
10013 | invalid_data | The connection deviceId is either null or incorrectly formatted. |
wx.closeBLEConnection({deviceId,success (res) {console.log(res)}})
Was this page helpful?