Ensure your device works with this simple test.
examples/ble_file_transfer_simpletest.py1# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries 2# SPDX-License-Identifier: MIT 3 4""" 5Used with ble_uart_echo_test.py. Transmits "echo" to the UARTService and receives it back. 6""" 7 8import binascii 9import random 10import time 11 12from adafruit_ble import BLERadio 13from adafruit_ble.advertising.standard import ( 14 Advertisement, 15 ProvideServicesAdvertisement, 16) 17 18import adafruit_ble_file_transfer 19 20 21def _write(client, filename, contents, *, offset=0): 22 start = time.monotonic() 23 try: 24 client.write(filename, contents, offset=offset) 25 duration = time.monotonic() - start 26 client = wait_for_reconnect() 27 except RuntimeError: 28 print("write failed. is usb connected?") 29 return client 30 print("wrote", filename, "at rate", len(contents) / duration, "B/s") 31 return client 32 33 34def _read(client, filename, *, offset=0): 35 start = time.monotonic() 36 try: 37 contents = client.read(filename, offset=offset) 38 duration = time.monotonic() - start 39 except ValueError: 40 print("missing file:", filename) 41 return b"" 42 print("read", filename, "at rate", len(contents) / duration, "B/s") 43 return contents 44 45 46ble = BLERadio() 47 48peer_address = None 49 50 51def wait_for_reconnect(): 52 print("reconnecting", end="") 53 while ble.connected: 54 pass 55 print(".", end="") 56 new_connection = ble.connect(peer_address) 57 print(".", end="") 58 if not new_connection.paired: 59 print(".", end="") 60 new_connection.pair() 61 new_service = new_connection[adafruit_ble_file_transfer.FileTransferService] 62 new_client = adafruit_ble_file_transfer.FileTransferClient(new_service) 63 print(".", end="") 64 time.sleep(2) 65 print("done") 66 return new_client 67 68 69# ble._adapter.erase_bonding() 70# print("erased") 71while True: 72 try: 73 while ble.connected: 74 for connection in ble.connections: 75 if adafruit_ble_file_transfer.FileTransferService not in connection: 76 continue 77 if not connection.paired: 78 print("pairing") 79 connection.pair() 80 print("paired") 81 print() 82 service = connection[adafruit_ble_file_transfer.FileTransferService] 83 client = adafruit_ble_file_transfer.FileTransferClient(service) 84 85 print("Testing write") 86 client = _write(client, "/hello.txt", b"Hello world") 87 time.sleep(1) 88 c = _read(client, "/hello.txt") 89 print(len(c), c) 90 print() 91 92 print("Testing mkdir") 93 try: 94 client.mkdir("/world/") 95 except ValueError: 96 print("path exists or isn't valid") 97 print(client.listdir("/")) 98 print(client.listdir("/world/")) 99 print() 100 101 print("Test writing within dir") 102 client = _write(client, "/world/hi.txt", b"Hi world") 103 104 hello_world = b"Hello world" 105 client = _write(client, "/world/hello.txt", hello_world) 106 c = _read(client, "/world/hello.txt") 107 print(c) 108 print() 109 110 # Test offsets 111 print("Testing offsets") 112 hello = len(b"Hello ") 113 c = _read(client, "/world/hello.txt", offset=hello) 114 print(c) 115 116 client = _write(client, "/world/hello.txt", b"offsets!", offset=hello) 117 c = _read(client, "/world/hello.txt", offset=0) 118 print(c) 119 print() 120 121 # Test deleting 122 print("Testing delete in /world/") 123 print(client.listdir("/world/")) 124 try: 125 client.delete("/world/hello.txt") 126 except ValueError: 127 print("delete failed") 128 129 try: 130 client.delete("/world/") 131 print("deleted /world/") 132 except ValueError: 133 print("delete failed") 134 print(client.listdir("/world/")) 135 try: 136 client.delete("/world/hi.txt") 137 except ValueError: 138 pass 139 try: 140 client.delete("/world/") 141 except ValueError: 142 pass 143 print() 144 145 # Test move 146 print("Testing move") 147 print(client.listdir("/")) 148 try: 149 client.move("/hello.txt", "/world/hi.txt") 150 except ValueError: 151 pass 152 try: 153 client.move("/hello.txt", "/hi.txt") 154 except ValueError: 155 print("move failed") 156 print(client.listdir("/")) 157 client.delete("/hi.txt") 158 print() 159 160 # Test larger files 161 print("Testing larger files") 162 large_1k = bytearray(1024) 163 for i, _ in enumerate(large_1k): 164 large_1k[i] = random.randint(0, 255) 165 client = _write(client, "/random.txt", large_1k) 166 contents = _read(client, "/random.txt") 167 if large_1k != contents: 168 print(binascii.hexlify(large_1k)) 169 print(binascii.hexlify(contents)) 170 raise RuntimeError("large contents don't match!") 171 print() 172 time.sleep(20) 173 except ConnectionError: 174 pass 175 176 print("disconnected, scanning") 177 for advertisement in ble.start_scan(ProvideServicesAdvertisement, Advertisement, timeout=1): 178 # print(advertisement.address, advertisement.address.type) 179 if ( 180 not hasattr(advertisement, "services") 181 or adafruit_ble_file_transfer.FileTransferService not in advertisement.services 182 ): 183 continue 184 ble.connect(advertisement) 185 peer_address = advertisement.address 186 print("connected to", advertisement.address) 187 break 188 ble.stop_scan()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4