Advanced central scan demo

This commit is contained in:
microbuilder committed 2017-07-18 02:01:00 +09:00
1 parent ce13ba37b7
commit 8c5621a17c
1 file changed
+150
@@ -0,0 +1,150 @@
/*********************************************************************
This is an example for our nRF52 based Bluefruit LE modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <bluefruit.h>
/* For a list of EIR data types see: https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile */
/* Matching enum: cores/nRF5/SDK/components/softdevice/s132/headers/ble_gap.h */
void setup()
{
Serial.begin(115200);
Serial.println("Bluefruit52 Central ADV Scan Example");
/* Enable both peripheral and central modes */
Bluefruit.begin(true, true);
/* Set the device name */
Bluefruit.setName("Bluefruit52");
/* Set the LED interval for blinky pattern on BLUE LED */
Bluefruit.setConnLedInterval(250);
/* Start Central Scanning
* - Enable auto scan if disconnected
* - Interval = 100 ms, window = 80 ms
* - Use active scan (used to retrieve the optional scan response adv packet)
* - Start(0) = will scan forever since no timeout is given
*/
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.setInterval(160, 80); // in units of 0.625 ms
Bluefruit.Scanner.useActiveScan(true); // Request scan response data
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
Serial.println("Scanning ...");
}
void scan_callback(ble_gap_evt_adv_report_t* report)
{
uint8_t buffer[32];
memset(buffer, 0, sizeof(buffer));
/* Display the timestamp and device address */
if (report->scan_rsp)
{
Serial.printf("[SR%10d] Packet received from ", millis());
}
else
{
Serial.printf("[ADV%9d] Packet received from ", millis());
}
Serial.printBuffer(report->peer_addr.addr, 6, ':');
Serial.print("\n");
/* Raw buffer contents */
Serial.printf("%14s %d bytes\n", "PAYLOAD", report->dlen);
if (report->dlen)
{
Serial.printf("%15s", " ");
Serial.printBuffer(report->data, report->dlen, '-');
Serial.println();
}
/* RSSI value */
Serial.printf("%14s %d dBm\n", "RSSI", report->rssi);
/* Adv Type */
Serial.printf("%14s ", "ADV TYPE");
switch (report->type)
{
case BLE_GAP_ADV_TYPE_ADV_IND:
Serial.printf("Connectable undirected\n");
break;
case BLE_GAP_ADV_TYPE_ADV_DIRECT_IND:
Serial.printf("Connectable directed\n");
break;
case BLE_GAP_ADV_TYPE_ADV_SCAN_IND:
Serial.printf("Scannable undirected\n");
break;
case BLE_GAP_ADV_TYPE_ADV_NONCONN_IND:
Serial.printf("Non-connectable undirected\n");
break;
}
/* Shortened Local Name */
if(Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, buffer, sizeof(buffer)))
{
Serial.printf("%14s %s\n", "SHORT NAME", buffer);
memset(buffer, 0, sizeof(buffer));
}
/* Complete Local Name */
if(Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, buffer, sizeof(buffer)))
{
Serial.printf("%14s %s\n", "COMPLETE NAME", buffer);
memset(buffer, 0, sizeof(buffer));
}
/* TX Power Level */
if (Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_TX_POWER_LEVEL, buffer, sizeof(buffer)))
{
Serial.printf("%14s %i\n", "TX PWR LEVEL", buffer[0]);
memset(buffer, 0, sizeof(buffer));
}
/* Check for BLE UART UUID */
if ( Bluefruit.Scanner.checkReportForUuid(report, BLEUART_UUID_SERVICE) )
{
Serial.printf("%14s %s\n", "BLE UART", "UUID Found!");
}
/* Check for DIS UUID */
if ( Bluefruit.Scanner.checkReportForUuid(report, UUID16_SVC_DEVICE_INFORMATION) )
{
Serial.printf("%14s %s\n", "DIS", "UUID Found!");
}
/* Check for Manufacturer Specific Data */
uint8_t msdlen = Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, buffer, sizeof(buffer));
if (msdlen)
{
Serial.printf("%14s ", "MAN SPEC DATA");
memset(buffer, 0, sizeof(buffer));
Serial.printBuffer(buffer, msdlen, '-');
Serial.println();
}
Serial.println();
}
void loop()
{
// Toggle both LEDs every 1 second
digitalToggle(LED_RED);
delay(1000);
}