Пример #1
0
def find_service(name=None, uuid=None, address=None):
    if not address:
        devices = discover_devices()
    else:
        devices = [address]

    results = []

    if uuid is not None and not is_valid_uuid(uuid):
        raise ValueError("invalid UUID")

    try:
        for addr in devices:
            try:
                s = _bt.SDPSession()
                s.connect(addr)
                matches = []
                if uuid is not None:
                    matches = s.search(uuid)
                else:
                    matches = s.browse()
            except _bt.error:
                continue

            if name is not None:
                matches = [s for s in matches if s.get("name", "") == name]

            for m in matches:
                m["host"] = addr

            results.extend(matches)
    except _bt.error as e:
        raise BluetoothError(*e.args)

    return results
Пример #2
0
def get_RSSI(addr):
    # Open an hci socket
    hci_sock = bluez.hci_open_dev()
    hci_fd = hci_sock.fileno()

    # Try to open a connection to remote BT device
    try:
        bt_sock = bluez.SDPSession()
        bt_sock.connect(addr)
    except:
        bt_sock.close()
        hci_sock.close()
        return None
    # Get handle to ACL connection to remote BT device
    reqstr = struct.pack("6sB17s", bluez.str2ba(addr), bluez.ACL_LINK,
                         "\0" * 17)
    request = array.array("c", reqstr)
    fcntl.ioctl(hci_fd, bluez.HCIGETCONNINFO, request, 1)
    handle = struct.unpack("8xH14x", request.tostring())[0]

    # Get RSSI
    cmd_pkt = struct.pack('H', handle)
    RSSI = bluez.hci_send_req(hci_sock, bluez.OGF_STATUS_PARAM,
                              bluez.OCF_READ_RSSI, bluez.EVT_CMD_COMPLETE, 4,
                              cmd_pkt)
    RSSI = struct.unpack('b', RSSI[3])[0]

    bt_sock.close()
    hci_sock.close()
    return RSSI