├── README.md ├── testblescan.py └── blescan.py /README.md: -------------------------------------------------------------------------------- 1 | Replaced by github.com/switchdoclabs/SDL_Pi_iBeaconScanner 2020 2 | 3 | 4 | SwitchDoc Labs, LLC 5 | June 2014 6 | 7 | blescanner is a python program designed to read iBeacon advertizments using a Bluetooth Dongle on a Raspberry Pi 8 | 9 | To test, "sudo python testblescanner.py" 10 | 11 | 12 | -------------------------------------------------------------------------------- /testblescan.py: -------------------------------------------------------------------------------- 1 | # test BLE Scanning software 2 | # jcs 6/8/2014 3 | 4 | import blescan 5 | import sys 6 | 7 | import bluetooth._bluetooth as bluez 8 | 9 | dev_id = 0 10 | try: 11 | sock = bluez.hci_open_dev(dev_id) 12 | print "ble thread started" 13 | 14 | except: 15 | print "error accessing bluetooth device..." 16 | sys.exit(1) 17 | 18 | blescan.hci_le_set_scan_parameters(sock) 19 | blescan.hci_enable_le_scan(sock) 20 | 21 | while True: 22 | returnedList = blescan.parse_events(sock, 10) 23 | print "----------" 24 | for beacon in returnedList: 25 | print beacon 26 | 27 | -------------------------------------------------------------------------------- /blescan.py: -------------------------------------------------------------------------------- 1 | # BLE iBeaconScanner based on https://github.com/adamf/BLE/blob/master/ble-scanner.py 2 | # JCS 06/07/14 3 | 4 | DEBUG = False 5 | # BLE scanner based on https://github.com/adamf/BLE/blob/master/ble-scanner.py 6 | # BLE scanner, based on https://code.google.com/p/pybluez/source/browse/trunk/examples/advanced/inquiry-with-rssi.py 7 | 8 | # https://github.com/pauloborges/bluez/blob/master/tools/hcitool.c for lescan 9 | # https://kernel.googlesource.com/pub/scm/bluetooth/bluez/+/5.6/lib/hci.h for opcodes 10 | # https://github.com/pauloborges/bluez/blob/master/lib/hci.c#L2782 for functions used by lescan 11 | 12 | # performs a simple device inquiry, and returns a list of ble advertizements 13 | # discovered device 14 | 15 | # NOTE: Python's struct.pack() will add padding bytes unless you make the endianness explicit. Little endian 16 | # should be used for BLE. Always start a struct.pack() format string with "<" 17 | 18 | import os 19 | import sys 20 | import struct 21 | import bluetooth._bluetooth as bluez 22 | 23 | LE_META_EVENT = 0x3e 24 | LE_PUBLIC_ADDRESS=0x00 25 | LE_RANDOM_ADDRESS=0x01 26 | LE_SET_SCAN_PARAMETERS_CP_SIZE=7 27 | OGF_LE_CTL=0x08 28 | OCF_LE_SET_SCAN_PARAMETERS=0x000B 29 | OCF_LE_SET_SCAN_ENABLE=0x000C 30 | OCF_LE_CREATE_CONN=0x000D 31 | 32 | LE_ROLE_MASTER = 0x00 33 | LE_ROLE_SLAVE = 0x01 34 | 35 | # these are actually subevents of LE_META_EVENT 36 | EVT_LE_CONN_COMPLETE=0x01 37 | EVT_LE_ADVERTISING_REPORT=0x02 38 | EVT_LE_CONN_UPDATE_COMPLETE=0x03 39 | EVT_LE_READ_REMOTE_USED_FEATURES_COMPLETE=0x04 40 | 41 | # Advertisment event types 42 | ADV_IND=0x00 43 | ADV_DIRECT_IND=0x01 44 | ADV_SCAN_IND=0x02 45 | ADV_NONCONN_IND=0x03 46 | ADV_SCAN_RSP=0x04 47 | 48 | 49 | def returnnumberpacket(pkt): 50 | myInteger = 0 51 | multiple = 256 52 | for c in pkt: 53 | myInteger += struct.unpack("B",c)[0] * multiple 54 | multiple = 1 55 | return myInteger 56 | 57 | def returnstringpacket(pkt): 58 | myString = ""; 59 | for c in pkt: 60 | myString += "%02x" %struct.unpack("B",c)[0] 61 | return myString 62 | 63 | def printpacket(pkt): 64 | for c in pkt: 65 | sys.stdout.write("%02x " % struct.unpack("B",c)[0]) 66 | 67 | def get_packed_bdaddr(bdaddr_string): 68 | packable_addr = [] 69 | addr = bdaddr_string.split(':') 70 | addr.reverse() 71 | for b in addr: 72 | packable_addr.append(int(b, 16)) 73 | return struct.pack("