1

假设外围设备正在侦听某个套接字或管道,我正在其中发送一个将修改特性的值。如何将更改通知到中央设备?外围设备和中央设备都在 Linux 平台上运行。因为无论为 Linux 设备给出的 Pizza 示例,都只会在中心写入内容时通知中心。假设如果 Bleno 之外的某个程序更改了值,那么我应该在哪里实现updateValueCallback()?是开onSubscribe还是onNotify

4

1 回答 1

6

您应该使用该 onSubscribe()事件。

onSubscribe()当中央(GATT 服务器)订阅来自外围设备onNotify()的通知时触发,而在中央订阅后发送通知时触发。

例如,我将我的特征定义如下:

var customCharacteristic = function() {
    bleno.Characteristic.call(this, {
        uuid: 'UUID_HERE',
        properties: ['read', 'notify', '... etc'],
        //Subscribe event
        onSubscribe = function(maxSize, updateValueCallback){
            console.log('subscribe'); //output that we've subscribed
            //Now the central has subscribed, poll something 
            //(the sensor or whatever you're using) every half 
            //second to see if the value has updated
            setInterval(function() {
                //poll sensor or get value or something
                updateValueCallback(new Buffer([VALUE]);
            }, 500);
        },
        //Notify event
        onNotify = function(){
            console.log('notify'); //to show when notification is being sent
        }
    });
}

//Don't forget to deal with when the central unsubscribes, you might want to stop the interval ticker!

大致基于这篇文章,从这里得到一些想法。

于 2015-04-02T16:24:12.403 回答