3131
3232#if MICROPY_PY_BLE_NUS
3333
34+ #if BLUETOOTH_WEBBLUETOOTH_REPL
35+ #include "hal_time.h"
36+ #endif // BLUETOOTH_WEBBLUETOOTH_REPL
37+
3438static ubluepy_uuid_obj_t uuid_obj_service = {
3539 .base .type = & ubluepy_uuid_type ,
3640 .type = UBLUEPY_UUID_128_BIT ,
@@ -75,13 +79,19 @@ static ubluepy_peripheral_obj_t ble_uart_peripheral = {
7579};
7680
7781static volatile bool m_cccd_enabled ;
82+ static volatile bool m_connected ;
7883
7984ringBuffer_typedef (uint8_t , ringbuffer_t );
8085
8186static ringbuffer_t m_rx_ring_buffer ;
8287static ringbuffer_t * mp_rx_ring_buffer = & m_rx_ring_buffer ;
8388static uint8_t m_rx_ring_buffer_data [128 ];
8489
90+ static ubluepy_advertise_data_t m_adv_data_uart_service ;
91+
92+ #if BLUETOOTH_WEBBLUETOOTH_REPL
93+ static ubluepy_advertise_data_t m_adv_data_eddystone_url ;
94+ #endif // BLUETOOTH_WEBBLUETOOTH_REPL
8595
8696int mp_hal_stdin_rx_chr (void ) {
8797 while (isBufferEmpty (mp_rx_ring_buffer )) {
@@ -127,8 +137,10 @@ STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn
127137
128138 if (event_id == 16 ) { // connect event
129139 self -> conn_handle = conn_handle ;
140+ m_connected = true;
130141 } else if (event_id == 17 ) { // disconnect event
131142 self -> conn_handle = 0xFFFF ; // invalid connection handle
143+ m_connected = false;
132144 }
133145}
134146
@@ -195,15 +207,21 @@ void ble_uart_init0(void) {
195207 mp_uint_t num_services ;
196208 mp_obj_get_array (service_list , & num_services , & services );
197209
198- ubluepy_advertise_data_t adv_data = {
199- .p_services = services ,
200- .num_of_services = num_services ,
201- .p_device_name = (uint8_t * )device_name ,
202- .device_name_len = strlen (device_name )
203- };
204-
205- (void )device_name ;
206- (void )services ;
210+ m_adv_data_uart_service .p_services = services ;
211+ m_adv_data_uart_service .num_of_services = num_services ;
212+ m_adv_data_uart_service .p_device_name = (uint8_t * )device_name ;
213+ m_adv_data_uart_service .device_name_len = strlen (device_name );
214+ m_adv_data_uart_service .connectable = true;
215+
216+ #if BLUETOOTH_WEBBLUETOOTH_REPL
217+ static uint8_t eddystone_url_data [26 ] = {0x2 , 0x1 , 0x6 ,
218+ 0x3 , 0x3 , 0xaa , 0xfe ,
219+ 18 , 0x16 , 0xaa , 0xfe , 0x10 , 0xee , 0x0 , 'm' , 'i' , 'c' , 'r' , 'o' , 'p' , 'y' , 't' , 'h' , 'o' , 'n' , 0x1 };
220+ // eddystone url adv data
221+ m_adv_data_eddystone_url .p_data = eddystone_url_data ;
222+ m_adv_data_eddystone_url .data_len = sizeof (eddystone_url_data );
223+ m_adv_data_eddystone_url .connectable = false;
224+ #endif
207225
208226 m_cccd_enabled = false;
209227
@@ -213,15 +231,33 @@ void ble_uart_init0(void) {
213231 m_rx_ring_buffer .end = 0 ;
214232 m_rx_ring_buffer .elems = m_rx_ring_buffer_data ;
215233
216- adv_data . connectable = true ;
234+ m_connected = false ;
217235
218- (void )ble_drv_advertise_data (& adv_data );
236+ ble_uart_advertise ();
237+ }
219238
220- while (m_cccd_enabled != true) {
221- ;
239+ void ble_uart_advertise (void ) {
240+ #if BLUETOOTH_WEBBLUETOOTH_REPL
241+ while (!m_connected ) {
242+ (void )ble_drv_advertise_data (& m_adv_data_uart_service );
243+ mp_hal_delay_ms (500 );
244+ (void )ble_drv_advertise_data (& m_adv_data_eddystone_url );
245+ mp_hal_delay_ms (500 );
222246 }
247+
248+ ble_drv_advertise_stop ();
249+ #else
250+ (void )ble_drv_advertise_data (& m_adv_data_uart_service );
251+ #endif // BLUETOOTH_WEBBLUETOOTH_REPL
252+ }
253+
254+ bool ble_uart_connected (void ) {
255+ return (m_connected );
223256}
224257
258+ bool ble_uart_enabled (void ) {
259+ return (m_cccd_enabled );
260+ }
225261
226262#endif // MICROPY_PY_BLE_NUS
227263
0 commit comments