def scan(self, timeout=1): devices = asyncio.run_coroutine_threadsafe( discover(timeout=timeout, device=self.device), self.loop).result(timeout * 3) # We need to keep scanning going for connect() to properly work asyncio.run_coroutine_threadsafe( discover(timeout=timeout, device=self.device), self.loop) return [ (dev.name, dev.address) for dev in devices if dev.metadata.get('manufacturer_data', {}).get(_manuf_id, []) in [_manuf_data_xiaomi, _manuf_data_xiaomi_pro, _manuf_data_ninebot] ]
async def ble_discover(self, loop, time): """ :param loop: :param time: :return: """ task1 = loop.create_task(discover(time)) await asyncio.wait([task1]) return task1
def _discover_neo(disc_time): logger.info('discovering buzz %s seconds', disc_time) devices = discover(disc_time) buzz_addr = None for d in devices: if str(d).find("Buzz") > 0: print("Found a Buzz! " + str(d) + "\r\nAddress substring: " + str(d)[:17]) # set the address to a found Buzz buzz_addr = str(d)[:17] break return buzz_addr
def update_nearby_devices(self): """Scans for nearby Bluetooth LE devices and adds each found device to the internal list model.""" loop = asyncio.get_event_loop() devices = loop.run_until_complete(bleak.discover(2.5)) self.devices.clear() for device in devices: self.beginInsertRows(QtCore.QModelIndex(), 0, 0) self.devices.append({ "name": device.name, "address": device.address }) self.endInsertRows()
def AV_ble_discover(search_txt_p=None): loop_l = asyncio.get_event_loop() devices_l = loop_l.run_until_complete(bleak.discover()) if search_txt_p is None: return devices_l else: selected_devices_l = [] for d_l in devices_l: if search_txt_p in str(d_l): selected_devices_l.append(d_l) return selected_devices_l
def start_scan(self, prefixes=b"", *, buffer_size=512, extended=False, timeout=0.1, interval=0.1, window=None, minimum_rssi=-80, active=True): start_time = time.monotonic() while not timeout or time.monotonic() - start_time < timeout: devices = asyncio.get_event_loop().run_until_complete( bleak.discover(timeout=interval)) for device in devices: se = ScanEntry(device) if se.matches(prefixes, all=False): yield se
def __init__(self, addr: str = None): loop = asyncio.get_event_loop() devices = loop.run_until_complete(discover()) for device in devices: if addr: if device.address == addr: self.device = device break else: try: if 1076 in device.metadata["manufacturer_data"].keys(): self.device = device break except KeyError: pass else: raise RuntimeError( "No address provided and could not find device via scan.") loop.run_until_complete(self._refresh_data())
def discover_wattcheker(self): def confirm_selection(selected): frame_device_list.destroy() ordinal = selected.get() bdaddr = list_wattchecker[ordinal].address self.setup_wattcheker(bdaddr) frame_device_list = tk.Frame(self.master) frame_device_list.grid(sticky=tk.NSEW) self.master.resizable(False, False) ble_devices = asyncio.get_event_loop().run_until_complete(discover()) list_wattchecker = [d for d in ble_devices if 'BTWATTCH2' in d.name] if list_wattchecker: selected = tk.IntVar() for i in range(len(list_wattchecker)): ttk.Radiobutton(frame_device_list, value=i, variable=selected, text=list_wattchecker[i]).pack() button = ttk.Button(frame_device_list, text='connect', command=lambda: confirm_selection(selected)) button.pack(anchor=tk.CENTER) else: messagebox.showerror('RS-BTWATTCH2', 'Device not found') sys.exit(0)
def scan_toys(timeout: float = 5.0): return asyncio.run(bleak.discover(timeout))
async def ble_discover(loop, time): task1 = loop.create_task(discover(time)) await asyncio.wait([task1]) return task1
while True: Service_uuid = input("Select Service above: ") if Service_uuid in services.services.keys(): return services.services[Service_uuid] async def InputCharacteristic(characteristics): for cur_char in characteristics: print("[Characteristic] {0} {1}".format(cur_char.uuid, cur_char.description)) while True: Char_uuid = input("Select Characteristic above: ") for char in characteristics: if Char_uuid == char.uuid: return char # loop = asyncio.get_event_loop() # loop.run_until_complete(run(address, loop)) devices = asyncio.run(discover()) for dev in devices: print(dev) client1 = asyncio.run(run(address1, MODEL_NBR_UUID1)) client2 = asyncio.run(run(address2, MODEL_NBR_UUID2)) service1 = asyncio.run(InputService(client1.services)) service2 = asyncio.run(InputService(client2.services)) characteristic1 = asyncio.run(InputCharacteristic(service1.characteristics)) characteristic2 = asyncio.run(InputCharacteristic(service2.characteristics))
def discover_btwattch2(): ble_devices = asyncio.get_event_loop().run_until_complete(discover()) return [d for d in ble_devices if 'BTWATTCH2' in d.name]
def okButtonClicked(self): devices = asyncio.run(discover(timeout=2.0)) for d in devices: print(d) print('okButtonClicked')