简体   繁体   English

外设自身改变特征值时如何实现更新值回调

[英]How to implement update value callback when peripheral itself is changing the characteristic value

Suppose if the peripheral is listening to some socket or pipe in which I am sending a value which will modify the characteristic. 假设外设正在侦听某些套接字或管道,我正在其中发送一个值,该值将修改特性。 How can I notify that change to the central device? 如何将更改通知中央设备? Both peripheral and central devices are running on the Linux platform. 外围设备和中央设备都在Linux平台上运行。 Because whatever the Pizza example given for Linux device will notify the central only when central is writing something. 因为任何给Linux设备的Pizza示例都将在Central正在编写内容时通知Central。 Suppose if the value is changed by some program outside Bleno an noble where I should implement the updateValueCallback() ? 假设如果值由Bleno以外的某个程序更改,那我应该在哪里实现updateValueCallback() Is it on onSubscribe or onNotify ? onSubscribe还是onNotify

You should use the onSubscribe() event. 您应该使用onSubscribe()事件。

onSubscribe() fires when the central (GATT Server) subscribes to notifications from your peripheral, whereas onNotify() fires when a notification is sent, after the central has subscribed. onSubscribe()时触发中心(GATT服务器)订阅从外围的通知,而onNotify()时,则发送通知火灾,中央已经订阅了。

For example, I've defined my characteristic as follows: 例如,我将特征定义如下:

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!

Roughly based on this article, with some ideas from here . 大致基于文章,从一些想法在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM