Commit dd288242 authored by martin hou's avatar martin hou

feat: added bluetooth connection information obtaining

parent ad38a3a4
...@@ -18,6 +18,16 @@ export type FileInfo = { ...@@ -18,6 +18,16 @@ export type FileInfo = {
signature: string; signature: string;
}; };
export type BluetoothStatus = {
status: string;
mac?: string;
name?: string;
a2dp?:boolean;
hfp?:boolean;
avrcp?:boolean;
battery?:number;
}
export type BluetoothDevice = { export type BluetoothDevice = {
name: string; name: string;
mac: string; mac: string;
...@@ -122,6 +132,7 @@ declare class Jensen { ...@@ -122,6 +132,7 @@ declare class Jensen {
scanDevices: (seconds?: number) => Promise<BluetoothDevice[]>; scanDevices: (seconds?: number) => Promise<BluetoothDevice[]>;
connectBTDevice: (mac: string, seconds?: number) => Promise<ReturnStruct['common']>; connectBTDevice: (mac: string, seconds?: number) => Promise<ReturnStruct['common']>;
disconnectBTDevice: (seconds?: number) => Promise<ReturnStruct['common']>; disconnectBTDevice: (seconds?: number) => Promise<ReturnStruct['common']>;
getBluetoothStatus: (seconds?: number) => Promise<BluetoothStatus>;
} }
export = Jensen; export = Jensen;
...@@ -83,6 +83,11 @@ export function Home() { ...@@ -83,6 +83,11 @@ export function Home() {
console.log(files); console.log(files);
} }
const getBluetoothStatus = async () => {
let info = await jensen.getBluetoothStatus();
alert(JSON.stringify(info));
}
return ( return (
<> <>
<div style={{ display: 'flex', flexDirection: 'row', gap: '16px', padding: '16px', alignItems: 'center' }}> <div style={{ display: 'flex', flexDirection: 'row', gap: '16px', padding: '16px', alignItems: 'center' }}>
...@@ -96,6 +101,7 @@ export function Home() { ...@@ -96,6 +101,7 @@ export function Home() {
Get Time Get Time
</button> </button>
<button style={{ width: '200px', height: '50px' }} onClick={listFiles}>List Files</button> <button style={{ width: '200px', height: '50px' }} onClick={listFiles}>List Files</button>
<button style={{ width: '200px', height: '50px' }} onClick={getBluetoothStatus}>Bluetooth Status</button>
<button style={{ width : '200px', height : '50px' }} onClick={bluetoothScan}>Bluetooth Scan</button> <button style={{ width : '200px', height : '50px' }} onClick={bluetoothScan}>Bluetooth Scan</button>
<button style={{ width : '200px', height : '50px' }} onClick={disconnectBTDevice}>Bluetooth Disconnect</button> <button style={{ width : '200px', height : '50px' }} onClick={disconnectBTDevice}>Bluetooth Disconnect</button>
</div> </div>
......
...@@ -24,6 +24,7 @@ const FACTORY_RESET = 0xf00b; ...@@ -24,6 +24,7 @@ const FACTORY_RESET = 0xf00b;
const BLUETOOTH_SCAN = 0x1001; const BLUETOOTH_SCAN = 0x1001;
const BLUETOOTH_CMD = 0x1002; const BLUETOOTH_CMD = 0x1002;
const BLUETOOTH_STATUS = 0x1003;
const TEST_SN_WRITE = 0xf007; const TEST_SN_WRITE = 0xf007;
const RECORD_TEST_START = 0xf008; const RECORD_TEST_START = 0xf008;
...@@ -55,7 +56,8 @@ const COMMAND_NAMES = { ...@@ -55,7 +56,8 @@ const COMMAND_NAMES = {
[RECORD_TEST_START]: 'record test start', [RECORD_TEST_START]: 'record test start',
[RECORD_TEST_END]: 'record test end', [RECORD_TEST_END]: 'record test end',
[BLUETOOTH_SCAN]: 'bluetooth-scan', [BLUETOOTH_SCAN]: 'bluetooth-scan',
[BLUETOOTH_CMD]: 'bluetooth-cmd' [BLUETOOTH_CMD]: 'bluetooth-cmd',
[BLUETOOTH_STATUS]: 'bluetooth-status'
}; };
let Logger = null; let Logger = null;
...@@ -584,6 +586,11 @@ Jensen.prototype.disconnectBTDevice = async function(seconds) { ...@@ -584,6 +586,11 @@ Jensen.prototype.disconnectBTDevice = async function(seconds) {
return this.send(new Command(BLUETOOTH_CMD).body([0x01]), seconds); return this.send(new Command(BLUETOOTH_CMD).body([0x01]), seconds);
} }
Jensen.prototype.getBluetoothStatus = async function(seconds) {
if (this.model != 'hidock-p1') return null;
return this.send(new Command(BLUETOOTH_STATUS), seconds);
}
Jensen.prototype.listFiles = async function () { Jensen.prototype.listFiles = async function () {
let tag = 'filelist'; let tag = 'filelist';
if (this[tag] != null) return null; if (this[tag] != null) return null;
...@@ -1145,7 +1152,7 @@ Jensen.registerHandler(GET_RECORDING_FILE, (msg) => { ...@@ -1145,7 +1152,7 @@ Jensen.registerHandler(GET_RECORDING_FILE, (msg) => {
}); });
Jensen.registerHandler(BLUETOOTH_SCAN, (msg) => { Jensen.registerHandler(BLUETOOTH_SCAN, (msg) => {
console.log('bluetooth-scan', msg); // console.log('bluetooth-scan', msg);
let nums = ((msg.body[0] & 0xff) << 8) | (msg.body[1] & 0xff); let nums = ((msg.body[0] & 0xff) << 8) | (msg.body[1] & 0xff);
let devices = []; let devices = [];
let decoder = new TextDecoder('UTF-8'); let decoder = new TextDecoder('UTF-8');
...@@ -1171,6 +1178,35 @@ Jensen.registerHandler(BLUETOOTH_SCAN, (msg) => { ...@@ -1171,6 +1178,35 @@ Jensen.registerHandler(BLUETOOTH_SCAN, (msg) => {
return devices; return devices;
}); });
Jensen.registerHandler(BLUETOOTH_STATUS, (msg) => {
if (msg.body.length == 0) return { status : 'disconnected' };
let status = msg.body[0];
if (status == 1) return { status : 'disconnected' };
let flen = ((msg.body[1] & 0xff) << 8) | (msg.body[2] & 0xff);
let decoder = new TextDecoder('UTF-8');
let sname = new Uint8Array(flen);
let i = 3;
for (let k = 0; i < msg.body.length && k < flen; i++, k++)
{
sname[k] = msg.body[i] & 0xff;
}
let mac = [];
for (let k = 0; i < msg.body.length && k < 6; k++)
{
let m = (msg.body[i++]).toString(16).toUpperCase();
mac.push(m.length == 1 ? '0' + m : m);
}
return {
status : 'connected',
mac : mac.join('-'),
name : decoder.decode(sname),
a2dp : (msg.body[i++] & 0xff) == 1,
hfp : (msg.body[i++] & 0xff) == 1,
avrcp : (msg.body[i++] & 0xff) == 1,
battery : parseInt((msg.body[i++] & 0xff) / 255 * 100)
};
});
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