/* * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #define __BTSTACK_FILE__ "gap_inquiry.c" // ***************************************************************************** /* EXAMPLE_START(gap_inquiry): GAP Inquiry Example * * @text The Generic Access Profile (GAP) defines how Bluetooth devices discover * and establish a connection with each other. In this example, the application * discovers surrounding Bluetooth devices and collects their Class of Device * (CoD), page scan mode, clock offset, and RSSI. After that, the remote name of * each device is requested. In the following section we outline the Bluetooth * logic part, i.e., how the packet handler handles the asynchronous events and * data packets. */ // ***************************************************************************** #include #include #include #include #include "btstack.h" #define MAX_DEVICES 20 enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED }; struct device { bd_addr_t address; uint8_t pageScanRepetitionMode; uint16_t clockOffset; enum DEVICE_STATE state; }; #define INQUIRY_INTERVAL 5 struct device devices[MAX_DEVICES]; int deviceCount = 0; enum STATE {INIT, ACTIVE} ; enum STATE state = INIT; static btstack_packet_callback_registration_t hci_event_callback_registration; static int getDeviceIndexForAddress( bd_addr_t addr){ int j; for (j=0; j< deviceCount; j++){ if (bd_addr_cmp(addr, devices[j].address) == 0){ return j; } } return -1; } static void start_scan(void){ printf("Starting inquiry scan..\n"); gap_inquiry_start(INQUIRY_INTERVAL); } static int has_more_remote_name_requests(void){ int i; for (i=0;i= MAX_DEVICES) break; // already full gap_event_inquiry_result_get_bd_addr(packet, addr); index = getDeviceIndexForAddress(addr); if (index >= 0) break; // already in our list memcpy(devices[deviceCount].address, addr, 6); devices[deviceCount].pageScanRepetitionMode = gap_event_inquiry_result_get_page_scan_repetition_mode(packet); devices[deviceCount].clockOffset = gap_event_inquiry_result_get_clock_offset(packet); // print info printf("Device found: %s ", bd_addr_to_str(addr)); printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(packet)); printf("pageScan %d, ", devices[deviceCount].pageScanRepetitionMode); printf("clock offset 0x%04x",devices[deviceCount].clockOffset); if (gap_event_inquiry_result_get_rssi_available(packet)){ printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(packet)); } if (gap_event_inquiry_result_get_name_available(packet)){ char name_buffer[240]; int name_len = gap_event_inquiry_result_get_name_len(packet); memcpy(name_buffer, gap_event_inquiry_result_get_name(packet), name_len); name_buffer[name_len] = 0; printf(", name '%s'", name_buffer); devices[deviceCount].state = REMOTE_NAME_FETCHED;; } else { devices[deviceCount].state = REMOTE_NAME_REQUEST; } printf("\n"); deviceCount++; break; case GAP_EVENT_INQUIRY_COMPLETE: for (i=0;i= 0) { if (packet[2] == 0) { printf("Name: '%s'\n", &packet[9]); devices[index].state = REMOTE_NAME_FETCHED; } else { printf("Failed to get name: page timeout\n"); } } continue_remote_names(); break; default: break; } break; default: break; } } /* @text For more details on discovering remote devices, please see * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices). */ /* @section Main Application Setup * * @text Listing MainConfiguration shows main application code. * It registers the HCI packet handler and starts the Bluetooth stack. */ /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */ int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]) { (void)argc; (void)argv; hci_event_callback_registration.callback = &packet_handler; hci_add_event_handler(&hci_event_callback_registration); // enabled EIR hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR); // turn on! hci_power_control(HCI_POWER_ON); return 0; } /* LISTING_END */ /* EXAMPLE_END */