使用 Anki Overdrive 汽车总是很麻烦。
以下片段显示了有关您问题的相关代码。
免责声明:我创建了一个 WIP Anki Overdrive SDK [1],试图让生活更轻松(片段从那里复制)
- 使用 noble (scanner.js) 扫描设备
module.exports = class Scanner {
constructor() {
this.peripherals = []
this.state = undefined
}
/**
* Set up noble listeners. Mandatory before scan.
*/
async setUpNoble () {
try {
await noble.on('discover', (peripheral) => {
const isAnkiDevice = function(peripheral) {
const serviceUuids = JSON.stringify(peripheral.advertisement.serviceUuids)
return (serviceUuids.indexOf("be15beef6186407e83810bd89c4d8df4") > -1)
}
if(isAnkiDevice(peripheral)) {
this.peripherals.push(peripheral)
}
})
await noble.on('stateChange', (status) => {
this.state = status
})
} catch (err) {
throw new Error(err)
}
}
/**
* Scans for devices and returns peripherals. Scans for 20 seconds before stopping.
*/
async scan() {
try {
let that = this
await waitUntil(() => {
return (that.state === 'poweredOn')
}, 20000)
console.log('Starting to scan...')
return await waitUntil(() => {
noble.stopScanning()
if(that.peripherals.length <= 0) {
console.log('Scanning...')
noble.startScanning()
} else {
console.log('Found ' + that.peripherals.length + ' devices.')
return that.peripherals
}
}, 20000, 4000)
} catch (err) {
throw new Error(err)
}
}
}
- 触发扫描并创建设备对象(anki.js)
async scanDevices() {
try {
const that = this
const scanner = new Scanner()
scanner.setUpNoble()
const peripherals = await scanner.scan()
const devices = peripherals.map((peripheral) => {
return new Device(peripheral)
})
return devices
} catch (err) {
throw new Error(err)
}
}
- 在 Device 对象 (device.js) 的构造函数中从外设设置 id 和 serviceUUids
module.exports = class AnkiDevice {
constructor (peripheral) {
this.peripheral = peripheral
this.id = peripheral.uuid
this.serviceUuids = JSON.stringify(peripheral.advertisement.serviceUuids)
....
- 连接到汽车 (device.js)
connect() {
try {
const that = this
console.log('Connecting with ' + this.id)
const getService = function(services){
if (os.platform() === 'win32' || os.platform() === 'linux') {
return services[2]
} else {
return service[0] // macOS
}
}
const setCharacteristics = function(characteristics) {
for(let i in characteristics) {
const characteristic = characteristics[i]
if (characteristic.uuid == 'be15bee06186407e83810bd89c4d8df4') {
that.readCharacteristic = characteristic
}
if (characteristic.uuid == 'be15bee16186407e83810bd89c4d8df4') {
that.writeCharacteristic = characteristic
}
}
}
const onConnect = async function() {
const services = await that.peripheral.discoverServices([])
const service = await getService(services)
const characteristics = await service.discoverCharacteristics([])
await setCharacteristics(characteristics)
// Listen to own disconnect
that.peripheral.once('disconnect', () => {
mediator.private.emit('deviceDisconnected', this)
})
mediator.private.emit('deviceConnected', that)
}
this.peripheral.once('connect', onConnect)
this.peripheral.connect()
} catch(err) {
throw new Error(err)
}
}
- 那么:激活 SDK 模式 (device.js)
activateSDKMode() {
console.log('Activating SDKMode for ' + this.id)
const that = this
if(!this.isConnected) {
return new Error('Car is not connected yet.')
}
const message = coder.encodeSDKActivation()
this._writeMessage(message)
.then(() => {
mediator.private.emit('SDKModeOn', that)
})
}
- 那么:打开日志记录(device.js)
turnOnLogging() {
console.log('Turning logging on for ' + this.id)
let that = this
if(!that.isConnected) {
return new Error('Car is not connected yet.')
}
that.readCharacteristic.notify(true)
that.readCharacteristic.on('read', (data) => {
that._onMessage(data)
})
mediator.private.emit('loggingOn', that)
}
[1] https://github.com/steinroe/anki-overdrive-sdk