Bluetooth adapter interface
Base library version 1.1.0 starts to support, and low versions need to be
compatible
iOS WeChat Client Version 6.5.6 is supported, and Android Client is not currently supported
wx.openBluetoothAdapter(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Initialize the Bluetooth adapter
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
success
|
Function
|
Is
|
Success returns successful initialization information
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
Example code:
wx.openBluetoothAdapter({
success: function (res) {
console.log(res)
}
})
Bug & Tip
-
tip
: Bluetooth debugging is currently only supported on the mac version of the development tool due to system issues
-
tip
: Base library version 1.1.0 starts to support, and the lower version needs to be
compatible
wx.closeBluetoothAdapter(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Turn off the Bluetooth module.
Calling the method breaks all established links and frees up system resources
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
success
|
Function
|
Yes
|
Success, return to successfully shut down module information
|
fail
|
Function
|
no
|
Interface call failed callback function
|
complete
|
Function
|
no
|
The callback function ended in the interface call (the call is successful, failed)
|
Sample code:
wx.closeBluetoothAdapter({
success: function (res) {
console.log(res)
}
})
wx.getBluetoothAdapterState(OBJECT)
Basic reservations version 1.1.0 began support, low version needs to be done
Compatibility
Get the status of the native Bluetooth adapter
Object parameter description:
Parameters
|
Type
|
Required
|
Description
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
discovering
|
Boolean
|
Whether the device is being searched
|
available
|
Boolean
|
Whether the Bluetooth adapter is available
|
errMsg
|
String
|
Success: ok, error: details
|
Example code:
wx.getBluetoothAdapterState({
success: function (res) {
console.log(res)
}
})
wx.onBluetoothAdapterStateChange(CALLBACK)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Listen for bluetooth adapter status change events
CALLBACK parameter description:
Parameters
|
Type
|
Description
|
available
|
boolean
|
Whether the Bluetooth adapter is available
|
discovering
|
boolean
|
Whether the Bluetooth adapter is searching
|
Example code:
wx.onBluetoothAdapterStateChange(function(res) {
console.log(`adapterState changed, now is`, res)
})
wx.startBluetoothDevicesDiscovery(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Start searching for nearby Bluetooth peripherals.
Note that this is more resource-intensive, call the stop method to stop the search after searching and connecting to the device.
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
services
|
Array
|
Whether
|
A list of uuids for the Bluetooth device's main service
|
allowDuplicatesKey
|
boolean
|
Whether
|
Whether to allow repeated escalation of the same device, if repeated escalation is allowed, the onDeviceFound method escalates the same device multiple times, but the RSSI values will vary
|
interval
|
integer
|
Whether
|
The interval between escalation devices, which defaults to 0, means that new devices are found to escalate immediately, otherwise they are escalated according to the incoming interval
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
Service
Parameter Description: Some Bluetooth devices broadcast the uuid of their main service.
If the array is passed in here, only devices with this primary service are searched based on the uuid list.
success returns parameters:
Parameters
|
Type
|
Description
|
errMsg
|
string
|
Success: ok, error: details
|
isDiscovering
|
boolean
|
Whether the current Bluetooth adapter is searching
|
Example code:
wx.startBluetoothDevicesDiscovery({
services: ['FEE7'],
success: function (res) {
console.log(res)
}
})
wx.stopBluetoothDevicesDiscovery(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Stop searching for nearby Bluetooth peripherals.
Call this method to stop the search after making sure you find the device you need to connect to.
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
errMsg
|
string
|
Success: ok, error: details
|
adapterState
Bluetooth adapter status information
Parameters
|
Type
|
Description
|
discovering
|
boolean
|
Whether the device is being searched
|
available
|
boolean
|
Whether the Bluetooth adapter is available
|
Example code:
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log(res)
}
})
wx.getBluetoothDevices(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Get all discovered Bluetooth devices, including those that are already connected to the machine
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
devices
|
Array
|
The list of connected devices for uuid
|
errMsg
|
string
|
Success: ok, error: details
|
The device object
Bluetooth device information
Parameters
|
Type
|
Description
|
name
|
string
|
Bluetooth device name, some devices may not have
|
deviceId
|
string
|
The id used to distinguish the device
|
Rssi
|
Int
|
The signal strength of the current Bluetooth device
|
advertisData
|
ArrayBuffer
|
The broadcast content of the current
Bluetooth device (Note: vConsole cannot print arrayBuffer type data).
|
Example code:
wx.getBluetoothDevices({
success: function (res) {
console.log(res)
}
})
Bug & Tip
-
tip
Your Mac system may not be
advertisData
and
RSSI
please use real debugging
-
tip
The
deviceId
obtained on the developer tool and Android is the device MAC address, and on iOS is the device uuid.
deviceId
cannot be hard-coded into code
wx.getConnectedBluetoothDevices(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be
compatible
Get a connected device based on uuid
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
services
|
Array
|
Is
|
A list of uuids for the Bluetooth device's main service
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
devices
|
Array
|
The list of devices searched for
|
errMsg
|
string
|
Success: ok, error: details
|
The device object
Bluetooth device information
Parameters
|
Type
|
Description
|
name
|
string
|
Bluetooth device name, some devices may not have
|
deviceId
|
string
|
The id used to distinguish the device
|
Example code:
wx.getConnectedBluetoothDevices({
success: function (res) {
console.log(res)
}
})
Bug & Tip
-
tip
The
deviceId
obtained on the developer tool and Android is the device MAC address, and on iOS is the device uuid.
deviceId
cannot be hard-coded into code
wx.onBluetoothDeviceFound(CALLBACK)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Listen for events that find a new device
CALLBACK parameter description:
Parameters
|
Type
|
Description
|
devices
|
Array
|
The list of newly searched devices
|
The device object
Parameters
|
Type
|
Description
|
deviceId
|
string
|
Bluetooth device id, reference device object
|
name
|
string
|
Bluetooth device name, reference device object
|
Rssi
|
Int
|
The signal strength of the current Bluetooth device
|
advertisData
|
ArrayBuffer
|
The broadcast content of the current
Bluetooth device (Note: vConsole cannot print arrayBuffer type data).
|
Example code:
wx.onBluetoothDeviceFound(function(devices) {
console.log('new device list has founded')
console.dir(devices)
})
Bug & Tip
-
tip
Your Mac system may not be
advertisData
and
RSSI
please use real debugging
-
tip
The
deviceId
obtained on the developer tool and Android is the device MAC address, and on iOS is the device uuid.
deviceId
cannot be hard-coded into code
Bluetooth low energy interface
wx.createBLEConnection(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Connect a Bluetooth Low Energy device
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
deviceId
|
string
|
Is
|
Bluetooth device id, refer to the getDevices interface
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
errMsg
|
string
|
Success: ok, error: details
|
Example code:
wx.createBLEConnection({
deviceId: deviceId,
success: function (res) {
console.log(res)
}
})
wx.closeBLEConnection(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Disconnect from your Bluetooth low-power device
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
deviceId
|
string
|
Is
|
Bluetooth device id, refer to the getDevices interface
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
errMsg
|
string
|
Success: ok, error: details
|
Example code:
wx.closeBLEConnection({
success: function (res) {
console.log(res)
}
})
wx.getBLEDeviceServices(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Get all service (services) for Bluetooth devices
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
deviceId
|
string
|
Is
|
Bluetooth device id, refer to the getDevices interface
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
services
|
array
|
The list of device services
|
errMsg
|
string
|
Success: ok, error: details
|
The service object
Bluetooth device service (service) information
Parameters
|
Type
|
Description
|
Uuid
|
string
|
The uuid of the Bluetooth device service
|
isPrimary
|
boolean
|
Whether the service is primary
|
Example code:
wx.getBLEDeviceServices({
deviceId: deviceId,
success: function (res) {
console.log('device services:', res.services)
}
})
wx.getBLEDeviceCharacteristics(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Get all characteristics of a Bluetooth device (feature value)
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
deviceId
|
string
|
Is
|
Bluetooth device id, reference device object
|
serviceId
|
string
|
Is
|
Bluetooth service uuid
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
characteristics
|
array
|
A list of device feature values
|
errMsg
|
string
|
Success: ok, error: details
|
The level object
Bluetooth device acteristic (feature value) information
Parameters
|
Type
|
Description
|
Uuid
|
string
|
The uuid of the Bluetooth device feature value
|
properties
|
object
|
The type of operation supported by the feature value
|
The properties object
Parameters
|
Type
|
Description
|
read
|
boolean
|
Whether the feature value supports read operations
|
write
|
boolean
|
Whether the feature value supports write operations
|
notify
|
boolean
|
Whether the feature value supports notify operations
|
indicate
|
boolean
|
Whether the feature value supports the signature operation
|
Example code:
wx.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: serviceId,
success: function (res) {
console.log('device getBLEDeviceCharacteristics:', res.characteristics)
}
})
wx.readBLECharacteristicValue(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Read the binary data value of the feature value of a Bluetooth low-power device.
Note: The feature value of the device must support
read
in order to be successfully called, referring specifically to the properties of the characteristic
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
deviceId
|
string
|
Is
|
Bluetooth device id, reference device object
|
serviceId
|
string
|
Is
|
The Bluetooth feature value corresponds to the service's uuid
|
characteristicId
|
string
|
Is
|
The uuid of the Bluetooth feature value
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
characteristic
|
object
|
Device feature value information
|
errMsg
|
string
|
Success: ok, error: details
|
The level object
Bluetooth device acteristic (feature value) information
Parameters
|
Type
|
Description
|
characteristicId
|
string
|
The uuid of the Bluetooth device feature value
|
serviceId
|
object
|
The Bluetooth device feature value corresponds to the service's uuid
|
value
|
ArrayBuffer
|
The binary value for the Bluetooth device feature
value (Note: vConsole cannot print arrayBuffer type data).
|
Example code:
wx.onBLECharacteristicValueChange(function(characteristic) {
console.log('characteristic value comed:', characteristic)
})
wx.readBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
success: function (res) {
console.log('readBLECharacteristicValue:', res.characteristic.value)
}
})
Bug & Tip
-
tip
: There is a possibility that the read-write interface will fail to read and write multiple times in parallel.
-
tip
:
read
read by the read interface
onBLECharacteristicValueChange
method.
wx.writeBLECharacteristicValue(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Write binary data to bluetooth low-power device feature values.
Note: The feature value of the device must support
write
in order to be successfully called, referring specifically to the properties of the characteristic
tips: There is a possibility that the read-write interface will fail to read and write multiple times in parallel
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
deviceId
|
string
|
Is
|
Bluetooth device id, reference device object
|
serviceId
|
string
|
Is
|
The Bluetooth feature value corresponds to the service's uuid
|
characteristicId
|
string
|
Is
|
The uuid of the Bluetooth feature value
|
value
|
ArrayBuffer
|
Is
|
The binary value for the Bluetooth device feature
value (Note: vConsole cannot print arrayBuffer type data).
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
errMsg
|
string
|
Success: ok, error: details
|
Example code:
wx.onBLECharacteristicValueChange(function(characteristic) {
console.log('characteristic value changed:', characteristic)
})
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)
wx.writeBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
value: buffer,
success: function (res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
}
})
wx.notifyBLECharacteristicValueChanged(OBJECT)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Enable the notify feature when the feature values of Bluetooth low-power devices change.
Note: The feature value of the device must
notify
in order to be successfully called, referring specifically to the properties of the characteristic
In addition, notify must be
notify
before the device featureValueChange event can be listened to
OBJECT parameter description:
Parameters
|
Type
|
Required
|
Description
|
deviceId
|
string
|
Is
|
Bluetooth device id, reference device object
|
serviceId
|
string
|
Is
|
The Bluetooth feature value corresponds to the service's uuid
|
characteristicId
|
string
|
Is
|
The uuid of the Bluetooth feature value
|
state
|
boolean
|
Is
|
true: Enable notify;
false: Deactivs notify
|
success
|
Function
|
Is
|
Successful returns the status of the camera Bluetooth adapter
|
fail
|
Function
|
Whether
|
The interface calls the failed callback function
|
complete
|
Function
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|
success returns parameters:
Parameters
|
Type
|
Description
|
errMsg
|
string
|
Success: ok, error: details
|
Example code:
wx.notifyBLECharacteristicValueChanged({
state: true,
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
success: function (res) {
console.log('notifyBLECharacteristicValueChanged success', res.errMsg)
}
})
wx.onBLEConnectionStateChanged(CALLBACK)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Listen for error events for Bluetooth low-power connections, including lost devices, abnormally disconnected connections, and more.
CALLBACK parameter description:
Parameters
|
Type
|
Description
|
deviceId
|
string
|
Bluetooth device id, reference device object
|
connected
|
boolean
|
The current state of the connection
|
Example code:
wx.onBLEConnectionStateChanged(function(res) {
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})
wx.onBLECharacteristicValueChange(CALLBACK)
Base library version 1.1.0 starts to support, and low versions need to be compatible
Listen for feature value changes for Bluetooth low-power devices.
The notify interface
notify
enabled before you can receive notification pushed by the device.
CALLBACK parameter description:
Parameters
|
Type
|
Description
|
deviceId
|
string
|
Bluetooth device id, reference device object
|
serviceId
|
string
|
The service uuid to which the feature value belongs
|
characteristicId
|
string
|
The feature value uuid
|
value
|
ArrayBuffer
|
The latest value of
the feature value (Note: vConsole cannot print arrayBuffer type data).
|
Example code:
wx.onBLECharacteristicValueChange(function(res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
})
List of Bluetooth error codes (errCode).
Error code
|
Description
|
Note
|
0
|
Ok
|
Normal
|
10000
|
not init
|
The Bluetooth adapter was not initialized
|
10001
|
not available
|
The current Bluetooth adapter is not available
|
10002
|
no device
|
The specified device was not found
|
10003
|
connection fail
|
The connection failed
|
10004
|
no service
|
The specified service was not found
|
10005
|
no characteristic
|
The specified feature value was not found
|
10006
|
no connection
|
The current connection is disconnected
|
10007
|
property not support
|
This operation is not supported by the current feature value
|
10008
|
system error
|
Exceptions reported by all remaining systems
|
10009
|
system not support
|
Android system is special, the system version below 4.3 does not support BLE
|
10010
|
no descriptor
|
The specified descriptor was not found
|
wx.makeBluetoothPair(Object object)
The base library 2.12.0 starts to support, and the lower version needs to be
compatible.
Bluetooth pairing interface for Android use only.
Some devices need to be paired first when connecting to Bluetooth on Android.
Parameters
Object object
Property
|
Type
|
The default
|
Required
|
Description
|
deviceId
|
string
|
|
Is
|
Bluetooth device id
|
pin
|
ArrayBuffer
|
|
Whether
|
Pin code
|
timeout
|
number
|
20
|
Whether
|
Time-out time
|
success
|
function
|
|
Whether
|
The interface calls a successful callback function
|
fail
|
function
|
|
Whether
|
The interface calls the failed callback function
|
complete
|
function
|
|
Whether
|
Callback function at end of interface call (call succeeds, fails are executed)
|