# iBeacon scanner * 之前的版本: https://github.com/jay16213/pi-ble-scanner * 此版本使用兩個樹莓派, 如果同時掃到同一個uuid, 才視為有這個人在. 現在使用一個裝置即可. * ref: https://github.com/citruz/beacontools * 安裝相關套件 ``` # install libbluetooth headers and libpcap2 sudo apt-get install python-dev libbluetooth-dev libcap2-bin # grant the python executable permission to access raw socket data sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f $(which python3)) # install beacontools with scanning support # 看是使用python或python3 pip install beacontools[scan] pip3 install beacontools[scan] ``` * 掃描程式 (其實跟github上是同一個, 只是少了socket與arg的功能. 然後多一個RSSI的資訊) ``` import time, sys import socket #import argparse from beacontools import BeaconScanner, IBeaconFilter #parser = argparse.ArgumentParser() #parser.add_argument("id") #parser.add_argument("addr") #parser.add_argument("port", type=int) def callback(bt_addr, rssi, packet, additional_info): # print("<%s, %d> %s %s" % (bt_addr, rssi, packet, additional_info)) uuid = additional_info['uuid'] global numOfPeople global validUUID if uuid in validUUID: # if this uuid is already scanned, ignore it if uuid not in scanList: print("legal uuid: {}".format(uuid)) print("RSSI: {}".format(rssi)) scanList.append(uuid) numOfPeople += 1 else: print("illegal uuid: {}".format(uuid)) def getValidUUID(): print("loading legal uuid") v = [] with open('uuid.txt') as f: for line in f: uuid = line.split("\n")[0] # cut the newline character print(uuid) v.append(uuid) return v if __name__=='__main__': # args = parser.parse_args() # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # sock.connect((args.addr, args.port)) numOfPeople = 0 validUUID = getValidUUID() scanner = BeaconScanner(callback) print("start scan") scanner.start() while True: try: print("----------") numOfPeople = 0 scanList = [] time.sleep(2) print("scanned: {}".format(numOfPeople)) # # convert list of uuid to a long string, separate # uuid_list = ' '.join(scanList) # # fix msg to 1024 bytes and encode in ascii # msg = str("{} {}".format(len(scanList), uuid_list)).ljust(1024, '\x00').encode("ascii") # sock.send(msg) except KeyboardInterrupt: # if user press ctrl-C => exit the program scanner.stop() # sock.close() sys.exit(0) ``` * 準備一個合法uuid list: uuid.txt * 直接看尾數的01, 02...來區分人員身分 ``` 00000000-1111-1111-0000-000000556601 00000000-1111-1111-0000-000000556602 . . . ``` * iBeacon的設定方式 (要用iphone或ipad才可以) * https://youyouyou.pixnet.net/blog/post/119467351