SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from network import Bluetooth
  2. from L99_BLEGATTSService import BLEGATTSService
  3.  
  4. class BLEGATTS:
  5.     def __init__(self):
  6.         self.bt_inst = None
  7.         self.advert_name = None
  8.         self.connect_callback = None
  9.         self.is_connected = False
  10.         self.services = { }
  11.  
  12.     def _connection_handler(self, bt):
  13.         events = bt.events()
  14.         if events & Bluetooth.CLIENT_CONNECTED:
  15.             self.is_connected = True
  16.             if self.connect_callback:
  17.                 self.connect_callback(self.is_connected)
  18.         elif events & Bluetooth.CLIENT_DISCONNECTED:
  19.             self.is_connected = False
  20.             if self.connect_callback:
  21.                 self.connect_callback(self.is_connected)
  22.  
  23.     def init(self, advert_name, advert_mfg_data = None, advert_service_data = None, advert_service_uuid = None, connect_callback = None):
  24.         self.bt_inst = Bluetooth(id=0,antenna=Bluetooth.INT_ANT)
  25.         self.advert_name = advert_name
  26.         self.connect_callback = connect_callback
  27.         self.bt_inst.set_advertisement(name=self.advert_name, manufacturer_data=advert_mfg_data, service_data=advert_service_data, service_uuid=advert_service_uuid)
  28.         self.bt_inst.callback(trigger=Bluetooth.CLIENT_CONNECTED|Bluetooth.CLIENT_DISCONNECTED, handler=self._connection_handler)
  29.  
  30.     def advertise(self, toggle=True, service_uuid=None):
  31.         self.bt_inst.advertise(toggle)
  32.  
  33.     def disconnect(self):
  34.         if self.is_connected:
  35.             self.bt_inst.disconnect_client()
  36.  
  37.     def deinit(self):
  38.         #TODO: stop services
  39.         self.disconnect()
  40.         self.bt_inst.deinit()
  41.  
  42.     def addService(self, service_name, uuid):
  43.         self.services[service_name] = BLEGATTSService(self.bt_inst, uuid)
  44.         return self.services[service_name]
  45.  
  46.     def getService(self, service_name):
  47.         return service_name in self.services and self.services[service_name] or None
  48.  
  49.     def setCharValue(self, svc_name, char_name, value):
  50.         if self.is_connected and svc_name in self.services and char_name in self.services[svc_name].characteristics:
  51.             #print ('%s:%s=%s' % (svc_name,char_name,str(value)))
  52.             self.services[svc_name].characteristics[char_name].setValue(value)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top