void bt_connect_callback(uint16_t conn_handle) {
bt_state = BT_STATE_CONNECTED;
cable_state = CABLE_STATE_DISCONNECTED;
+
+ BLEConnection* conn = Bluefruit.Connection(conn_handle);
+ conn->requestPHY(BLE_GAP_PHY_2MBPS);
+ conn->requestMtuExchange(512+3);
+ conn->requestDataLengthUpdate();
}
void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
Bluefruit.Security.setSecuredCallback(bt_connect_callback);
Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback);
Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete);
+ Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms
+
const ble_gap_addr_t gap_addr = Bluefruit.getAddr();
char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
// start device information service
bledis.begin();
+ SerialBT.bufferTXD(true); // enable buffering
+
+ SerialBT.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); // enable encryption for BLE serial
SerialBT.begin();
blebas.begin();
if (battery_percent >= 98) {
battery_state = BATTERY_STATE_CHARGED;
}
+
+ #if HAS_BLE
+ if ((bt_state == BT_STATE_ON) || bt_state == BT_STATE_CONNECTED) {
+ if (battery_state != BATTERY_STATE_CHARGING) {
+ blebas.write(battery_percent);
+ } else {
+ blebas.write(100);
+ }
+ }
+ #endif
#endif
if (battery_ready) {
Serial.write(byte);
} else {
SerialBT.write(byte);
+
+ // This ensures that the TX buffer is flushed after a frame is queued in serial.
+ // serial_in_frame is used to ensure that the flush only happens at the end of the frame
+ if (serial_in_frame && byte == FEND) {
+ SerialBT.flushTXD();
+ serial_in_frame = false;
+ }
+ else if (!serial_in_frame && byte == FEND) {
+ serial_in_frame = true;
+ }
}
#else
Serial.write(byte);