Demonstration of the GAP profile. It shows advertising, scanning and connecting. The demo will cycle through several modes and print over the serial connection information about current activity.

GAP - Advertising, Scanning, Connecting

Demonstration of GAP API usage. It shows advertising, scanning and connecting. The demo will cycle through several modes and print over the serial connection information about current activity.

Running the application

Requirements

The sample application can be seen on any BLE scanner on a smartphone. If you don't have a scanner on your phone, please install :

- nRF Master Control Panel for Android.

- LightBlue for iPhone.

Information about activity is printed over the serial connection - please have a client open. You may use:

- Tera Term

Hardware requirements are in the main readme.

Building instructions

Building instructions for all samples are in the main readme.

Revision:
10:6f1c573093c1
Parent:
1:d4bb1e33950e
Child:
11:37872bf83624
--- a/source/main.cpp	Tue Sep 04 14:30:21 2018 +0100
+++ b/source/main.cpp	Wed Sep 26 15:00:23 2018 +0100
@@ -89,10 +89,22 @@
 static const size_t ADV_PARAM_SET_MAX  =
     sizeof(advertising_params) / sizeof(GapAdvertisingParams);
 
+static const char* to_string(Gap::Phy_t phy) {
+    switch(phy.value()) {
+        case Gap::Phy_t::LE_1M:
+            return "LE 1M";
+        case Gap::Phy_t::LE_2M:
+            return "LE 2M";
+        case Gap::Phy_t::LE_CODED:
+            return "LE coded";
+        default:
+            return "invalid PHY";
+    }
+}
 
 /** Demonstrate advertising, scanning and connecting
  */
-class GAPDevice : private mbed::NonCopyable<GAPDevice>
+class GAPDevice : private mbed::NonCopyable<GAPDevice>, public Gap::EventHandler
 {
 public:
     GAPDevice() :
@@ -132,6 +144,9 @@
             makeFunctionPointer(this, &GAPDevice::on_timeout)
         );
 
+        /* handle gap events */
+        _ble.gap().setEventHandler(this);
+
         error = _ble.init(this, &GAPDevice::on_init_complete);
 
         if (error) {
@@ -162,6 +177,14 @@
         printf("Device address: %02x:%02x:%02x:%02x:%02x:%02x\r\n",
                addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
 
+        /* setup the default phy used in connection to 2M to reduce power consumption */
+        Gap::Phys_t tx_phys = { /* 1M */ false, /* 2M */ true, /* coded */ false };
+        Gap::Phys_t rx_phys = { /* 1M */ false, /* 2M */ true, /* coded */ false };
+        ble_error_t err = _ble.gap().setPreferedPhys(&tx_phys, &rx_phys);
+        if (err) {
+            printf("INFO: GAP::setPreferedPhys failed with error code %s", BLE::errorToString(err));
+        }
+
         /* all calls are serialised on the user thread through the event queue */
         _event_queue.call(this, &GAPDevice::demo_mode_start);
     };
@@ -378,6 +401,56 @@
         _event_queue.call(this, &GAPDevice::demo_mode_end);
     };
 
+    /**
+     * Implementation of Gap::EventHandler::onReadPhy
+     */
+    virtual void onReadPhy(
+        ble_error_t error,
+        Gap::Handle_t connectionHandle,
+        Gap::Phy_t txPhy,
+        Gap::Phy_t rxPhy
+    ) {
+        if (error) {
+            printf(
+                "Phy read on connection %d failed with error code %s\r\n",
+                connectionHandle,
+                BLE::errorToString(error)
+            );
+        } else {
+            printf(
+                "Phy read on connection %d - Tx Phy: %s, Rx Phy: %s\r\n",
+                connectionHandle,
+                to_string(txPhy),
+                to_string(rxPhy)
+            );
+        }
+    }
+
+    /**
+     * Implementation of Gap::EventHandler::onPhyUpdateComplete
+     */
+    virtual void onPhyUpdateComplete(
+        ble_error_t error,
+        Gap::Handle_t connectionHandle,
+        Gap::Phy_t txPhy,
+        Gap::Phy_t rxPhy
+    ) {
+        if (error) {
+            printf(
+                "Phy update on connection: %d failed with error code %s\r\n",
+                connectionHandle,
+                BLE::errorToString(error)
+            );
+        } else {
+            printf(
+                "Phy update on connection %d - Tx Phy: %s, Rx Phy: %s\r\n",
+                connectionHandle,
+                to_string(txPhy),
+                to_string(rxPhy)
+            );
+        }
+    }
+
     /** called if timeout is reached during advertising, scanning
      *  or connection initiation */
     void on_timeout(const Gap::TimeoutSource_t source)