Skip to content

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:
    PropertiesTypeDescription
    deviceIdStringDevice id of the connection state change
    serverIdStringUUID of the server
    connectedBooleanCurrent 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:

js
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。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    serverBLEPeripheralServerThe 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。

    PropertiesTypeDefault valueRequiredDescription
    serviceObject-YesObject describing the service
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Service structure attributes

    Structure propertiesTypeRequiredDescription
    uuidstringYesUUID of the Bluetooth service
    characteristicsArray.<Object>YesCharacteristics list
  • Characteristics structure attributes

    Structure propertiesTypeRequiredDescription
    uuidstringYesUUID of the characteristic
    propertiesObjectNoOperations supported by the characteristic
    permissionObjectNoCharacteristic permissions
    valueArray.<Object>NoBinary value corresponding to the characteristic
    descriptorsArray.<Object>NoDescriptor data
    • properties structure attribute
    Structure propertiesTypeDefault valueRequiredDescription
    writebooleanfalseNowrite
    writeNoResponsebooleanfalseNono reply write
    readbooleanfalseNoread
    notifybooleanfalseNosubscribe
    indicatebooleanfalseNoreply
    • permission structure attribute
    Structure propertiesTypeDefault valueRequiredDescription
    readablebooleanfalseNoreadable
    writeablebooleanfalseNowritable
    readEncryptionRequiredbooleanfalseNoencrypted read request
    writeEncryptionRequiredbooleanfalseNoencrypted write request
    • descriptorsStructure properties
    Structure propertiesTypeRequiredDescription
    uuidstringYesDescriptors UUID
    permissionObjectNodescriptor permissions
    valueArrayBufferNoDescriptor data
    • permission structure attribute
    Structure propertiesTypeDefault valueRequiredDescription
    writebooleanfalseNowrite
    readbooleanfalseNoread

.removeService

TIP

The API usage is as follows: BLEPeripheralServer.removeService(Object object)

  • Functional description: Remove services.
  • Parameters and descriptions: Object object。
    PropertiesTypeDefault valueRequiredDescription
    serviceIdString-Yesservice UUID
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback 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。

    PropertiesTypeDefault valueRequiredDescription
    advertiseRequestobject-Yesbroadcast custom parameters
    powerLevelStringmediumNobroadcast power, legal values
    1.low:low power
    1.medium:moderate power
    1.high:high power
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • advertiseRequest structure attribute

    Structure propertiesTypeDefault valueRequiredDescription
    connectablebooleantrueNowhether the current device is connectable
    deviceNameString-NodeviceName field in broadcast, empty by default
    serviceUuidsArray.<string>-Noservice UUID list to be broadcast. Please refer to the precautions when using 16/32-bit UUID
    manufactureDataArray.<Object>-Nobroadcast manufacturer information. Only supported by Android, iOS cannot be customized due to system restrictions
    beaconobject-Noparameters broadcast as beacon devices
    • manufactureDataStructure properties
    Structure propertiesTypeRequiredDescription
    manufacturerIdStringYesmanufacturer ID, hexadecimal starting with 0x
    manufacturerSpecificDataArrayBufferNomanufacturer information
    • beacon structure attributes
    Structure propertiesTypeRequiredDescription
    uuidnumberYesUUID broadcast by Beacon device
    majornumberYesPrimary ID of Beacon device
    minornumberYesSecondary ID of Beacon device
    measurePowernumberYesReference 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。
    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback 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。
    PropertiesTypeDefault valueRequiredDescription
    serviceIdstring-YesThe UUID of the service corresponding to the Bluetooth characteristic
    characteristicIdstring-YesThe UUID of the Bluetooth characteristic
    valueArrayBuffer-YesBinary value corresponding to characteristic
    needNotifyboolean-YesWhether to notify the host that value has been updated
    callbackIdnumber-YesOptional, used when processing the packet back
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback 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:
    PropertiesTypeDescription
    serverIdStringThe UUID of the service corresponding to the Bluetooth characteristic
    characteristicIdStringThe UUID of the Bluetooth characteristic
    callbackIdNumberUnique identification code, callwriteCharacteristicValueused when
    valueArrayBufferRequest 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:

js
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:
    PropertiesTypeDescription
    serverIdStringThe UUID of the service corresponding to the Bluetooth characteristic
    characteristicIdStringThe UUID of the Bluetooth characteristic
    callbackIdNumberUnique 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:

js
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:
    PropertiesTypeDescription
    serverIdStringThe UUID of the service corresponding to the Bluetooth characteristic
    characteristicIdStringThe 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:

js
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:
    PropertiesTypeDescription
    serverIdStringThe UUID of the service corresponding to the Bluetooth characteristic
    characteristicIdStringThe 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:

js
const listener = function (res) { console.log(res) }


BLEPeripheralServer.onCharacteristicUnsubscribed(listener)
BLEPeripheralServer.offCharacteristicUnsubscribed(listener)   // Pass the same function object used for listening.