From b4bbfecd093b0032fa8795369ed914854a8b706e Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 26 Jan 2021 23:41:05 +0100 Subject: [PATCH] Store the timestamp of the last Bluetooth DATA input report received on the interrupt channel This can be used detect if the connection to a Bluetooth device is lost fx if the battery runs out or it gets out of range --- BTHID.cpp | 2 ++ BTHID.h | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/BTHID.cpp b/BTHID.cpp index d0d93c2e..7fb429ad 100644 --- a/BTHID.cpp +++ b/BTHID.cpp @@ -337,6 +337,8 @@ void BTHID::ACLData(uint8_t* l2capinbuf) { } #endif if(l2capinbuf[8] == 0xA1) { // HID BT DATA (0xA0) | Report Type (Input 0x01) + lastBtDataInputIntMillis = (uint32_t)millis(); // Store the timestamp of the report + uint16_t length = ((uint16_t)l2capinbuf[5] << 8 | l2capinbuf[4]); ParseBTHIDData((uint8_t)(length - 1), &l2capinbuf[9]); // First byte will be the report ID diff --git a/BTHID.h b/BTHID.h index f493adce..dabf70f9 100644 --- a/BTHID.h +++ b/BTHID.h @@ -93,6 +93,15 @@ public: pBtd->pairWithHID(); }; + /** + * Used to get the millis() of the last Bluetooth DATA input report received on the interrupt channel. + * This can be used detect if the connection to a Bluetooth device is lost fx if the battery runs out or if it gets out of range. + * @return Timestamp in milliseconds of the last Bluetooth DATA input report received on the interrupt channel. + */ + uint32_t getLastMessageTime() { + return lastBtDataInputIntMillis; + }; + protected: /** @name BluetoothService implementation */ /** @@ -173,5 +182,7 @@ private: uint8_t interrupt_dcid[2]; // L2CAP device CID for HID_Interrupt - Always 0x0071 uint8_t sdp_dcid[2]; uint8_t l2cap_state; + + uint32_t lastBtDataInputIntMillis; // Variable used to store the millis value of the last Bluetooth DATA input report received on the interrupt channel }; #endif