Bluetooth-Low Power Peripheral Device
onBLEPeripheralConnectionStateChanged
TIP
The API usage is as follows: wx.onBLEPeripheralConnectionStateChanged(function listener)。
- Functional description: Listen for events of current peripheral devices being connected or disconnected
- Parameters and descriptions: function listener,The listener function for the current peripheral device being connected or disconnected, the parameter Object res is as follows:
Properties Type Description deviceId String Device id of the connection state change serverId String UUID of the server connected Boolean Current connection state
offBLEPeripheralConnectionStateChanged
TIP
The API usage is as follows: wx.offBLEPeripheralConnectionStateChanged(function listener)
Functional description: Remove listeners for events of current peripheral devices being connected or disconnected.
Parameters and descriptions: function listener,The listener function passed in by onBLEPeripheralConnectionStateChanged. If this parameter is not passed, all listeners are removed.
Sample code:
const listener = function (res) { console.log(res) }
wx.onBLEPeripheralConnectionStateChanged(listener)
wx.offBLEPeripheralConnectionStateChanged(listener) // Pass the same function object used for listening.createBLEPeripheralServer
TIP
The API usage is as follows: wx.createBLEPeripheralServer(Object object)
Functional description: Establish a local server as a Bluetooth low energy peripheral device. Multiple can be created.
Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res。
Properties Type Description server BLEPeripheralServer The server of the peripheral device
BLEPeripheralServer
TIP
The server of the peripheral device
.addService
TIP
The API usage is as follows: BLEPeripheralServer.addService(Object object)
Functional description: Add services.
Parameters and descriptions: Object object。
Properties Type Default value Required Description service Object - Yes Object describing the service success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Service structure attributes
Structure properties Type Required Description uuid string Yes UUID of the Bluetooth service characteristics Array.< Object>Yes Characteristics list Characteristics structure attributes
Structure properties Type Required Description uuid string Yes UUID of the characteristic properties Object No Operations supported by the characteristic permission Object No Characteristic permissions value Array.< Object>No Binary value corresponding to the characteristic descriptors Array.< Object>No Descriptor data - properties structure attribute
Structure properties Type Default value Required Description write boolean false No write writeNoResponse boolean false No no reply write read boolean false No read notify boolean false No subscribe indicate boolean false No reply - permission structure attribute
Structure properties Type Default value Required Description readable boolean false No readable writeable boolean false No writable readEncryptionRequired boolean false No encrypted read request writeEncryptionRequired boolean false No encrypted write request - descriptorsStructure properties
Structure properties Type Required Description uuid string Yes Descriptors UUID permission Object No descriptor permissions value ArrayBuffer No Descriptor data - permission structure attribute
Structure properties Type Default value Required Description write boolean false No write read boolean false No read
.removeService
TIP
The API usage is as follows: BLEPeripheralServer.removeService(Object object)
- Functional description: Remove services.
- Parameters and descriptions: Object object。
Properties Type Default value Required Description serviceId String - Yes service UUID success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure)
.startAdvertising
TIP
The API usage is as follows: BLEPeripheralServer.startAdvertising(Object Object)
Functional description: Start broadcasting locally created peripheral devices
Parameters and descriptions: Object object。
Properties Type Default value Required Description advertiseRequest object - Yes broadcast custom parameters powerLevel String medium No broadcast power, legal values
1.low:low power
1.medium:moderate power
1.high:high powersuccess Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) advertiseRequest structure attribute
Structure properties Type Default value Required Description connectable boolean true No whether the current device is connectable deviceName String - No deviceName field in broadcast, empty by default serviceUuids Array.< string>- No service UUID list to be broadcast. Please refer to the precautions when using 16/32-bit UUID manufactureData Array.< Object>- No broadcast manufacturer information. Only supported by Android, iOS cannot be customized due to system restrictions beacon object - No parameters broadcast as beacon devices - manufactureDataStructure properties
Structure properties Type Required Description manufacturerId String Yes manufacturer ID, hexadecimal starting with 0x manufacturerSpecificData ArrayBuffer No manufacturer information - beacon structure attributes
Structure properties Type Required Description uuid number Yes UUID broadcast by Beacon device major number Yes Primary ID of Beacon device minor number Yes Secondary ID of Beacon device measurePower number Yes Reference value for judging RSSI size when 1 meter away from the device
.stopAdvertising
TIP
The API usage is as follows: BLEPeripheralServer.stopAdvertising()
- Functional description: Stop broadcasting
- Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure)
.writeCharacteristicValue
TIP
The API usage is as follows: BLEPeripheralServer.writeCharacteristicValue(Object Object)
- Functional description: Write binary data value to the specified characteristic and notify the connected host. The characteristic value of the slave has changed. This interface will handle whether to send a packet back or subscribe.
- Parameters and descriptions: Object object。
Properties Type Default value Required Description serviceId string - Yes The UUID of the service corresponding to the Bluetooth characteristic characteristicId string - Yes The UUID of the Bluetooth characteristic value ArrayBuffer - Yes Binary value corresponding to characteristic needNotify boolean - Yes Whether to notify the host that value has been updated callbackId number - Yes Optional, used when processing the packet back success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure)
.onCharacteristicWriteRequest
TIP
The API usage is as follows: BLEPeripheralServer.onCharacteristicWriteRequest(function listener)
- Functional description: Listen for the connected device to request to write the characteristic value event of the current peripheral device. After receiving the message, you need to call writeCharacteristicValue immediately to write back the data, otherwise the host will not receive a response.
- Parameters and descriptions: function listener,Listening function for the connected device to request to write the characteristic value event of the current peripheral device, the parameter Object res is as follows:
Properties Type Description serverId String The UUID of the service corresponding to the Bluetooth characteristic characteristicId String The UUID of the Bluetooth characteristic callbackId Number Unique identification code, callwriteCharacteristicValueused when value ArrayBuffer Request to write binary data value of characteristic
.offCharacteristicWriteRequest
TIP
The API usage is as follows: BLEPeripheralServer.offCharacteristicWriteRequest(function listener)
Functional description: Remove listeners for events of connected devices requesting to write characteristic values
Parameters and descriptions: function listener,onCharacteristicWriteRequest The passed listening function. If this parameter is not passed, all listening functions will be removed.
Sample code:
const listener = function (res) { console.log(res) }
BLEPeripheralServer.onCharacteristicWriteRequest(listener)
BLEPeripheralServer.offCharacteristicWriteRequest(listener) // Pass the same function object used for listening..onCharacteristicReadRequest
TIP
The API usage is as follows: BLEPeripheralServer.onCharacteristicReadRequest(function listener)
- Functional description: Listen to the connected device request to read the characteristic value event of the current peripheral device. After receiving this message, you need to call writeCharacteristicValue immediately to write back the data, otherwise the host will not receive a response.
- Parameters and descriptions: function listener,The connected device requests to read the characteristic value event of the current peripheral device. The parameter Object res is as follows:
Properties Type Description serverId String The UUID of the service corresponding to the Bluetooth characteristic characteristicId String The UUID of the Bluetooth characteristic callbackId Number Unique identification code, callwriteCharacteristicValueused when
.offCharacteristicReadRequest
TIP
The API usage is as follows: BLEPeripheralServer.offCharacteristicReadRequest(function listener)
Functional description: Remove listeners for events of connected devices requesting to read characteristic values
Parameters and descriptions: function listener,onCharacteristicReadRequest The passed listening function. If this parameter is not passed, all listening functions will be removed.
Sample code:
const listener = function (res) { console.log(res) }
BLEPeripheralServer.onCharacteristicReadRequest(listener)
BLEPeripheralServer.offCharacteristicReadRequest(listener) // Pass the same function object used for listening..onCharacteristicSubscribed
TIP
The API usage is as follows: BLEPeripheralServer.onCharacteristicSubscribed(function listener)
- Functional description: Listen for characteristic subscription events, only supported by iOS
- Parameters and descriptions: function listener,The listening function of the characteristic subscription event, the parameter Object res is as follows:
Properties Type Description serverId String The UUID of the service corresponding to the Bluetooth characteristic characteristicId String The UUID of the Bluetooth characteristic
.offCharacteristicSubscribed
TIP
The API usage is as follows: BLEPeripheralServer.offCharacteristicSubscribed(function listener)
Functional description: Remove listeners for characteristic subscription events
Parameters and descriptions: function listener,onCharacteristicSubscribed The passed listening function. If this parameter is not passed, all listening functions will be removed.
Sample code:
const listener = function (res) { console.log(res) }
BLEPeripheralServer.onCharacteristicSubscribed(listener)
BLEPeripheralServer.offCharacteristicSubscribed(listener) // Pass the same function object used for listening..onCharacteristicUnsubscribed
TIP
The API usage is as follows: BLEPeripheralServer.onCharacteristicUnsubscribed(function listener)
- Functional description: Listen for the feature subscription cancellation event, only supported by iOS
- Parameters and descriptions: function listener,The listening function of the cancel characteristic subscription event, the parameter Object res is as follows:
Properties Type Description serverId String The UUID of the service corresponding to the Bluetooth characteristic characteristicId String The UUID of the Bluetooth characteristic
.offCharacteristicUnsubscribed
TIP
The API usage is as follows: BLEPeripheralServer.offCharacteristicUnsubscribed(function listener)
Functional description: Remove the listener function for the feature subscription cancellation event
Parameters and descriptions: function listener,onCharacteristicUnsubscribed The passed listening function. If this parameter is not passed, all listening functions will be removed.
Sample code:
const listener = function (res) { console.log(res) }
BLEPeripheralServer.onCharacteristicUnsubscribed(listener)
BLEPeripheralServer.offCharacteristicUnsubscribed(listener) // Pass the same function object used for listening.