def __init__(self, hass, devices, bt_device_id): """Construct interface object.""" self.hass = hass # List of beacons to monitor self.devices = devices # Number of the bt device (hciX) self.bt_device_id = bt_device_id def callback(bt_addr, _, packet, additional_info): """Handle new packets.""" temperature = struct.unpack('<h', struct.pack('>H', packet.temperature))[0] / 256 self.process_packet( additional_info['namespace'] if additional_info else None, additional_info['instance'] if additional_info else None, bt_addr, temperature) # pylint: disable=import-error from beacontools import ( BeaconScanner, BtAddrFilter, EddystoneFilter, EddystoneTLMFrame) device_filters = [(BtAddrFilter(d.mac) if d.mac else EddystoneFilter(d.namespace, d.instance)) for d in devices] self.scanner = BeaconScanner( callback, bt_device_id, device_filters, EddystoneTLMFrame) self.scanning = False
def test_process_packet_btaddr(self): """Test processing of a packet and callback execution with bt addr filter.""" callback = MagicMock() scanner = BeaconScanner( callback, device_filter=BtAddrFilter("1c:d6:cd:ef:94:35")) pkt = b"\x41\x3e\x41\x02\x01\x03\x01\x35\x94\xef\xcd\xd6\x1c\x19\x02\x01\x06\x03\x03\xaa"\ b"\xfe\x11\x16\xaa\xfe\x00\xe3\x12\x34\x56\x78\x90\x12\x34\x67\x89\x01\x00\x00\x00"\ b"\x00\x00\x01\x00\x00\xdd" scanner._mon.process_packet(pkt) self.assertEqual(callback.call_count, 1) args = callback.call_args[0] self.assertEqual(args[0], "1c:d6:cd:ef:94:35") self.assertEqual(args[1], -35) self.assertIsInstance(args[2], EddystoneUIDFrame) self.assertEqual(args[3], { "namespace": "12345678901234678901", "instance": "000000000001" })
def test_invalid_bt_filter(self): """Test passing of an invalid bluetooth address as filter.""" callback = MagicMock() with self.assertRaises(ValueError): BeaconScanner(callback, device_filter=BtAddrFilter("this is crap"))
def test_repr_filter(self): self.assertEqual( BtAddrFilter("aa:bb:cc:dd:ee:ff").__repr__(), "BtAddrFilter(bt_addr=aa:bb:cc:dd:ee:ff)")
import time from beacontools import BtAddrFilter, BeaconScanner, IBeaconFilter, CYPRESS_BEACON_DEFAULT_UUID def callback(bt_addr, rssi, packet, additional_info): print("<%s, %d> Major:%s %.1fdegC %.1f %%RH" % (bt_addr, rssi, packet.major, packet.cypress_temperature, packet.cypress_humidity)) scanner = BeaconScanner(callback,device_filter=BtAddrFilter(bt_addr="00:A0:50:29:34:D5")) scanner.start() time.sleep(6*60) scanner.stop()
import time from beacontools import BtAddrFilter, BeaconScanner, IBeaconFilter, CYPRESS_BEACON_DEFAULT_UUID def callback(bt_addr, rssi, packet, additional_info): print("<%s, %d> Major:%s %.1fdegC %.1f %%RH" % (bt_addr, rssi, packet.major, packet.cypress_temperature, packet.cypress_humidity)) # scan for all iBeacon advertisements from beacons with the specified uuid scanner = BeaconScanner( callback, device_filter=BtAddrFilter(bt_addr="00:A0:50:17:1F:1D")) scanner.start() # Cypress beacons by default transmit every 5 minutes time.sleep(6 * 60) scanner.stop()
addrA1 = "18:04:ED:51:7A:F1"; #lists for storing RSSI values, need one for each beacon listA0 = []; listA1 = []; #define function "callback" which prints BT info #need a copy for each beacon to scan def callbackA0(bt_addr, rssi, packet, additional_info): listA0.append(rssi); def callbackA1(bt_addr, rssi, packet, additional_info): listA1.append(rssi); #scan for iBeacon advertisements from beacons with the specified address scannerA0 = BeaconScanner(callbackA0,device_filter=BtAddrFilter(bt_addr=addrA0)) scannerA1 = BeaconScanner(callbackA1,device_filter=BtAddrFilter(bt_addr=addrA1)) #start scanning process scannerA0.start() scannerA1.start() #run amount of scans equal to 'runs' while (count < runs): time.sleep(scanTime) #wait for time period defined by 'scanTime' #average RSSI values avgA0 = int(sum(listA0) / len(listA0)) avgA1 = int(sum(listA1) / len(listA1)) #distance calculation, in cm