src/eric7/MicroPython/Devices/EspDevices.py

branch
mpy_network
changeset 9855
c9244db5566a
parent 9848
3d750b2e012c
child 9857
0122ae72618d
diff -r c1e298e5c588 -r c9244db5566a src/eric7/MicroPython/Devices/EspDevices.py
--- a/src/eric7/MicroPython/Devices/EspDevices.py	Mon Mar 06 17:03:40 2023 +0100
+++ b/src/eric7/MicroPython/Devices/EspDevices.py	Tue Mar 07 16:22:07 2023 +0100
@@ -1038,6 +1038,149 @@
         clientsList = ast.literal_eval(out.decode("utf-8"))
         return clientsList, ""
 
+    ##################################################################
+    ## 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 bluetooth
+        if hasattr(bluetooth, 'BLE'):
+            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 bluetooth
+    import ubinascii
+    import ujson
+
+    ble = bluetooth.BLE()
+
+    ble_active = ble.active()
+    if not ble_active:
+        ble.active(True)
+
+    res = {
+        'active': ble_active,
+        'mac': ubinascii.hexlify(ble.config('mac')[1], ':').decode(),
+        'addr_type': ble.config('mac')[0],
+        'name': ble.config('gap_name'),
+        'rxbuf': ble.config('rxbuf'),
+        'mtu': ble.config('mtu'),
+    }
+
+    if not ble_active:
+        ble.active(False)
+
+    print(ujson.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.tr("Public") if bleStatus == 0 else self.tr("Random"),
+            )
+        )
+        status.append(
+            (self.tr("Rx-Buffer"), self.tr("{0} Bytes").format(bleStatus["rxbuf"]))
+        )
+        status.append(
+            (self.tr("MTU"), self.tr("{0} Bytes").format(bleStatus["mtu"]))
+        )
+
+        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 bluetooth
+
+    ble = bluetooth.BLE()
+    if not ble.active():
+        ble.active(True)
+    print(ble.active())
+
+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 bluetooth
+
+    ble = bluetooth.BLE()
+    if ble.active():
+        ble.active(False)
+    print(ble.active())
+
+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