Power ON or OFF a bluetooth device. :param dev_id: Device id. :type dev_id: ``int`` :param enable: Whether to enable of disable the device. :type enable: ``bool``
(dev_id, enable)
| 77 | |
| 78 | |
| 79 | def toggle_device(dev_id, enable): |
| 80 | """ |
| 81 | Power ON or OFF a bluetooth device. |
| 82 | |
| 83 | :param dev_id: Device id. |
| 84 | :type dev_id: ``int`` |
| 85 | :param enable: Whether to enable of disable the device. |
| 86 | :type enable: ``bool`` |
| 87 | """ |
| 88 | hci_sock = socket.socket(socket.AF_BLUETOOTH, |
| 89 | socket.SOCK_RAW, |
| 90 | socket.BTPROTO_HCI) |
| 91 | # print("Power %s bluetooth device %d" % ('ON' if enable else 'OFF', dev_id)) |
| 92 | # di = struct.pack("HbBIBBIIIHHHH10I", dev_id, *((0,) * 22)) |
| 93 | # fcntl.ioctl(hci_sock.fileno(), bluez.HCIGETDEVINFO, di) |
| 94 | req_str = struct.pack("H", dev_id) |
| 95 | request = array.array("b", req_str) |
| 96 | try: |
| 97 | fcntl.ioctl(hci_sock.fileno(), |
| 98 | bluez.HCIDEVUP if enable else bluez.HCIDEVDOWN, |
| 99 | request[0]) |
| 100 | except IOError as e: |
| 101 | if e.errno == EALREADY: |
| 102 | pass |
| 103 | # print("Bluetooth device %d is already %s" % ( |
| 104 | # dev_id, 'enabled' if enable else 'disabled')) |
| 105 | else: |
| 106 | raise |
| 107 | finally: |
| 108 | hci_sock.close() |
| 109 | |
| 110 | |
| 111 | # Types of bluetooth scan |
no outgoing calls
no test coverage detected