Add reliable Android BLE ride recorder
This commit is contained in:
@@ -7,12 +7,13 @@ import argparse
|
||||
import asyncio
|
||||
import csv
|
||||
import os
|
||||
import secrets
|
||||
import signal
|
||||
from contextlib import ExitStack
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from trikke_ble import BleFrameReassembler, encode_ack
|
||||
from trikke_ble import BleFrameReassembler, encode_ack, encode_begin_session
|
||||
from trikke_protocol import (
|
||||
CSV_COLUMNS,
|
||||
PACKET_TYPE_METADATA,
|
||||
@@ -27,6 +28,7 @@ DEVICE_NAME = "TrikkeSensor"
|
||||
SERVICE_UUID = "7d2ea000-f75b-4a9b-8fbe-3d4c2a1e9c10"
|
||||
DATA_UUID = "7d2ea000-f75b-4a9b-8fbe-3d4c2a1e9c11"
|
||||
ACK_UUID = "7d2ea000-f75b-4a9b-8fbe-3d4c2a1e9c12"
|
||||
CONTROL_UUID = "7d2ea000-f75b-4a9b-8fbe-3d4c2a1e9c13"
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
@@ -86,6 +88,7 @@ async def capture(args: argparse.Namespace) -> int:
|
||||
last_persisted_raw: bytes | None = None
|
||||
sample_count = 0
|
||||
frame_count = 0
|
||||
session_token = secrets.randbits(64)
|
||||
|
||||
with ExitStack() as stack:
|
||||
raw_capture = stack.enter_context(output.open("wb"))
|
||||
@@ -110,6 +113,11 @@ async def capture(args: argparse.Namespace) -> int:
|
||||
reassembler.reset()
|
||||
while not fragments.empty():
|
||||
fragments.get_nowait()
|
||||
await client.write_gatt_char(
|
||||
CONTROL_UUID,
|
||||
encode_begin_session(session_token),
|
||||
response=True,
|
||||
)
|
||||
await client.start_notify(DATA_UUID, on_fragment)
|
||||
print("BLE connected and subscribed")
|
||||
while not stop.is_set() and client.is_connected:
|
||||
|
||||
@@ -6,6 +6,7 @@ import struct
|
||||
|
||||
BLE_FRAGMENT_HEADER = struct.Struct("<IHH")
|
||||
BLE_ACK = struct.Struct("<4sI")
|
||||
BLE_BEGIN_SESSION = struct.Struct("<4sQ")
|
||||
BLE_MIN_FRAME_SIZE = 36
|
||||
BLE_MAX_FRAME_SIZE = 196
|
||||
|
||||
@@ -64,3 +65,7 @@ class BleFrameReassembler:
|
||||
|
||||
def encode_ack(packet_sequence: int) -> bytes:
|
||||
return BLE_ACK.pack(b"ACK1", packet_sequence & 0xFFFFFFFF)
|
||||
|
||||
|
||||
def encode_begin_session(session_token: int) -> bytes:
|
||||
return BLE_BEGIN_SESSION.pack(b"BGN1", session_token & 0xFFFFFFFFFFFFFFFF)
|
||||
|
||||
Reference in New Issue
Block a user