Bluetooth library (for full only)
Functional overview
Based on the native platform Bluetooth library encapsulation, it is used to scan peripheral devices, obtain the returned device collection, display it in the list, select the device to be connected, and return the device information and whether the device is in OTA mode after successful connection. If the connection fails, there will be an error callback. The default scan signal filter parameter is 80, and the automatic scan connection timeout is 20 seconds. (Note: only for full version)
Method
addBleDelegate(api:)
Add Bluetooth agent IDOBleDelegate
addDfuDelegate(api:)
Add DFU upgrade (nordic)
bluetoothRegister(heartPingSecond:outputToConsole:)
Register, call when the program starts running
delegate agent heartPingSecond: heartbeat packet interval (ios) outputToConsole: console output log
startScan(macAddress:completion:)
Start searching macAddress (Android): Search based on Mac address Returns the specified search device, returns null if not specified
scanFilter(deviceName:deviceID:macAddress:uuid:)
Search filter conditions
deviceName: Only search for devices with deviceName
deviceID: Only search for devices with deviceID
macAddress: Only search for devices with macAddress
uuid: Only search for devices with uuid
stopScan()
Stop searching
connect(device:)
Connect device: Mac address must be passed, iOS needs to bring uuid, it is best to use the object returned by the search
autoConnect(device:)
Use this to reconnect the device
cancelConnect(macAddress:completion:)
Cancel the connection
getBluetoothState(completion:)
Get Bluetooth status
getDeviceState(device:completion:)
Get device connection status
writeData(data:device:type:completion:)
Send data
data: data
device: device sending data
type:0 BLE data, 1 SPP data
platform: 0 ido, 1 Hengxuan, 2 VC
setBtPair(device:)
bt pairing (android)
cancelPair(device:)
Cancel pairing (android)
connectSPP(btMacAddress:)
Connect SPP (android)
disconnectSPP(btMacAddress:)
Disconnect SPP (android)
startNordicDFU(config:)
Initiate dfu upgrade
exportLog(completion:)
Export ble log, return the absolute path of the compressed log zip file
Example
Register Bluetooth module
Swift:
// Register Bluetooth library
sdk.ble.addBleDelegate(api: <IDOBleDelegate>)
sdk.ble.getBluetoothState { [weak self] stateModel in
// Status monitoring
}
Kotlin:
//Register Bluetooth library
sdk.ble.addBleDelegate(IDOBleDelegate)
sdk.ble.getBluetoothState {
//Status monitoring
}
Proxy implementation
Swift:
// Implementation of proxy
extension SomeClass: IDOBleDelegate {
func scanResult(list: [IDODeviceModel]?) {
// Scan device list
print("scanResult list count:\(String(describing: list?.count))")
}
func bluetoothState(state: IDOBluetoothStateModel) {
// Bluetooth status
print("on bluetoothState callback: \(String(describing: state.scanType?.rawValue))")
}
func deviceState(state: IDODeviceStateModel) {
// Device state
print("on deviceState callback: \(String(describing: state.state))")
//If the device you are connecting to supports pairing encryption, you need to handle the following two situations, as shown in the following examples:
if (deviceState.errorState == IDOConnectErrorType.deviceAlreadyBindAndNotSupportRebind) {//1. The device cannot be bound again this time, so you need to reset the device first.
let alert = UIAlertController(title: "Note",
message: "The watch has been bound and does not support repeated binding, First reset the watch on the watch side!",
preferredStyle: UIAlertController.Style.alert)
let ok = UIAlertAction(title: "Got it", style: UIAlertAction.Style.default) { action in
self.dismiss(animated: true)
}
alert.addAction(ok)
present(alert, animated: true, completion: nil)
} else if (deviceState.errorState == IDOConnectErrorType.deviceHasBeenReset) {//2. The device has been reset and the local device information needs to be deleted
let alert = UIAlertController(title: "Note",
message: "The watch has been reset, pls remove the watch!",
preferredStyle: UIAlertController.Style.alert)
let delete = UIAlertAction(title: "Remove it",
style: UIAlertAction.Style.default) { action in
SVProgressHUD.show(withStatus: "unbind...")
guard let macAddress = deviceState.macAddress else {
SVProgressHUD.showSuccess(withStatus: "macAddress is nil")
return
}
//You can delete the device on the app side here, as shown below:
sdk.cmd.unbind(macAddress: macAddress , isForceRemove: true, completion: { [weak self] rs in
//Release resources
if rs {
SVProgressHUD.showSuccess(withStatus: "unbind successful")
} else {
SVProgressHUD.showError(withStatus: "unbind failure")
}
UserDefaults.standard.setBind(macAddress, isBind: false)
UserDefaults.standard.synchronize()
self?.dismiss(animated: true)
})
}
alert.addAction(delete)
present(alert, animated: true, completion: nil)
}
}
}
Kotlin:
// Implement the proxy
private val bleDelegate = object : IDOBleDelegate {
override fun scanResult(list: List<IDOBleDeviceModel>?) {
// Scan device list
}
override fun bluetoothState(state: IDOBluetoothStateModel) {
// Bluetooth state
}
override fun deviceState(state: IDODeviceStateModel) {
// Device state
//If the device you are connecting to supports pairing encryption, you need to handle the following two situations, as shown in the following examples:
if (idoDeviceStateModel.errorState == IDOConnectErrorType.DEVICEALREADYBINDANDNOTSUPPORTREBIND) {//1. The device cannot be bound again this time, so you need to reset the device first.
//device already bind, and not support rebind
AlertDialog.Builder(this@FunctionActivity).setTitle("Note")
.setMessage("The watch has been bound and does not support repeated binding, First reset the watch on the watch side!").setPositiveButton("Got it", null)
.show()
} else if (idoDeviceStateModel.errorState == IDOConnectErrorType.DEVICEHASBEENRESET) {//2. The device has been reset and the local device information needs to be deleted
//The watch has been reset, pls remove the watch!
AlertDialog.Builder(this@FunctionActivity).setTitle("Note")
.setMessage("The watch has been reset, pls remove the watch!").setPositiveButton("Remove it", { dialog, which ->
//You can delete the device on the app side here, as shown below:
sdk.cmd.unbind(device?.macAddress ?: "", true, {
if (it) {
//Release resources
sdk.ble.cancelPair(device);
sdk.ble.cancelConnect(device?.macAddress) {}
sdk.messageIcon.resetIconInfoData(
macAddress = device?.macAddress.toString(),
deleteIcon = true
) {}
} else {
//unbind failed
}
})
}).show()
}
}
}
override fun stateSPP(state: IDOSppStateModel) {
}
override fun writeSPPCompleteState(btMacAddress: String) {
}
}