Reputation: 1
I try to get nmea data from bluetooth gps receiver. But can't decode received data. This is my code;
import bluetooth
# Set the MAC address of the Bluetooth device you want to connect to
target_address = 'XX:XX:XX:XX:XX:XX'
# Search for nearby Bluetooth devices
nearby_devices = bluetooth.discover_devices()
print(nearby_devices)
# Loop through the nearby devices and try to connect to the target device
for address in nearby_devices:
if address == target_address:
# Try to connect to the target device
print(f"Connecting to {target_address}...")
try:
# Create a Bluetooth socket and connect to the target device
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((target_address, 1))
# Receive some data from the target device
try:
while True:
data = sock.recv(1024)
if not data:
break
try:
print("Received", data)
except Exception as e:
print(e)
except OSError:
print(OSError)
except Exception as e:
# Failed to connect to the target device
print(f"Failed to connect to {target_address}")
print(e)
pass
Example datas;
Received b'\xaaD\x12\x1c\xaf\x00\x00\x02,\x00\x00\x00Z\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc1\x10\xee~\xaaD\x12\x1c*\x00\x00\x02H\x00\x00\x00b\x14\x00\x00\x00\x00\x00\x00*5\x00\x00,5\x12\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x112\xe0U\xaaD\x12\x1c\xae\x00\x00\x02\x1c\x00\x00\x00b\x14\x00\x00\x00\x00\x00\x00'
Received b'*5\x00\x00d\x00\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07g@r'
Received b'\xaaD\x12\x1c\xaf\x00\x00\x02,\x00\x00\x00Z\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc1\x10\xee~\xaaD\x12\x1c*\x00\x00\x02H\x00\x00\x00b\x14\x00\x00\x00\x00\x00\x00\x129\x00\x00\x149\x12\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1e\xfe\xdax\xaaD\x12\x1c\xae\x00\x00\x02\x1c\x00\x00\x00b\x14\x00\x00\x00\x00\x00\x00'
When try decode with utf-8;
['DC:0D:30:00:22:61']
Connecting to DC:0D:30:00:22:61...
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xe4 in position 0: invalid continuation byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xcb in position 0: invalid continuation byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xb4 in position 0: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0x9c in position 0: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0x83 in position 0: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xd2 in position 39: unexpected end of data
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0x8e in position 39: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0x9a in position 36: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0x96 in position 37: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
Received
R♥d↕↑Sз
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xf3 in position 0: invalid continuation byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xdc in position 0: invalid continuation byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xab in position 0: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0x94 in position 0: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0x8e in position 36: invalid start byte
'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte
'utf-8' codec can't decode byte 0xa5 in position 37: invalid start byte
When try decode with ascii;
['DC:0D:30:00:22:61', 'B8:8E:82:F8:99:BF']
Connecting to DC:0D:30:00:22:61...
'ascii' codec can't decode byte 0xaa in position 0: ordinal not in range(128)
'ascii' codec can't decode byte 0x9c in position 0: ordinal not in range(128)
'ascii' codec can't decode byte 0xaa in position 0: ordinal not in range(128)
'ascii' codec can't decode byte 0x84 in position 0: ordinal not in range(128)
'ascii' codec can't decode byte 0xaa in position 0: ordinal not in range(128)
'ascii' codec can't decode byte 0xe7 in position 37: ordinal not in range(128)
Normally data should look like this : $GPGGA,210230,3855.4487,N,09446.0071,W,1,07,1.1,370.5,M,-29.5,M,,*7A
What do i have to decode it?
Upvotes: 0
Views: 73