Skip to main content
Version: v2

ble

Start from API_LEVEL 2.0 . Please refer to API_LEVEL.

The "Device App" communicates with the "Side Service" through the ble module using the device's Bluetooth communication capabilities.

tip

For a complete example of Bluetooth communication, please refer to Bluetooth Communication

Method​

createConnect(callback)​

Create connection

Type​

(callback: (index: number, data: object, size: number) => void) => void

Parameters​

Callback parameterDescriptionRequiredType
indexsubpackage numberNOnumber
datareceived dataNOobject
sizelength of data receivedNOnumber

disConnect()​

Disconnects

Type​

() => void

send(data, size)​

Send a message

Type​

(data: object, size: number) => void

Parameters​

ParametersDescriptionRequiredType
datadata to be sentNOobject
sizelength of data to be sentNOnumber

connectStatus()​

Query connection status

Type​

() => Result

Result​

DescriptionType
connection status true connected, false not connectedboolean

addListener(callback)​

Register a connection status listener

type​

(callback: (status: boolean) => void) => void

Parameters​

Callback parameterDescriptionType
statusconnection statusboolean

removeListener​

Cancel the connection status listener

type​

() => void

Code example​

import { createConnect, send, disConnect, connectStatus, addListener } from '@zos/ble'

// Create Connection
createConnect(function (index, data, size) {
// Receive message callback, return the received message as it is
send(data, size)
})

// Disconnection
disConnect()

// Print Bluetooth connection status
console.log(connectStatus())

// Register to listen for connection status
addListener(function (status) {
// Print connection status
console.log(status)
})