version 3 final working real BT
This commit is contained in:
Binary file not shown.
@@ -160,22 +160,54 @@ class RealWristband(BaseWristband):
|
||||
)
|
||||
|
||||
try:
|
||||
self.client = BleakClient(self.ble_address)
|
||||
self.client = BleakClient(self.ble_address, timeout=20.0)
|
||||
await self.client.connect()
|
||||
|
||||
if not self.client.is_connected:
|
||||
raise Exception("Connection failed")
|
||||
|
||||
print(f"✓ Connected to {self.band_id}")
|
||||
|
||||
# Subscribe to notifications
|
||||
await self.client.start_notify(CHAR_UUID, self._notification_handler)
|
||||
print(f"✓ Subscribed to notifications from {self.band_id}")
|
||||
|
||||
# CRITICAL: Keep connection alive!
|
||||
print(f" Monitoring {self.band_id}... (will stay connected)")
|
||||
while self.client.is_connected and self.status == WristbandStatus.IN_USE:
|
||||
await asyncio.sleep(1.0) # Keep alive loop
|
||||
|
||||
print(f" Connection to {self.band_id} closed")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to connect to {self.band_id}: {e}")
|
||||
print(f"❌ Error with {self.band_id}: {e}")
|
||||
print(f" Type: {type(e).__name__}")
|
||||
print(f" Details: {str(e)}")
|
||||
self.status = WristbandStatus.MAINTENANCE
|
||||
|
||||
finally:
|
||||
# Clean disconnect
|
||||
if self.client and self.client.is_connected:
|
||||
try:
|
||||
await self.client.stop_notify(CHAR_UUID)
|
||||
await self.client.disconnect()
|
||||
print(f" Disconnected from {self.band_id}")
|
||||
except:
|
||||
pass
|
||||
|
||||
def _notification_handler(self, sender, data: bytearray):
|
||||
decoded = self.decoder.decode(bytes(data))
|
||||
if decoded:
|
||||
self.last_packet = decoded
|
||||
self.packet_count += 1
|
||||
|
||||
# Print to console (for debugging)
|
||||
print(
|
||||
f"🔵 [{self.band_id}] Packet #{self.packet_count}: "
|
||||
f"HR={decoded['hr_bpm']} SpO2={decoded['spo2']}% "
|
||||
f"Temp={decoded['temp_c']:.1f}°C"
|
||||
)
|
||||
|
||||
asyncio.create_task(self._send_to_backend(decoded))
|
||||
|
||||
async def _send_to_backend(self, decoded: dict):
|
||||
|
||||
Reference in New Issue
Block a user