src/eric7/MicroPython/Devices/CircuitPythonDevices.py

branch
mpy_network
changeset 9855
c9244db5566a
parent 9841
3c6118eee33e
child 9857
0122ae72618d
diff -r c1e298e5c588 -r c9244db5566a src/eric7/MicroPython/Devices/CircuitPythonDevices.py
--- a/src/eric7/MicroPython/Devices/CircuitPythonDevices.py	Mon Mar 06 17:03:40 2023 +0100
+++ b/src/eric7/MicroPython/Devices/CircuitPythonDevices.py	Tue Mar 07 16:22:07 2023 +0100
@@ -83,6 +83,12 @@
             6: "[wifi.AuthMode.WPA3, wifi.AuthMode.PSK]",
             7: "[wifi.AuthMode.WPA2, wifi.AuthMode.WPA3, wifi.AuthMode.PSK]",
         }
+        self.__bleAddressType = {
+            0: self.tr("Public"),
+            1: self.tr("Random Static"),
+            2: self.tr("Random Private Resolvable"),
+            3: self.tr("Random Private Non-Resolvable"),
+        }
 
     def setConnected(self, connected):
         """
@@ -1137,6 +1143,142 @@
             self.tr("CircuitPython does not support reporting of connected clients."),
         )
 
+    ##################################################################
+    ## Methods below implement Bluetooth related methods
+    ##################################################################
+
+    def hasBluetooth(self):
+        """
+        Public method to check the availability of Bluetooth.
+
+        @return flag indicating the availability of Bluetooth
+        @rtype bool
+        """
+        command = """
+def has_bt():
+    try:
+        import _bleio
+        if hasattr(_bleio, 'adapter'):
+            return True
+    except ImportError:
+        pass
+
+    return False
+
+print(has_bt())
+del has_bt
+"""
+        out, err = self._interface.execute(
+            command, mode=self._submitMode, timeout=10000
+        )
+        if err:
+            raise OSError(self._shortError(err))
+        return out.strip() == b"True"
+
+    def getBluetoothStatus(self):
+        """
+        Public method to get Bluetooth status data of the connected board.
+
+        @return list of tuples containing the translated status data label and
+            the associated value
+        @rtype list of tuples of (str, str)
+        """
+        command = """
+def ble_status():
+    import _bleio
+    import binascii
+    import json
+
+    a = _bleio.adapter
+
+    ble_enabled = a.enabled
+    if not ble_enabled:
+        a.enabled = True
+
+    res = {
+        'active': ble_enabled,
+        'mac': binascii.hexlify(bytes(reversed(a.address.address_bytes)), ':').decode(),
+        'addr_type': a.address.type,
+        'name': a.name,
+        'advertising': a.advertising,
+        'connected': a.connected,
+    }
+
+    if not ble_enabled:
+        a.enabled = False
+
+    print(json.dumps(res))
+
+ble_status()
+del ble_status
+"""
+        out, err = self._interface.execute(command, mode=self._submitMode)
+        if err:
+            raise OSError(self._shortError(err))
+
+        status = []
+        bleStatus = json.loads(out.decode("utf-8"))
+        status.append((self.tr("Active"), self.bool2str(bleStatus["active"])))
+        status.append((self.tr("Name"), bleStatus["name"]))
+        status.append((self.tr("MAC-Address"), bleStatus["mac"]))
+        status.append(
+            (self.tr("Address Type"), self.__bleAddressType[bleStatus["addr_type"]])
+        )
+        status.append((self.tr("Connected"), self.bool2str(bleStatus["connected"])))
+        status.append((self.tr("Advertising"), self.bool2str(bleStatus["advertising"])))
+
+        return status
+
+    def activateBluetoothInterface(self):
+        """
+        Public method to activate the Bluetooth interface.
+
+        @return flag indicating the new state of the Bluetooth interface
+        @rtype bool
+        """
+        command = """
+def activate_ble():
+    import _bleio
+
+    a = _bleio.adapter
+    if not a.enabled:
+        a.enabled = True
+    print(a.enabled)
+
+activate_ble()
+del activate_ble
+"""
+        out, err = self._interface.execute(command, mode=self._submitMode)
+        if err:
+            raise OSError(self._shortError(err))
+
+        return out.strip() == b"True"
+
+    def deactivateBluetoothInterface(self):
+        """
+        Public method to deactivate the Bluetooth interface.
+
+        @return flag indicating the new state of the Bluetooth interface
+        @rtype bool
+        """
+        command = """
+def deactivate_ble():
+    import _bleio
+
+    a = _bleio.adapter
+    if a.enabled:
+        a.enabled = False
+    print(a.enabled)
+
+deactivate_ble()
+del deactivate_ble
+"""
+        out, err = self._interface.execute(command, mode=self._submitMode)
+        if err:
+            raise OSError(self._shortError(err))
+
+        return out.strip() == b"True"
+
 
 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber):
     """

eric ide

mercurial