当外围设备本身正在改变特征值时,如何实现更新值回调

时间:2015-03-10 07:35:57

标签: bleno

假设外围设备正在侦听某些套接字或管道,我将在其中发送一个将修改该特性的值。如何通知中央设备的更改?外围设备和中央设备都在Linux平台上运行。因为无论为Linux设备提供的Pizza示例,只有当中心正在编写内容时才会通知中心。假设这个值是由Bleno之外的某个程序改变的,我应该实现updateValueCallback()?是onSubscribe还是onNotify

1 个答案:

答案 0 :(得分:3)

您应该使用onSubscribe()事件。

当中央(GATT服务器)订阅来自您的外围设备的通知时,

onSubscribe()会触发,而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!

大致基于this文章,以及here的一些想法。