add rssi poll example
This commit is contained in:
3 files changed
+140
No files matched your search
@@ -0,0 +1,128 @@
|
||||
/*********************************************************************
|
||||
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>
|
||||
|
||||
BLEUart bleuart; // uart over ble
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 RSSI Example");
|
||||
Serial.println("------------------------\n");
|
||||
|
||||
// Setup the BLE LED to be enabled on CONNECT
|
||||
// Note: This is actually the default behaviour, but provided
|
||||
// here in case you want to control this LED manually
|
||||
Bluefruit.autoConnLed(true);
|
||||
|
||||
// Config the peripheral connection with maximum bandwidth
|
||||
// more SRAM required by SoftDevice
|
||||
// Note: All config***() function must be called before begin()
|
||||
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
||||
|
||||
Bluefruit.begin();
|
||||
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
Bluefruit.setTxPower(4);
|
||||
Bluefruit.setName("Bluefruit52");
|
||||
Bluefruit.setConnectCallback(connect_callback);
|
||||
Bluefruit.setDisconnectCallback(disconnect_callback);
|
||||
|
||||
// Configure and Start BLE Uart Service
|
||||
bleuart.begin();
|
||||
|
||||
// Set up and start advertising
|
||||
startAdv();
|
||||
|
||||
Serial.println("Please use Adafruit's Bluefruit LE app to connect");
|
||||
}
|
||||
|
||||
void startAdv(void)
|
||||
{
|
||||
// Advertising packet
|
||||
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
||||
Bluefruit.Advertising.addTxPower();
|
||||
|
||||
// Include bleuart 128-bit uuid
|
||||
Bluefruit.Advertising.addService(bleuart);
|
||||
|
||||
// Secondary Scan Response packet (optional)
|
||||
// Since there is no room for 'Name' in Advertising packet
|
||||
Bluefruit.ScanResponse.addName();
|
||||
|
||||
/* Start Advertising
|
||||
* - Enable auto advertising if disconnected
|
||||
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
|
||||
* - Timeout for fast mode is 30 seconds
|
||||
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
|
||||
*
|
||||
* For recommended advertising interval
|
||||
* https://developer.apple.com/library/content/qa/qa1931/_index.html
|
||||
*/
|
||||
Bluefruit.Advertising.restartOnDisconnect(true);
|
||||
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
||||
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
||||
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Forward data from HW Serial to BLEUART
|
||||
while (Bluefruit.connected())
|
||||
{
|
||||
uint16_t conn_hdl = Bluefruit.connHandle();
|
||||
|
||||
// Get the reference to current connected connection
|
||||
BLEConnection* conn = Bluefruit.Gap.Connection(conn_hdl);
|
||||
|
||||
// get the RSSI value of this connection
|
||||
// monitorRssi() must be called previously (in callback)
|
||||
int8_t rssi = conn->getRssi();
|
||||
|
||||
Serial.printf("Rssi = %d", rssi);
|
||||
Serial.println();
|
||||
|
||||
// print one per second
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void connect_callback(uint16_t conn_handle)
|
||||
{
|
||||
Serial.println("Connected");
|
||||
|
||||
// Get the reference to current connection
|
||||
BLEConnection* conn = Bluefruit.Gap.Connection(conn_handle);
|
||||
|
||||
// Start monitoring rssi of this connection
|
||||
// This function should be called in connect callback
|
||||
// no parameters means we don't use rssi changed callback
|
||||
conn->monitorRssi();
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
* https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) conn_handle;
|
||||
(void) reason;
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Disconnected");
|
||||
}
|
||||
@@ -110,6 +110,17 @@ bool BLEConnection::monitorRssi(uint8_t threshold, uint8_t skip_count)
|
||||
return true;
|
||||
}
|
||||
|
||||
int8_t BLEConnection::getRssi(void)
|
||||
{
|
||||
int8_t rssi;
|
||||
uint8_t channel_idx;
|
||||
|
||||
VERIFY_STATUS(sd_ble_gap_rssi_get(_conn_hdl, &rssi, &channel_idx), 0);
|
||||
(void) channel_idx;
|
||||
|
||||
return rssi;
|
||||
}
|
||||
|
||||
void BLEConnection::stopRssi(void)
|
||||
{
|
||||
sd_ble_gap_rssi_stop(_conn_hdl);
|
||||
|
||||
@@ -78,6 +78,7 @@ class BLEConnection
|
||||
uint8_t getPeerAddr(uint8_t addr[6]);
|
||||
|
||||
bool monitorRssi(uint8_t threshold = BLE_GAP_RSSI_THRESHOLD_INVALID, uint8_t skip_count = 0);
|
||||
int8_t getRssi(void);
|
||||
void stopRssi(void);
|
||||
|
||||
bool getHvnPacket(void);
|
||||
|
||||
Reference in new issue
Block a user