class PyBluetooth: def __init__(self, name, serviceUUID): self._bt = Bluetooth() self.setAdvertisement(name=name, serviceUUID=serviceUUID) self._bt.callback(trigger=Bluetooth.CLIENT_CONNECTED, handler=self.onClientConnect, arg=self) self._bt.callback(trigger=Bluetooth.CLIENT_DISCONNECTED, handler=self.onClientConnect, arg=self) self._services = {} def __del__(self): self._bt.disconnect_client() def addService(self, uuid, characteristicNumber=0, isPrimary=False): self._services[uuid] = self._bt.service(self.formatUUID(uuid), nbr_chars=characteristicNumber, isprimary=True) return self._services[uuid] def getService(self, uuid): return self._services[uuid] def setAdvertisement(self, name, manufacturerData=None, serviceData=None, serviceUUID=None): self._name = name if serviceUUID != None: serviceUUID = self.formatUUID(serviceUUID) self._bt.set_advertisement(name=name, manufacturer_data=manufacturerData, service_data=serviceData, service_uuid=serviceUUID) def startAdvertisement(self): self._bt.advertise(True) def stopAdvertisement(self): self._bt.advertise(False) def formatUUID(self, uuid): uuid = uuid.encode().replace(b'-', b'') tmp = binascii.unhexlify(uuid) return bytes(reversed(tmp)) def onClientConnect(self): events = self._bt.events() print("[" + self._name + "]: Client connected") pycom.heartbeat(False) pycom.rgbled(0x110011) def onClientDisconnect(self): print("[" + self._name + "]: Client disconnected") pycom.heartbeat(True)
class Blues: # Initialization method. def __init__(self): print('Advertising Bluetooth...') self.mBluetooth = Bluetooth() self.mEvents = self.mBluetooth.events() # self.mBluetooth.init([id=0, mode=Bluetooth.BLE, antenna=Bluetooth.INT_ANT, modem_sleep=False, pin=None, privacy=True, secure_connections=True, mtu=200]) self.mBluetooth.set_advertisement( name='FiPy', service_uuid=2) self.mBluetooth.callback(trigger=self.mBluetooth.CLIENT_CONNECTED | self.mBluetooth.CLIENT_DISCONNECTED, handler=self.Connection) self.mBluetooth.advertise(True) # Create Service & Characteristic self.CreateServChar() self.mCharacteristic.callback( trigger=self.mBluetooth.CHAR_WRITE_EVENT | self.mBluetooth.CHAR_READ_EVENT, handler=self.CharCallback) # Connection method: Used as callback function to detect a BLE connection. def Connection(self, zResponse): self.mEvents = zResponse.events() if self.mEvents & self.mBluetooth.CLIENT_CONNECTED: print("Client connected") elif self.mEvents & self.mBluetooth.CLIENT_DISCONNECTED: print("Client disconnected") def CreateServChar(self): # Create service and characteristic self.mService = self.mBluetooth.service( uuid=3, isprimary=True) self.mCharacteristic = self.mService.characteristic( uuid=31, properties=self.mBluetooth.PROP_WRITE | self.mBluetooth.PROP_READ, value=6) def CharCallback(self, chr, zResponse): self.mEvents, value = zResponse if self.mEvents & self.mBluetooth.CHAR_READ_EVENT: print("Read from Char = {}".format(value)) if self.mEvents & self.mBluetooth.CHAR_WRITE_EVENT: print("Written to Char = {}".format(value))