-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.py
99 lines (80 loc) · 3.31 KB
/
bluetooth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import asyncio
from bleak import BleakClient
import socketio
import json
message_buffer = ''
# Socket.IO client setup
sio = socketio.AsyncClient()
# address = "94:A9:A8:18:0B:AC" # Old house
address = "94:A9:A8:18:18:22" # New House
CHARACTERISTIC_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb" # The characteristic UUID
ble_client = None
@sio.event
async def connect():
print("Connected to the server.")
@sio.event
async def disconnect():
print("Disconnected from the server.")
@sio.event
async def message(data):
"""Handle incoming messages from the WebSocket and forward them to the Bluetooth device."""
print(f"Received message from server: {data}")
if ble_client and ble_client.is_connected:
# Convert the message to bytearray before sending
await ble_client.write_gatt_char(CHARACTERISTIC_UUID, bytearray(data, "utf-8"))
print(f"Forwarded message to the device: {data}")
def is_complete_json(json_str):
try:
json.loads(json_str)
return True
except json.JSONDecodeError:
return False
async def forward_complete_message(message):
"""Parse and forward the complete message as JSON."""
try:
# Forward the parsed JSON object to the server
message_json = json.loads(message)
message_json['device_mac'] = address
message=json.dumps(message_json)
await sio.emit('message', message)
print("Forwarded complete JSON to the server.")
except Exception as e:
# Log or handle any exceptions that occur during forwarding
print(f"Error forwarding message: {e}")
def notification_handler(sender, data):
"""Callback function that is called when a notification is received."""
global message_buffer
message = data.decode('utf-8')
# Append the new fragment to the buffer
message_buffer += message
# print(message_buffer)
# Check if the buffer forms a complete JSON string
if is_complete_json(message_buffer):
print("Complete JSON message assembled, forwarding...")
asyncio.create_task(forward_complete_message(message_buffer))
message_buffer = '' # Clear the buffer after forwarding
# else:
# print("Message fragment received, waiting for more data...")
async def listen_for_messages(address):
global ble_client
ble_client = BleakClient(address)
async with ble_client as client:
if await client.is_connected():
print(f"Connected to {address}")
# Connect to the socket.io server
await sio.connect('http://192.168.1.159:3000') # Update with your server address
print("Socket.IO Client connected")
# Send "Hello World" to the device
# await client.write_gatt_char(CHARACTERISTIC_UUID, bytearray("Hello World", "utf-8"))
# print("Sent 'Hello World' to the device.")
# Subscribe to notifications from the device
await client.start_notify(CHARACTERISTIC_UUID, notification_handler)
print("Listening for messages from the device. Press Ctrl+C to stop...")
# Keep the program running
await asyncio.sleep(float('inf'))
else:
print(f"Failed to connect to {address}")
async def main():
await listen_for_messages(address)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())