55Used with ble_uart_echo_test.py. Transmits "echo" to the UARTService and receives it back.
66"""
77
8+ import binascii
89import random
910import time
1011
1112from adafruit_ble import BLERadio
12- from adafruit_ble .advertising .standard import ProvideServicesAdvertisement
13+ from adafruit_ble .advertising .standard import (
14+ ProvideServicesAdvertisement ,
15+ Advertisement ,
16+ )
1317import adafruit_ble_file_transfer
1418
1519
1620def _write (client , filename , contents , * , offset = 0 ):
21+ # pylint: disable=redefined-outer-name
1722 start = time .monotonic ()
18- client .write (filename , contents , offset = offset )
19- duration = time .monotonic () - start
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
2030 print ("wrote" , filename , "at rate" , len (contents ) / duration , "B/s" )
31+ return client
2132
2233
2334def _read (client , filename , * , offset = 0 ):
35+ # pylint: disable=redefined-outer-name
2436 start = time .monotonic ()
25- contents = client .read (filename , offset = offset )
26- duration = time .monotonic () - start
37+ try :
38+ contents = client .read (filename , offset = offset )
39+ duration = time .monotonic () - start
40+ except ValueError :
41+ print ("missing file:" , filename )
42+ return b""
2743 print ("read" , filename , "at rate" , len (contents ) / duration , "B/s" )
2844 return contents
2945
3046
3147ble = BLERadio ()
48+
49+ peer_address = None
50+
51+
52+ def wait_for_reconnect ():
53+ print ("waiting for disconnect" )
54+ while ble .connected :
55+ pass
56+ print ("reconnecting to" , peer_address )
57+ new_connection = ble .connect (peer_address )
58+ print ("reconnected" )
59+ if not new_connection .paired :
60+ print ("pairing" )
61+ new_connection .pair ()
62+ new_service = new_connection [adafruit_ble_file_transfer .FileTransferService ]
63+ new_client = adafruit_ble_file_transfer .FileTransferClient (new_service )
64+ print ("sleeping" )
65+ time .sleep (2 )
66+ return new_client
67+
68+
3269# ble._adapter.erase_bonding()
3370# print("erased")
3471while True :
3572 try :
3673 while ble .connected :
3774 for connection in ble .connections :
38- print (
39- "services" , connection ._bleio_connection .discover_remote_services ()
40- )
75+ # pylint: disable=redefined-outer-name
4176 if adafruit_ble_file_transfer .FileTransferService not in connection :
4277 continue
4378 if not connection .paired :
4479 print ("pairing" )
4580 connection .pair ()
81+ print ("paired" )
4682 service = connection [adafruit_ble_file_transfer .FileTransferService ]
4783 client = adafruit_ble_file_transfer .FileTransferClient (service )
48- _write (client , "/hello.txt" , "Hello world" .encode ("utf-8" ))
84+ client = _write (client , "/hello.txt" , "Hello world" .encode ("utf-8" ))
85+ time .sleep (1 )
4986 c = _read (client , "/hello.txt" )
5087 print (len (c ), c )
51- client .mkdir ("/world/" )
88+ try :
89+ client .mkdir ("/world/" )
90+ except ValueError :
91+ print ("path exists or isn't valid" )
5292 print (client .listdir ("/world/" ))
53- _write (client , "/world/hi.txt" , "Hi world" .encode ("utf-8" ))
93+ client = _write (client , "/world/hi.txt" , "Hi world" .encode ("utf-8" ))
5494
5595 hello_world = "Hello world" .encode ("utf-8" )
56- _write (client , "/world/hello.txt" , hello_world )
96+ client = _write (client , "/world/hello.txt" , hello_world )
5797 c = _read (client , "/world/hello.txt" )
5898 print (c )
5999
@@ -62,39 +102,60 @@ def _read(client, filename, *, offset=0):
62102 c = _read (client , "/world/hello.txt" , offset = hello )
63103 print (c )
64104
65- _write (client , "/world/hello.txt" , "offsets!" , offset = hello )
105+ client = _write (
106+ client , "/world/hello.txt" , "offsets!" .encode ("utf-8" ), offset = hello
107+ )
66108 c = _read (client , "/world/hello.txt" , offset = 0 )
67109 print (c )
68110
69111 # Test deleting
70112 print (client .listdir ("/world/" ))
71- client .delete ("/world/hello.txt" )
113+ try :
114+ client .delete ("/world/hello.txt" )
115+ except ValueError :
116+ print ("exception correctly raised" )
117+
72118 try :
73119 client .delete ("/world/" ) # should raise an exception
74120 except ValueError :
75121 print ("exception correctly raised" )
76122 print (client .listdir ("/world/" ))
77- client .delete ("/world/hi.txt" )
78- client .delete ("/world/" )
123+ try :
124+ client .delete ("/world/hi.txt" )
125+ except ValueError :
126+ print ("missing /world/hi.txt" )
127+ try :
128+ client .delete ("/world/" )
129+ except ValueError :
130+ print ("cannot delete /world/" )
79131 print (client .listdir ("/" ))
80132
81133 large_1k = bytearray (1024 )
82- for i in range ( len ( large_1k ) ):
134+ for i , _ in enumerate ( large_1k ):
83135 large_1k [i ] = random .randint (0 , 255 )
84- _write (client , "/random.txt" , large_1k )
85- time .sleep (0.1 )
136+ client = _write (client , "/random.txt" , large_1k )
86137 contents = _read (client , "/random.txt" )
87138 if large_1k != contents :
88- print ("large contents don't match!" )
139+ print (binascii .hexlify (large_1k ))
140+ print (binascii .hexlify (contents ))
141+ raise RuntimeError ("large contents don't match!" )
89142 time .sleep (20 )
90- except ConnectionError :
143+ except ConnectionError as e :
91144 pass
92145
93146 print ("disconnected, scanning" )
94- for advertisement in ble .start_scan (ProvideServicesAdvertisement , timeout = 1 ):
95- if adafruit_ble_file_transfer .FileTransferService not in advertisement .services :
147+ for advertisement in ble .start_scan (
148+ ProvideServicesAdvertisement , Advertisement , timeout = 1
149+ ):
150+ # print(advertisement.address, advertisement.address.type)
151+ if (
152+ not hasattr (advertisement , "services" )
153+ or adafruit_ble_file_transfer .FileTransferService
154+ not in advertisement .services
155+ ):
96156 continue
97157 ble .connect (advertisement )
98- print ("connected" )
158+ peer_address = advertisement .address
159+ print ("connected to" , advertisement .address )
99160 break
100161 ble .stop_scan ()
0 commit comments