import {
OnBluetoothDeviceFoundOptions,
OnBluetoothDeviceFoundCallback
} from '../../interface.uts'
import {
DevicesOptions
} from '../type.uts'
import { ble } from '@kit.ConnectivityKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
export function onBluetoothDeviceFound(options : OnBluetoothDeviceFoundCallback,name ?:string) {
let isSuccess = true;
console.log(JSON.stringify(options),name)
try {
let devicesData : Array<DevicesOptions> = [];
ble.on("BLEDeviceFind", (data: Array<ble.ScanResult>)=>{
// console.info(JSON.stringify(data))
for(let i = 0; i < data.length; i++){
let obj : DevicesOptions = {
deviceId: data[i].deviceId,
RSSI: data[i].rssi,
name: data[i].deviceName
}
devicesData.push(obj)
}
let res : OnBluetoothDeviceFoundOptions = {
devices: devicesData
};
options?.(res);
});
let scanOptions: ble.ScanOptions = {
interval: 500,
dutyMode: ble.ScanDuty.SCAN_MODE_LOW_LATENCY,
matchMode: ble.MatchMode.MATCH_MODE_AGGRESSIVE
}
ble.startBLEScan(name ? [{name: name}] as ble.ScanFilter[] : null,scanOptions);
} catch (err) {
isSuccess = false;
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
}
0 个回复