import { features } from "process"; import { BtListType, BtPairedDeviceList, DeviceFeature } from "../main"; import { initSdk, YLSdkImpl } from "../main/app" import { DeviceImpl } from "../main/device"; async function sleep(ms: number) { return new Promise((resolve) => { setTimeout(() => { resolve(''); }, ms) }); } function compareBtPairList(arrLeft: Array<{ deviceName: string, deviceBTAddr: string, isConnected: boolean }>, arrRight: Array<{ deviceName: string, deviceBTAddr: string, isConnected: boolean }>) { if (arrLeft.length !== arrRight.length) { return false; } const length = arrLeft.length; for (let i = 0; i < length; i++) { if (arrLeft[i].deviceName != arrRight[i].deviceName || arrLeft[i].deviceBTAddr != arrRight[i].deviceBTAddr || arrLeft[i].isConnected != arrRight[i].isConnected) { return false; } } return true; } (async () => { try { let ylSdk = await initSdk("test_App_Id"); ylSdk.getSDKVersionAsync().then((result) => { console.log("current sdk version is:" + result); }).catch((error) => { console.log("get current error:" + error); }) ylSdk.RegEvent('attach', async (deviceImpl: DeviceImpl) => { console.log("device attached, name:" + deviceImpl.deviceName); if (!deviceImpl.isDongle) { console.log("device is not dongle, ignore"); return; } var bSupportBluetooth = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaBluetooth); console.log("is support bluetooth:" + bSupportBluetooth); if (bSupportBluetooth) { try { // test scan new device console.error("scan new device"); await deviceImpl.scanNewDevicesAsync(); // test stop sacn device await sleep(10 * 1000); console.error("stop scan new device"); await deviceImpl.stopBTScanAsync(); } catch (error) { console.error("scan new device exception " + error); } } var inquireSearchResult: boolean = true; var connectNewOpt: boolean = false; deviceImpl.RegEvent('onBTParingListChange', async (pairingListInfo) => { console.log('on bt pair list changed, list type:' + pairingListInfo.listType + ', info' + JSON.stringify(pairingListInfo.deviceList, null, 3)); if (pairingListInfo.listType == BtListType.SearchResult) { if (connectNewOpt) { return; } let listGetSearch = await deviceImpl.getSearchDeviceListAsync(); if (!compareBtPairList(listGetSearch, pairingListInfo.deviceList)) { console.log('get search device list:' + JSON.stringify(listGetSearch, null, 3)); throw new Error("listGetSearch != pairingListInfo.deviceList"); } const deviceList = pairingListInfo.deviceList.filter(device => device.deviceName == "Yealink CP900"); if (deviceList.length > 0) { try { // test connect console.log("connect bt new device " + JSON.stringify(deviceList[0], null, 3)); connectNewOpt = true; await deviceImpl.connectNewDeviceAsync(deviceList[0].deviceName, deviceList[0].deviceBTAddr, deviceList[0].isConnected); } catch (error) { console.error("connect new device exception =>" + error); } } else { console.log("CP900 was not found from pairing list, enter pairing mode first"); } } else if (pairingListInfo.listType == BtListType.PairedDevices) { if (pairingListInfo.deviceList.length != 1) { return; } let connectedName: string = ""; try { // test get connected device name connectedName = await deviceImpl.getConnectedBTDeviceNameAsync(); } catch (error) { console.error("get connected bt device name exception =>" + error); } if (connectedName != pairingListInfo.deviceList[0].deviceName) { return; } console.log("verify connected name success,name:" + connectedName); // test get paired list const supportPairedList = await deviceImpl.isPairingListSupportedAsync(); var pairedList: Array<{ deviceName: string, deviceBTAddr: string, isConnected: boolean }> = []; console.log("paired list support:" + supportPairedList); if (supportPairedList) { // test get paired list pairedList = await deviceImpl.getPairingListAsync(); console.log("get paired list:" + JSON.stringify(pairedList, null, 3)); if (pairedList.length <= 0) { throw new Error("getPairingListAsync empty"); } } // test disconnect try { console.log("ready to disconnect device after 2s"); await sleep(2000); await deviceImpl.disconnectBTDeviceAsync(); await sleep(2000); } catch (error) { console.error("disconnect bt device exception =>" + error); } const deviceCP900 = pairedList.filter(device => device.deviceName == "Yealink CP900"); if (deviceCP900.length >= 1) { try { // test reconnect device console.log("connect paired device:" + JSON.stringify(deviceCP900[0], null, 3)); await deviceImpl.connectNewDeviceAsync(deviceCP900[0].deviceName, deviceCP900[0].deviceBTAddr, deviceCP900[0].isConnected); console.log("ready to disconnect device after 2s"); await sleep(2000); await deviceImpl.disconnectBTDeviceAsync(); // test clear paired list await deviceImpl.clearPairingListAsync(); await sleep(1000); const pairedList = await deviceImpl.getPairingListAsync(); console.log("get paired device list:" + JSON.stringify(pairedList, null, 3)); if (pairedList.length > 0) { throw new Error("clear paired list error"); } else { console.log("bt test ok"); ylSdk.disposeAsync(); } } catch (error) { console.error("reconnect bt device exception =>" + error); } } } }) }); } catch (error) { console.error("Got exception err " + error); } })();