Esempio n. 1
0
def init_hci(iface: str = 'hci0'):
    # hciconfig <hci> up 的前提是 rfkill 先 unblock
    subprocess.check_output('rfkill unblock %d' % find_rfkill_devid(iface),
                            stderr=STDOUT,
                            timeout=5,
                            shell=True)
    subprocess.check_output('hciconfig {} up'.format(iface),
                            stderr=STDOUT,
                            timeout=5,
                            shell=True)
    subprocess.check_output('systemctl restart bluetooth.service',
                            stderr=STDOUT,
                            timeout=5,
                            shell=True)

    hci = HCI(iface)

    # 下面在发送各种 HCI command 时,如果出现如下异常:
    #     BlockingIOError: [Errno 11] Resource temporarily unavailable
    # 那么可能是 hci socket 被设为了 non-blocking mode。
    hci.inquiry_cancel()
    hci.exit_periodic_inquiry_mode()
    hci.write_scan_enable()  # No scan enabled
    event_params = hci.le_set_advertising_enable()  # Advertising is disabled
    if event_params['Status'] != 0x00:
        #print(WARNING, 'Status of HCI_LE_Set_Advertising_Enable command: 0x%02x'%event_params['Status'])
        pass

    try:
        hci.le_set_scan_enable({
            'LE_Scan_Enable': 0x00,  # Scanning disabled
            'Filter_Duplicates': 0x01  # Ignored
        })
    except RuntimeError as e:
        #print(WARNING, e)
        pass

    hci.set_event_filter({'Filter_Type': 0x00})  # Clear All Filters

    event_params = hci.read_bdaddr()
    if event_params['Status'] != 0:
        raise RuntimeError
    else:
        local_bd_addr = event_params['BD_ADDR'].upper()

    # Clear bluetoothd cache
    cache_path = PosixPath('/var/lib/bluetooth/') / local_bd_addr / 'cache'
    if cache_path.exists():
        for file in cache_path.iterdir():
            os.remove(file)

    hci.close()
Esempio n. 2
0
def init_hci(iface='hci0'):
    hci = HCI(iface)

    exitcode, output = subprocess.getstatusoutput('rfkill unblock %d' %
                                                  find_rfkill_devid(iface))
    if exitcode != 0:
        logger.error('rfkill: ' + output)
        sys.exit(exitcode)

    exitcode, output = subprocess.getstatusoutput("hciconfig up " + iface)
    if exitcode != 0:
        logger.error("Failed to up " + iface)
        sys.exit(exitcode)
    else:
        time.sleep(0.5)

    # hci.reset()
    hci.inquiry_cancel()
    hci.exit_periodic_inquiry_mode()

    hci.write_scan_enable()  # No scan enabled

    event_params = hci.le_set_advertising_enable()  # Advertising is disabled
    if event_params['Status'] != 0x00:
        #print(WARNING, 'Status of HCI_LE_Set_Advertising_Enable command: 0x%02x'%event_params['Status'])
        pass

    try:
        hci.le_set_scan_enable({
            'LE_Scan_Enable': 0x00,  # Scanning disabled
            'Filter_Duplicates': 0x01  # Ignored
        })
    except RuntimeError as e:
        #print(WARNING, e)
        pass

    hci.set_event_filter({'Filter_Type': 0x00})  # Clear All Filters

    event_params = hci.read_bdaddr()
    if event_params['Status'] != 0:
        raise RuntimeError
    else:
        local_bd_addr = event_params['BD_ADDR'].upper()

    # Clear bluetoothd cache
    cache_path = PosixPath('/var/lib/bluetooth/') / local_bd_addr / 'cache'
    if cache_path.exists():
        for file in cache_path.iterdir():
            os.remove(file)