bosmoment / PineTime-apps

Firmware for the PineTime based on RIOT, NimBLE and LittleVGL
GNU Lesser General Public License v2.1
79 stars 32 forks source link

stop advertising when BT connected #10

Closed danielkucera closed 4 years ago

danielkucera commented 4 years ago

to save power.

bergzand commented 4 years ago

I was under the impression that NimBLE automatically stops advertising as soon as it is connected to a host. Is this assumption incorrect?

danielkucera commented 4 years ago

@bergzand , no, it does not stop. You can try connecting with Gadgetbridge and scan afterwards with https://play.google.com/store/apps/details?id=uk.co.alt236.btlescan and the same after applying my patch

bergzand commented 4 years ago

Without the patch, if I connect the pinetime with my laptop, it does not show up in Gadgetbridge or in nRF connect app. As soon as I disconnect it pops up almost immediately.

bergzand commented 4 years ago

I'll try to find confirmation of this in the NimBLE source code.

bergzand commented 4 years ago

With the following patch applied:

diff --git a/modules/bleman/bleman.c b/modules/bleman/bleman.c
index 3ff7318..39ec2f9 100644
--- a/modules/bleman/bleman.c
+++ b/modules/bleman/bleman.c
@@ -123,6 +123,10 @@ static int _gap_event_cb(struct ble_gap_event *event, void *arg)
                 return 0;
             }
             else {
+                int res = ble_gap_adv_stop();
+                if (res != 0) {
+                    LOG_WARNING("[bleman]: unable to stop advertisements: %d\n", res);
+                }
                 bleman->conn_handle = event->connect.conn_handle;
                 bleman->state = BLEMAN_BLE_STATE_CONNECTED;

When connecting it logs that it is unable to stop advertisements with result code 2 (BLE_HS_EALREADY) meaning that advertisements are already disabled.

danielkucera commented 4 years ago

You are right. I don't know how I was testing this.