IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.6k stars 490 forks source link

code stuck while writing to glucose measurement characteristic to notify #347

Open neha-s-deshpande opened 5 years ago

neha-s-deshpande commented 5 years ago

Hi, I am writing code on RPi which connects to device having glucose service. It worked well but from few days it is unable to write to the glucose measurement characteristic and code stuck there while waiting for response from device.

!usr/bin/env python

from future import print_function import bluepy from bluepy import btle from bluepy.btle import Scanner, DefaultDelegate, Peripheral from time import sleep,time, mktime import binascii import datetime import os

handleDiscovery function is called when new device is scanned

class ScanDelegate(DefaultDelegate):

def __init__(self):
    DefaultDelegate.__init__(self)

def handleDiscovery(self, dev, isNewDev, isNewData):       
    name = repr(dev.getValueText(dev.COMPLETE_LOCAL_NAME))

class MyDelegate_glucose(DefaultDelegate): def init(self): btle.DefaultDelegate.init(self)

def handleNotification(self, cHandle, data):

    data = binascii.hexlify(data)
    print(data)

write to glucose characteristic

def glucose_data(connected_dev):
connected_dev.withDelegate(MyDelegate_glucose())#stores the reference for connected device which receives callbacks for glucose meter try: connected_dev.writeCharacteristic(12,bytearray([0x01,0x00]),withResponse = True)#configure glucose measurement characteristic for notification connected_dev.writeCharacteristic(15,bytearray([0x01,0x00]),withResponse = True)#configure glucose measurement context for notification connected_dev.writeCharacteristic(20,bytearray([0x02,0x00]),withResponse = True)#configure record access control point for indiaction connected_dev.writeCharacteristic(19,bytearray([0x01,0x01]),withResponse = True)#write to record access control point to request for stored records except: print("exception occured") end_time = time()+30 while time()<end_time: if connected_dev.waitForNotifications(30): print("before calling handler") continue

scanner = Scanner().withDelegate(ScanDelegate()) devices = scanner.scan(2) ADDR_TO_CONNECT = connected_dev = Peripheral(ADDR_TO_CONNECT) glucose_data(connected_dev)