SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from network import Bluetooth
  2. from L99_BLEGATTSCharacteristic import BLEGATTSCharacteristic
  3.  
  4. class BLEGATTSService:
  5.     def __init__(self, bt_inst, uuid):
  6.         self.uuid = uuid
  7.         self.bt_inst = bt_inst
  8.         self.bt_svc_inst = None
  9.         self.is_defined = False
  10.         self.is_started = False
  11.         self.characteristics = { }
  12.  
  13.     def start(self):
  14.         if not self.is_defined:
  15.             self.bt_svc_inst = self.bt_inst.service(uuid=self.uuid, isprimary=True, nbr_chars=len(self.characteristics), start=False)
  16.             for key, char in self.characteristics.items():
  17.                 char.setInstance(self.bt_svc_inst.characteristic(uuid=char.uuid, properties=char.prop, value=char.static_read))
  18.             self.is_defined = True
  19.  
  20.         if not self.is_started:
  21.             self.bt_svc_inst.start()
  22.             self.is_started = True
  23.  
  24.         return self
  25.  
  26.     def stop(self):
  27.         if self.is_started:
  28.             self.bt_svc_inst.stop()
  29.             self.is_started = False
  30.  
  31.         return self
  32.  
  33.     def addReadChar(self, name, uuid, static_read=None, dynamic_read=None):
  34.         self.characteristics[name] = BLEGATTSCharacteristic(name,uuid,Bluetooth.PROP_INDICATE|Bluetooth.PROP_BROADCAST|Bluetooth.PROP_READ,static_read=static_read,dynamic_read=dynamic_read,dynamic_write=None)
  35.         return self#.characteristics[name]
  36.    
  37.     def addReadNotifyChar(self, name, uuid, static_read=None, dynamic_read=None, extended=True):
  38.         self.characteristics[name] = BLEGATTSCharacteristic(name,uuid,Bluetooth.PROP_INDICATE|Bluetooth.PROP_BROADCAST|Bluetooth.PROP_READ|Bluetooth.PROP_NOTIFY,static_read=static_read,dynamic_read=dynamic_read,dynamic_write=None)
  39.         return self#.characteristics[name]
  40.  
  41.     def addNotifyChar(self, name, uuid, static_read=None, dynamic_read=None):
  42.         self.characteristics[name] = BLEGATTSCharacteristic(name,uuid,Bluetooth.PROP_INDICATE|Bluetooth.PROP_BROADCAST|Bluetooth.PROP_NOTIFY,static_read=static_read,dynamic_read=dynamic_read,dynamic_write=None)
  43.         return self#.characteristics[name]
  44.  
  45.     def addReadWriteChar(self, name, uuid, static_read=None, dynamic_read=None, dynamic_write=None):
  46.         self.characteristics[name] = BLEGATTSCharacteristic(name,uuid,Bluetooth.PROP_INDICATE|Bluetooth.PROP_BROADCAST|Bluetooth.PROP_READ|Bluetooth.PROP_WRITE,static_read=static_read,dynamic_read=dynamic_read,dynamic_write=dynamic_write)
  47.         return self#.characteristics[name]
  48.  
  49.     def addReadWriteNotifyChar(self, name, uuid, static_read=None, dynamic_read=None, dynamic_write=None):
  50.         self.characteristics[name] = BLEGATTSCharacteristic(name,uuid,Bluetooth.PROP_INDICATE|Bluetooth.PROP_BROADCAST|Bluetooth.PROP_READ|Bluetooth.PROP_WRITE|Bluetooth.PROP_NOTIFY,static_read=static_read,dynamic_read=dynamic_read,dynamic_write=dynamic_write)
  51.         return self#.characteristics[name]
  52.  
  53.     # HACK: since we can't do descriptors, we need to somehow let the client know that this char is carrying analog values so we add EXT_PROP
  54.     def addReadNotifyCharForAnalog(self, name, uuid, static_read=None, dynamic_read=None, extended=True):
  55.         self.characteristics[name] = BLEGATTSCharacteristic(name,uuid,Bluetooth.PROP_EXT_PROP|Bluetooth.PROP_INDICATE|Bluetooth.PROP_BROADCAST|Bluetooth.PROP_READ|Bluetooth.PROP_NOTIFY,static_read=static_read,dynamic_read=dynamic_read,dynamic_write=None)
  56.         return self#.characteristics[name]
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