mirror of
https://github.com/kawasaki/pyscrlink.git
synced 2025-09-06 09:40:14 +02:00
BLESession.startNotifications: Refactor out to a function
This commit is contained in:
committed by
Shin'ichiro Kawasaki
parent
6fdc8dcbb3
commit
e42869c93b
@@ -513,17 +513,7 @@ class BLESession(Session):
|
|||||||
message = base64.standard_b64encode(b).decode('ascii')
|
message = base64.standard_b64encode(b).decode('ascii')
|
||||||
res['result'] = { 'message': message, 'encode': 'base64' }
|
res['result'] = { 'message': message, 'encode': 'base64' }
|
||||||
if params.get('startNotifications') == True:
|
if params.get('startNotifications') == True:
|
||||||
logger.debug(f"start notification for {chara_id}")
|
self.startNotifications(service_id, chara_id)
|
||||||
service = self.perip.getServiceByUUID(UUID(service_id))
|
|
||||||
chas = service.getCharacteristics(forUUID=chara_id)
|
|
||||||
handle = chas[0].getHandle()
|
|
||||||
# prepare notification handler
|
|
||||||
self.delegate.add_handle(service_id, chara_id, handle)
|
|
||||||
# request notification to the BLE device
|
|
||||||
self.lock.acquire()
|
|
||||||
self.perip.writeCharacteristic(chas[0].getHandle() + 1,
|
|
||||||
b"\x01\x00", True)
|
|
||||||
self.lock.release()
|
|
||||||
|
|
||||||
elif self.status == self.CONNECTED and method == 'write':
|
elif self.status == self.CONNECTED and method == 'write':
|
||||||
logger.debug("handle write request")
|
logger.debug("handle write request")
|
||||||
@@ -548,6 +538,19 @@ class BLESession(Session):
|
|||||||
logger.debug(res)
|
logger.debug(res)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def startNotifications(self, service_id, chara_id):
|
||||||
|
logger.debug(f"start notification for {chara_id}")
|
||||||
|
service = self.perip.getServiceByUUID(UUID(service_id))
|
||||||
|
chas = service.getCharacteristics(forUUID=chara_id)
|
||||||
|
handle = chas[0].getHandle()
|
||||||
|
# prepare notification handler
|
||||||
|
self.delegate.add_handle(service_id, chara_id, handle)
|
||||||
|
# request notification to the BLE device
|
||||||
|
self.lock.acquire()
|
||||||
|
self.perip.writeCharacteristic(chas[0].getHandle() + 1,
|
||||||
|
b"\x01\x00", True)
|
||||||
|
self.lock.release()
|
||||||
|
|
||||||
def end_request(self):
|
def end_request(self):
|
||||||
logger.debug("end_request of BLESession")
|
logger.debug("end_request of BLESession")
|
||||||
if self.delegate:
|
if self.delegate:
|
||||||
|
Reference in New Issue
Block a user