Created
November 17, 2016 14:59
-
-
Save mikerr/372911c955e2a94b96089fbc300c2b5d to your computer and use it in GitHub Desktop.
Python script to output bluetooth LE MAC addresses and current RSSI (signal strength)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import struct | |
from ctypes import (CDLL, get_errno) | |
from ctypes.util import find_library | |
from socket import ( | |
socket, | |
AF_BLUETOOTH, | |
SOCK_RAW, | |
BTPROTO_HCI, | |
SOL_HCI, | |
HCI_FILTER, | |
) | |
btlib = find_library("bluetooth") | |
bluez = CDLL(btlib, use_errno=True) | |
dev_id = bluez.hci_get_route(None) | |
sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI) | |
sock.bind((dev_id,)) | |
err = bluez.hci_le_set_scan_parameters(sock.fileno(), 0, 0x10, 0x10, 0, 0, 1000); | |
if err < 0: | |
raise Exception("Set scan parameters failed") | |
# occurs when scanning is still enabled from previous call | |
# allows LE advertising events | |
hci_filter = struct.pack( | |
"<IQH", | |
0x00000010, | |
0x4000000000000000, | |
0 | |
) | |
sock.setsockopt(SOL_HCI, HCI_FILTER, hci_filter) | |
err = bluez.hci_le_set_scan_enable( | |
sock.fileno(), | |
1, # 1 - turn on; 0 - turn off | |
0, # 0-filtering disabled, 1-filter out duplicates | |
1000 # timeout | |
) | |
while True: | |
data = sock.recv(1024) | |
# print bluetooth address from LE Advert. packet | |
addr = ':'.join("{0:02x}".format(ord(x)) for x in data[12:6:-1]) | |
rssi = (ord(data[-1])) | |
print addr,rssi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have exactly the same question. I know this is quite old, but have someone figure it out by now ?
Thanks in advance