Commit 801565ad authored by martin hou's avatar martin hou

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

parent af03c3aa
...@@ -273,6 +273,18 @@ declare class Jensen { ...@@ -273,6 +273,18 @@ declare class Jensen {
// 获取蓝牙连接状态 // 获取蓝牙连接状态
getBluetoothStatus: (seconds?: number) => Promise<BluetoothStatus>; 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>; getRealtimeSettings: () => Promise<any>;
......
...@@ -96,10 +96,42 @@ export function Home() { ...@@ -96,10 +96,42 @@ export function Home() {
if (jensen == null) return; if (jensen == null) return;
setDevices([]); setDevices([]);
setGreeting('scan started at: ' + new Date().toLocaleString()); setGreeting('scan started at: ' + new Date().toLocaleString());
let devices = await jensen.scanDevices(); let count = prompt('Please Input the Scan Count:', '1');
if (count === undefined || count === null) return;
let countNum = parseInt(count);
if (isNaN(countNum) || countNum <= 0) return alert('Please Input the Correct Scan Count');
let rst = await jensen.startBluetoothScan(countNum, 5);
if (rst == null || rst.result != 'success') return alert('start bluetooth scan failed');
// console.log(devices); // console.log(devices);
// setDevices(devices);
setGreeting('scanning...');
}
const stopBluetoothScan = async () => {
let jensen = getJensen();
if (jensen == null) return;
let rst = await jensen.stopBluetoothScan(5);
if (rst == null || rst.result != 'success') return alert('stop bluetooth scan failed.');
setGreeting('scan stopped');
}
const getScanResults = async () => {
let jensen = getJensen();
if (jensen == null) return;
let devices = await jensen.getScanResults(5);
if (devices == null) return alert('get scan results failed.');
setDevices(devices); setDevices(devices);
setGreeting(null) }
const writeBluetoothDeviceList = async () => {
let jensen = getJensen();
if (jensen == null) return;
let macs = prompt('Please Input the MACs (Separated by ","):', '00:00:00:00:00:00');
if (macs === undefined || macs === null) return;
let macsList = macs.split(',').map((item) => item.trim());
let rst = await jensen.writeBluetoothDeviceList(macsList, 5);
if (rst == null || rst.result != 'success') return alert('write bluetooth device list failed.');
setGreeting('write bluetooth device list success');
} }
const connect = async () => { const connect = async () => {
...@@ -416,8 +448,11 @@ export function Home() { ...@@ -416,8 +448,11 @@ export function Home() {
<button onClick={transferFile}>Transfer</button> <button onClick={transferFile}>Transfer</button>
<button onClick={batteryStatus}>Battery</button> <button onClick={batteryStatus}>Battery</button>
<button onClick={getBluetoothStatus}>Bluetooth Status</button> <button onClick={getBluetoothStatus}>Bluetooth Status</button>
<button onClick={bluetoothScan}>Bluetooth Scan</button> <button onClick={bluetoothScan}>Start Scan</button>
<button onClick={disconnectBTDevice}>Bluetooth Disconnect</button> <button onClick={stopBluetoothScan}>Stop Scan</button>
<button onClick={getScanResults}>Scan Results</button>
<button onClick={writeBluetoothDeviceList}>Write MACs</button>
<button onClick={disconnectBTDevice}>Disconnect</button>
<button onClick={updateDeviceTone}>Update Tone</button> <button onClick={updateDeviceTone}>Update Tone</button>
<button onClick={updateUAC}>Update UAC</button> <button onClick={updateUAC}>Update UAC</button>
<button onClick={setWebUSBTimeout}>Set Timeout</button> <button onClick={setWebUSBTimeout}>Set Timeout</button>
......
...@@ -36,6 +36,11 @@ const BLUETOOTH_SCAN = 0x1001; ...@@ -36,6 +36,11 @@ const BLUETOOTH_SCAN = 0x1001;
const BLUETOOTH_CMD = 0x1002; const BLUETOOTH_CMD = 0x1002;
const BLUETOOTH_STATUS = 0x1003; 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 TEST_SN_WRITE = 0xf007;
const RECORD_TEST_START = 0xf008; const RECORD_TEST_START = 0xf008;
const RECORD_TEST_END = 0xf009; const RECORD_TEST_END = 0xf009;
...@@ -766,6 +771,38 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) { ...@@ -766,6 +771,38 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) {
return this.send(new Command(BLUETOOTH_STATUS), 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.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.prototype.setWebUSBTimeout = async function (timeout, seconds) Jensen.prototype.setWebUSBTimeout = async function (timeout, seconds)
{ {
let data = []; let data = [];
...@@ -1568,6 +1605,33 @@ Jensen.registerHandler(BLUETOOTH_STATUS, (msg) => { ...@@ -1568,6 +1605,33 @@ Jensen.registerHandler(BLUETOOTH_STATUS, (msg) => {
}; };
}); });
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;
});
Jensen.registerHandler(RECORD_TEST_START, commonMessageParser); Jensen.registerHandler(RECORD_TEST_START, commonMessageParser);
Jensen.registerHandler(RECORD_TEST_END, commonMessageParser); Jensen.registerHandler(RECORD_TEST_END, commonMessageParser);
Jensen.registerHandler(DEVICE_MSG_TEST, commonMessageParser); Jensen.registerHandler(DEVICE_MSG_TEST, commonMessageParser);
......
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