Adam-Langley / pybleno

A direct port of the Bleno bluetooth LE peripheral role library to Python2/3
MIT License
68 stars 34 forks source link

Notification from different characteristics messed up #57

Closed ShinChoon closed 2 years ago

ShinChoon commented 2 years ago

Hi I found that if I used lightBlue to subscribe to multiple characteristics, the notifications from different characteristics messed up, for example the VALUE1 can appear in characteristic 2's updateValueCallback Below is the example code for one characteristic `import array import logging import _thread import time from pybleno import * LOGGER = logging.getLogger(name)

class VALUE1_Characteristic(Characteristic): def init(self, chara_uuid, application): Characteristic.init(self, { 'uuid': chara_uuid, 'properties': ['read', 'write', 'notify'], 'descriptors': [ Descriptor( { 'uuid' : '2901', 'value' : 'VALUE1.' } )], 'value': None }) self._updateValuecallbackVALUE1 = None self.application = application self.preresultVALUE1 = 0 self.switch_loop = True self.result = array.array('B', [0] * 1)

def onWriteRequest(self, data, offset, withoutResponse, callback):
    if offset:
        callback(Characteristic.RESULT_ATTR_NOT_LONG)
    else:
        answer = ord(bytes(data))
        if answer:
            string = 'True'
        else:
            string = 'False'

        def on_applicationready(result):
            if self._updateValuecallbackVALUE1:
                self.result = bytes(chr(result).encode('utf-8'))
                self._updateValuecallbackVALUE1(self.result)

        self.application.once('ready', [], on_applicationready)
        self.application.controlmessage(VALUE1=string)
        callback(Characteristic.RESULT_SUCCESS)

def onReadRequest(self, offset, callback):
    if offset:
        callback(Characteristic.RESULT_ATTR_NOT_LONG, self.result)
    else:
        result = self.application.pull_out_message('VALUE1')
        self.result = bytes(chr(result).encode('utf-8'))
        self.preresultVALUE1 = result
        callback(Characteristic.RESULT_SUCCESS, self.result)

def onSubscribe(self, maxValueSize, updateValueCallback):
    LOGGER.info('EchoCharacteristic - onSubscribe - VALUE1')
    self.switch_loop = True
    self.checkInterval()
    self._updateValuecallbackVALUE1 = updateValueCallback

def onUnsubscribe(self):
    LOGGER.info('EchoCharacteristic - onUnsubscribe')
    self._updateValuecallbackVALUE1 = None
    self.switch_loop = False

def checkInterval(self):
    _thread.start_new_thread(self.loop_status_check, (1,))

def loop_status_check(self, num):
    while self.switch_loop:
        result = 0
        result = self.application.pull_out_message('VALUE1')
        if result != self.preresultVALUE1:
            self.preresultVALUE1 = result
            self.result = bytes(chr(result).encode('utf-8'))
            self._updateValuecallbackVALUE1(self.result)
            LOGGER.info("going: preresultVALUE1: {}".format(self.preresultVALUE1))
        time.sleep(num)

`

ShinChoon commented 2 years ago

Just found the result source which is the app has some issues