Commit 79a69479 authored by martin hou's avatar martin hou

feat: 增加蓝牙异步扫描接口的实现

parent 56c1b57c
......@@ -271,6 +271,18 @@ declare class Jensen {
// 获取蓝牙连接状态
getBluetoothStatus: (seconds?: number) => Promise<BluetoothStatus>;
// 发起蓝牙扫描请求
startBluetoothScan: (count: number, seconds?: number) => Promise<ReturnStruct['common']>;
// 停止蓝牙扫描请求
stopBluetoothScan: (seconds?: number) => Promise<ReturnStruct['common']>;
// 获取蓝牙设备列表
getScanResults: (seconds?: number) => Promise<BluetoothDevice[]>;
// 写入蓝牙设备列表
writeBluetoothDeviceList: (mac_list: string[], seconds?: number) => Promise<ReturnStruct['common']>;
// 获取实时音频流的设置信息,主要是确定设备端的实时音频的编码信息
getRealtimeSettings: () => Promise<any>;
......
......@@ -36,6 +36,10 @@ const BLUETOOTH_SCAN = 0x1001;
const BLUETOOTH_CMD = 0x1002;
const BLUETOOTH_STATUS = 0x1003;
const BT_SCAN = 0x1005;
const BT_DEV_LIST = 0x1006;
const BT_WRITE_DEV_LIST = 0x1007;
const TEST_SN_WRITE = 0xf007;
const RECORD_TEST_START = 0xf008;
const RECORD_TEST_END = 0xf009;
......@@ -705,6 +709,25 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) {
return this.send(new Command(BLUETOOTH_STATUS), seconds);
};
// 发起蓝牙扫描请求
Jensen.prototype.startBluetoothScan = async function (count, seconds) {
if (this.model.indexOf('hidock-p1') == -1) return null;
count = count & 0xff;
return this.send(new Command(BT_SCAN).body([0x01, count]), seconds);
};
// 停止蓝牙扫描
Jensen.prototype.stopBluetoothScan = async function (seconds) {
if (this.model.indexOf('hidock-p1') == -1) return null;
return this.send(new Command(BT_SCAN).body([0x00, 0x00]), seconds);
};
// 获取蓝牙设备列表
Jensen.prototype.getScanResults = async function (seconds) {
if (this.model.indexOf('hidock-p1') == -1) return null;
return this.send(new Command(BT_DEV_LIST), seconds);
};
Jensen.prototype.setWebUSBTimeout = async function (timeout, seconds)
{
let data = [];
......@@ -1541,5 +1564,45 @@ Jensen.registerHandler(READ_WEBUSB_TIMEOUT, (msg) => {
return { timeout: timeout };
});
// 写入蓝牙设备列表
Jensen.prototype.writeBluetoothDeviceList = async function (mac_list, seconds) {
if (this.model.indexOf('hidock-p1') == -1) return null;
let data = [];
for (let i = 0; i < mac_list.length; i++) {
let mac = mac_list[i];
let marr = mac.split(':');
if (marr.length != 6) throw new Error('invalid mac');
for (let j = 0; j < marr.length; j++) data.push(parseInt(marr[j], 16));
}
return this.send(new Command(BT_WRITE_DEV_LIST).body(data), seconds);
};
Jensen.registerHandler(BT_SCAN, commonMessageParser);
Jensen.registerHandler(BT_WRITE_DEV_LIST, commonMessageParser);
Jensen.registerHandler(BT_DEV_LIST, (msg) => {
// console.log('bluetooth-scan', msg);
if (msg.body.length == 0) return [];
let nums = ((msg.body[0] & 0xff) << 8) | (msg.body[1] & 0xff);
let devices = [];
let decoder = new TextDecoder('UTF-8');
for (let i = 0, k = 2; i < nums; i++) {
let len = ((msg.body[k++] & 0xff) << 8) | (msg.body[k++] & 0xff);
let sname = new Uint8Array(len);
for (let f = 0; f < len; f++) {
sname[f] = msg.body[k++] & 0xff;
}
let mac = [];
for (let f = 0; f < 6; f++) {
let m = (msg.body[k++] & 0xff).toString(16).toUpperCase();
mac.push(m.length == 1 ? '0' + m : m);
}
devices.push({
name: decoder.decode(sname),
mac: mac.join('-')
});
}
return devices;
});
export { Jensen };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment