node.js - 无法将带有 BLE 适配器的 Windows 笔记本电脑连接到 BLE 设备

标签 node.js bluetooth-lowenergy ibeacon anki

我正在尝试在我的 Windows 笔记本电脑上设置一个基于 Nodejs 的演示,并使用额外的 BLE 适配器将我的笔记本电脑连接到另一个 BLE 设备 (Anki Overdrive)。我在互联网上看过这个演示,很好奇我是否也可以设置它。我已遵循所有说明,但失败了,因为原始演示是使用 MacBook 制作的,它使用不同类型的内置 BLE 适配器。我也用 MacBook 尝试过,效果非常好,但我想用 Windows 设备进行设置。我附加了一部分 Nodejs 代码,该代码应返回 BLE 设备的外设 ID:

//UUID for Anki Overdrive Car be15beef6186407e83810bd89c4d8df4

var noble = require('noble');

noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    noble.startScanning();

    setTimeout(function() {
       noble.stopScanning();
       process.exit(0);
     }, 2000);
  } else {
    noble.stopScanning();
  }
});

noble.on('discover', function(peripheral) {
  var serviceUuids = JSON.stringify(peripheral.advertisement.serviceUuids);
  if(serviceUuids.indexOf("be15beef6186407e83810bd89c4d8df4") > -1) {
    console.log('Car discovered. ID: ' + peripheral.id); 
  }
});

到目前为止,我发现 MacBook 中的内置 BLE 会发回外设 ID。相比之下,Windows BLE 适配器仅返回来自其他 BLE 设备的 MAC 地址。谁能帮我?我真的不知道我能做些什么来解决这个问题。

为了进一步理解,我提供了使用 MacBook 设置的原始演示的链接。 https://github.com/IBM-Bluemix/node-mqtt-for-anki-overdrive .

提前非常感谢。

最佳答案

使用 Anki Overdrive 汽车总是很麻烦。

以下代码片段显示了与您的问题相关的代码。

免责声明:我创建了一个 WIP Anki Overdrive SDK [1],试图让生活变得更轻松(代码片段是从那里复制的)

  1. 使用 Nobel (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)
    }
}
}
  • 触发扫描并创 build 备对象(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.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

    关于node.js - 无法将带有 BLE 适配器的 Windows 笔记本电脑连接到 BLE 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42486526/

    相关文章:

    javascript - Node.js 应用程序中的领域驱动设计

    ios - iOS 中的广告 tx 功率级别

    Python 使用 gattlib 在 Windows 10 上进行 BLE 扫描

    bluetooth-lowenergy - 减少BLE startScan在Android 5.0 Lollipop上检测到的设备

    ios - 无法使用 iOS Estimote Indoor SDK 手动创建位置

    android - 为什么 altbeacon 库只检测少量信标

    promise 中的 Node.js readline

    node.js - OAuth 2.0 Playground 错误 403 : access_denied

    ios - ScanForPeripheral 状态保留与恢复 iOS

    windows - 通过安装程序 (MSI) Windows 7 更新 Node.js 时看不到最新版本