Platform difference description for low power Bluetooth APIs
Mini App | H5 |
---|---|
√ | x |
2.8.4+
Set the Bluetooth maximum transmission unit. It needs to be called after the successful invocation of uni.createBLEConnection with the mtu setting range of (22,512). Valid for Android 5.1 and above.
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Id used to distinguish the devices | |
mtu | number | Yes | Within the maximum transmission unit (22,512) interval, in byte. | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Write binary data into the characteristic value of the low power Bluetooth devices. Note: Successful invocation requires the characteristic value of the device supporting write.
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Bluetooth device id | |
serviceId | string | Yes | uuid of the service corresponding to Bluetooth characteristic value | |
characteristicId | string | Yes | uuid of Bluetooth characteristic value | |
value | ArrayBuffer | Yes | Binary value corresponding to the characteristic value of Bluetooth device | |
writeType | string | is the writing mode setting of | Bluetooth eigenvalues. There are two modes, iOS prioritizes write, and Android prioritizes writeNoResponse . | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
writeType
Properties | Description |
---|---|
write | Forcibly reply writing, an error is reported when it is not supported |
writeNoResponse | Force no reply write, report an error if not supported |
Error code | Error message | Instruction |
---|---|---|
0 | ok | Normal |
10000 | not init | Bluetooth is not initialized |
10001 | not available | The bluetooth adapter is currently unavailable. |
10002 | no device | Specified device not found |
10003 | connection fail | Connection failed |
10004 | no service | Specified service not found |
10005 | no characteristic | Specified feature value not found |
10006 | no connection | The current connection is disconnected |
10007 | property not support | The current characteristic value does not support this operation. |
10008 | system error | Exceptions reported by all other systems |
10009 | system not support | Android system-specific. BLE is not available on system version lower than 4.3. |
10010 | already connect | Connected |
10011 | need pin | Pairing device requires pairing code |
10012 | operate time out | Connection timed out |
10013 | invalid_data | The connection deviceId is empty or in an incorrect format |
writeBLECharacteristicValue
interface will be called immediately after the successful call of notifyBLECharacteristicValueChange
, and the system error 10008 will occur on some models//Send a 0x00 hexadecimal data to the Bluetooth device
const buffer = new ArrayBuffer(1)
const dataView = new DataView(buffer)
dataView.setUint8(0, 0)
uni.writeBLECharacteristicValue({
//The deviceId here needs to be obtained in the getBluetoothDevices or onBluetoothDeviceFound interface.
deviceId,
//The serviceId here needs to be obtained in the getBLEDeviceServices interface.
serviceId,
//The characteristicId here needs to be obtained in the getBLEDeviceCharacteristics interface.
characteristicId,
//The value here is of type ArrayBuffer
value: buffer,
success(res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
}
})
Read the binary data value of the characteristic value of the low power Bluetooth devices. Note: Successful invocation requires the characteristic value of the device supporting read.
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Bluetooth device id | |
serviceId | string | Yes | uuid of the service corresponding to Bluetooth characteristic value | |
characteristicId | string | Yes | uuid of Bluetooth characteristic value | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Error code | Error message | Instruction |
---|---|---|
0 | ok | Normal |
10000 | not init | Bluetooth is not initialized |
10001 | not available | The bluetooth adapter is currently unavailable. |
10002 | no device | Specified device not found |
10003 | connection fail | Connection failed |
10004 | no service | Specified service not found |
10005 | no characteristic | Specified feature value not found |
10006 | no connection | The current connection is disconnected |
10007 | property not support | The current characteristic value does not support this operation. |
10008 | system error | Exceptions reported by all other systems |
10009 | system not support | Android system-specific. BLE is not available on system version lower than 4.3. |
10010 | already connect | Connected |
10011 | need pin | Pairing device requires pairing code |
10012 | operate time out | Connection timed out |
10013 | invalid_data | The connection deviceId is empty or in an incorrect format |
onBLECharacteristicValueChange
method.//Only callbacks here can be obtained.
uni.onBLECharacteristicValueChange(function (characteristic) {
console.log('characteristic value comed:', characteristic)
})
uni.readBLECharacteristicValue({
//The deviceId here needs to have established a link with the corresponding device through createBLEConnection.
deviceId,
//The serviceId here needs to be obtained in the getBLEDeviceServices interface.
serviceId,
//The characteristicId here needs to be obtained in the getBLEDeviceCharacteristics interface.
characteristicId,
success(res) {
console.log('readBLECharacteristicValue:', res.errCode)
}
})
listen to the change events of low power Bluetooth connection status. Include developers actively connecting or disconnecting, device loss, abnormal disconnection and so on.
CALLBACK return parameter
Attribute | Type | Instruction |
---|---|---|
deviceId | string | Bluetooth device ID |
connected | boolean | Is it connected? |
uni.onBLEConnectionStateChange(function (res) {
//The callback of this method can be used to handle abnormal situations such as unexpected disconnection and so on.
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})
listen to the characteristic value change events of the low power Bluetooth devices. You must first enable the notifyBLECharacteristicValueChange
interface to receive notification pushed by the device.
CALLBACK return parameter
Attribute | Type | Instruction |
---|---|---|
deviceId | string | Bluetooth device id |
serviceId | string | uuid of the service corresponding to Bluetooth characteristic value |
characteristicId | string | uuid of Bluetooth characteristic value |
value | ArrayBuffer | Latest value of the eigenvalue |
// Example for ArrayBuffer transformed to hexadecimal string
function ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}
uni.onBLECharacteristicValueChange(function (res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
console.log(ab2hex(res.value))
})
Enable the notify function when the characteristic values of low power Bluetooth device changes, and subscribe to the characteristic values.
Notice: Only when the characteristic value of the necessary device supports notify or indicate can it be called successfully. In addition, you must first enable notifyBLECharacteristicValueChange
to listen to device characteristicValueChange
events
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Bluetooth device id | |
serviceId | string | Yes | uuid of the service corresponding to Bluetooth characteristic value | |
characteristicId | string | Yes | uuid of Bluetooth characteristic value | |
state | boolean | Yes | Enable notify or not | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Error code | Error message | Instruction |
---|---|---|
0 | ok | Normal |
10000 | not init | Bluetooth is not initialized |
10001 | not available | The bluetooth adapter is currently unavailable. |
10002 | no device | Specified device not found |
10003 | connection fail | Connection failed |
10004 | no service | Specified service not found |
10005 | no characteristic | Specified feature value not found |
10006 | no connection | The current connection is disconnected |
10007 | property not support | The current characteristic value does not support this operation. |
10008 | system error | Exceptions reported by all other systems |
10009 | system not support | Android system-specific. BLE is not available on system version lower than 4.3. |
10010 | already connect | Connected |
10011 | need pin | Pairing device requires pairing code |
10012 | operate time out | Connection timed out |
10013 | invalid_data | The connection deviceId is empty or in an incorrect format |
uni.onBLECharacteristicValueChange
callback.writeBLECharacteristicValue
interface will be called immediately after the successful call of notifyBLECharacteristicValueChange
, and the system error 10008 will occur on some modelsuni.notifyBLECharacteristicValueChange({
//The deviceId here needs to have established a link with the corresponding device through createBLEConnection.
deviceId,
//The serviceId here needs to be obtained in the getBLEDeviceServices interface.
serviceId,
//The characteristicId here needs to be obtained in the getBLEDeviceCharacteristics interface.
characteristicId,
success(res) {
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
}
})
Obtain all services of the Bluetooth devices.
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Bluetooth device id | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Success return parameter description:
Attribute | Type | Instruction |
---|---|---|
services | Array<Object> | Equipment service list |
Structure of res.services
Attribute | Type | Instruction |
---|---|---|
uuid | string | uuid of Bluetooth device service |
isPrimary | boolean | Whether the service is a primary service |
Error code | Error message | Instruction |
---|---|---|
0 | ok | Normal |
10000 | not init | Bluetooth is not initialized |
10001 | not available | The bluetooth adapter is currently unavailable. |
10002 | no device | Specified device not found |
10003 | connection fail | Connection failed |
10004 | no service | Specified service not found |
10005 | no characteristic | Specified feature value not found |
10006 | no connection | The current connection is disconnected |
10007 | property not support | The current characteristic value does not support this operation. |
10008 | system error | Exceptions reported by all other systems |
10009 | system not support | Android system-specific. BLE is not available on system version lower than 4.3. |
10010 | already connect | Connected |
10011 | need pin | Pairing device requires pairing code |
10012 | operate time out | Connection timed out |
10013 | invalid_data | The connection deviceId is empty or in an incorrect format |
uni.getBLEDeviceServices({
//The deviceId here needs to have established a link with the corresponding device through createBLEConnection.
deviceId,
success(res) {
console.log('device services:', res.services)
}
})
2.8.4+
Get the signal strength of Bluetooth device.
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Bluetooth device id | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Obtain all characteristic values of a certain service of that Bluetooth device.
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Bluetooth device id | |
serviceId | string | Yes | Bluetooth service uuid needs to be obtained using getBLEDeviceServices | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Success return parameter description:
| Attribute| Type| Instruction| |---|---|---|---| | characteristics| Array<Object>| Equipment service list|
Structure of res.characteristics
Attribute | Type | Instruction |
---|---|---|
uuid | string | uuid of Bluetooth device's characteristic value |
properties | Object | Operation types supported by this characteristic value |
Structure of properties
Attribute | Type | Instruction |
---|---|---|
read | boolean | Does this characteristic value support read operation |
write | boolean | Does this characteristic value support write operation |
notify | boolean | Does this characteristic value support notify operation |
indicate | boolean | Does this characteristic value support indicate operation |
Error code | Error message | Instruction |
---|---|---|
0 | ok | Normal |
10000 | not init | Bluetooth is not initialized |
10001 | not available | The bluetooth adapter is currently unavailable. |
10002 | no device | Specified device not found |
10003 | connection fail | Connection failed |
10004 | no service | Specified service not found |
10005 | no characteristic | Specified feature value not found |
10006 | no connection | The current connection is disconnected |
10007 | property not support | The current characteristic value does not support this operation. |
10008 | system error | Exceptions reported by all other systems |
10009 | system not support | Android system-specific. BLE is not available on system version lower than 4.3. |
uni.getBLEDeviceCharacteristics({
//The deviceId here needs to have established a link with the corresponding device through createBLEConnection.
deviceId,
//The serviceId here needs to be obtained in the getBLEDeviceServices interface.
serviceId,
success(res) {
console.log('device getBLEDeviceCharacteristics:', res.characteristics)
}
})
Connect low power Bluetooth devices.
If the APP has searched for a Bluetooth device beforehand and successfully connected, you can directly transmit the deviceId obtained by the previous search and directly try to connect to the device without searching again.
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Id used to distinguish the devices | |
timeout | number | No | Timeout time in ms | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Error code | Error message | Instruction |
---|---|---|
0 | ok | Normal |
10000 | not init | Bluetooth is not initialized |
10001 | not available | The bluetooth adapter is currently unavailable. |
10002 | no device | Specified device not found |
10003 | connection fail | Connection failed |
10004 | no service | Specified service not found |
10005 | no characteristic | Specified feature value not found |
10006 | no connection | The current connection is disconnected |
10007 | property not support | The current characteristic value does not support this operation. |
10008 | system error | Exceptions reported by all other systems |
10009 | system not support | Android system-specific. BLE is not available on system version lower than 4.3. |
10010 | already connect | Connected |
10011 | need pin | Pairing device requires pairing code |
10012 | operate time out | Connection timed out |
10013 | invalid_data | The connection deviceId is empty or in an incorrect format |
createBLEConnection
and closeBLEConnection
interfaces in pairs as much as possible. If Android calls createBLEConnection
multiple times to create a connection, the system may hold multiple connection instances of the same device, resulting in the failure to truly disconnect from the device when calling closeBLEConnection
.uni.onBLEConnectionStateChange
callback event and perform reconnect operation as needed when Bluetooth device is disconnecteduni.createBLEConnection({
//The deviceId here needs to have established a link with the corresponding device through createBLEConnection.
deviceId,
success(res) {
console.log(res)
}
})
Disconnect from low power Bluetooth devices.
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
deviceId | string | Yes | Id used to distinguish the devices | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Error code | Error message | Instruction |
---|---|---|
0 | ok | Normal |
10000 | not init | Bluetooth is not initialized |
10001 | not available | The bluetooth adapter is currently unavailable. |
10002 | no device | Specified device not found |
10003 | connection fail | Connection failed |
10004 | no service | Specified service not found |
10005 | no characteristic | Specified feature value not found |
10006 | no connection | The current connection is disconnected |
10007 | property not support | The current characteristic value does not support this operation. |
10008 | system error | Exceptions reported by all other systems |
10009 | system not support | Android system-specific. BLE is not available on system version lower than 4.3. |
10010 | already connect | Connected |
10011 | need pin | Pairing device requires pairing code |
10012 | operate time out | Connection timed out |
10013 | invalid_data | The connection deviceId is empty or in an incorrect format |
uni.closeBLEConnection({
deviceId,
success(res) {
console.log(res)
}
})