static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ if (packet_type != HCI_EVENT_PACKET) return; switch (hci_event_packet_get_type(packet)) { case BTSTACK_EVENT_STATE: // BTstack activated, get started if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ printf("Start scaning!\n"); gap_set_scan_parameters(0,0x0030, 0x0030); gap_start_scan(); } break; case GAP_EVENT_ADVERTISING_REPORT:{ bd_addr_t address; gap_event_advertising_report_get_address(packet, address); uint8_t event_type = gap_event_advertising_report_get_advertising_event_type(packet); uint8_t address_type = gap_event_advertising_report_get_address_type(packet); int8_t rssi = gap_event_advertising_report_get_rssi(packet); uint8_t length = gap_event_advertising_report_get_data_length(packet); const uint8_t * data = gap_event_advertising_report_get_data(packet); printf("Advertisement event: evt-type %u, addr-type %u, addr %s, rssi %d, data[%u] ", event_type, address_type, bd_addr_to_str(address), rssi, length); printf_hexdump(data, length); dump_advertisement_data(data, length); break; } default: break; } }
// Either connect to remote specified on command line or start scan for device with "LE Streamer" in advertisement static void le_streamer_client_start(void){ if (cmdline_addr_found){ printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); state = TC_W4_CONNECT; gap_connect(cmdline_addr, 0); } else { printf("Start scanning!\n"); state = TC_W4_SCAN_RESULT; gap_set_scan_parameters(0,0x0030, 0x0030); gap_start_scan(); } }
/* LISTING_START(GATTBrowserHCIPacketHandler): Connecting and disconnecting from the GATT client */ static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ UNUSED(channel); UNUSED(size); if (packet_type != HCI_EVENT_PACKET) return; advertising_report_t report; uint8_t event = hci_event_packet_get_type(packet); switch (event) { case BTSTACK_EVENT_STATE: // BTstack activated, get started if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; if (cmdline_addr_found){ printf("Trying to connect to %s\n", bd_addr_to_str(cmdline_addr)); gap_connect(cmdline_addr, 0); break; } printf("BTstack activated, start scanning!\n"); gap_set_scan_parameters(0,0x0030, 0x0030); gap_start_scan(); break; case GAP_EVENT_ADVERTISING_REPORT: fill_advertising_report_from_packet(&report, packet); dump_advertising_report(&report); // stop scanning, and connect to the device gap_stop_scan(); gap_connect(report.address,report.address_type); break; case HCI_EVENT_LE_META: // wait for connection complete if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; connection_handler = hci_subevent_le_connection_complete_get_connection_handle(packet); // query primary services gatt_client_discover_primary_services(handle_gatt_client_event, connection_handler); break; case HCI_EVENT_DISCONNECTION_COMPLETE: printf("\nGATT browser - DISCONNECTED\n"); break; default: break; } }
static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ if (packet_type != HCI_EVENT_PACKET) return; uint8_t event = hci_event_packet_get_type(packet); switch (event) { case BTSTACK_EVENT_STATE: // BTstack activated, get started if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; if (cmdline_addr_found){ printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); state = TC_W4_CONNECT; gap_connect(cmdline_addr, 0); break; } printf("Start scanning!\n"); state = TC_W4_SCAN_RESULT; gap_set_scan_parameters(0,0x0030, 0x0030); gap_start_scan(); break; case GAP_EVENT_ADVERTISING_REPORT: if (state != TC_W4_SCAN_RESULT) return; fill_advertising_report_from_packet(&report, packet); if (blacklist_contains(report.address)) { break; } dump_advertising_report(&report); // stop scanning, and connect to the device state = TC_W4_CONNECT; gap_stop_scan(); printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(report.address)); gap_connect(report.address,report.address_type); break; case HCI_EVENT_LE_META: // wait for connection complete if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; if (state != TC_W4_CONNECT) return; connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); // initialize gatt client context with handle, and add it to the list of active clients // query primary services printf("\nSearch for battery service.\n"); state = TC_W4_SERVICE_RESULT; gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, battery_service_uuid); break; case HCI_EVENT_DISCONNECTION_COMPLETE: if (cmdline_addr_found){ printf("\nDisconnected %s\n", bd_addr_to_str(cmdline_addr)); exit(0); } printf("\nDisconnected %s\n", bd_addr_to_str(report.address)); printf("Restart scan.\n"); state = TC_W4_SCAN_RESULT; gap_start_scan(); break; default: break; } }