Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to decode advertisement with service data for each UUID separately #7

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions TheengsExplorer/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@


def decode(advertisement_data):
"""Decode advertisement data with Theengs Decoder."""
data_json = {}

if advertisement_data.service_data:
dstr = list(advertisement_data.service_data.keys())[0]
# TheengsDecoder only accepts 16 bit UUIDs, this converts the 128 bit UUID to 16 bit.
data_json["servicedatauuid"] = dstr[4:8]
dstr = str(list(advertisement_data.service_data.values())[0].hex())
data_json["servicedata"] = dstr
if advertisement_data.local_name:
data_json["name"] = advertisement_data.local_name

if advertisement_data.manufacturer_data:
dstr = str(
Expand All @@ -23,13 +20,25 @@ def decode(advertisement_data):
dstr += str(list(advertisement_data.manufacturer_data.values())[0].hex())
data_json["manufacturerdata"] = dstr

if advertisement_data.local_name:
data_json["name"] = advertisement_data.local_name
if advertisement_data.service_data:
# Try to decode advertisement with service data for each UUID separately.
for uuid, data in advertisement_data.service_data.items():
# TheengsDecoder only accepts 16 bit UUIDs, this converts the 128 bit UUID to 16 bit.
data_json["servicedatauuid"] = uuid[4:8]
data_json["servicedata"] = data.hex()
try:
# Return the first one that decodes correctly.
return json.loads(decodeBLE(json.dumps(data_json)))
except TypeError:
pass # Just try the following UUID if Theengs Decoder can't decode the data.

# No UUID resulted in successful decoding.
return {}

if data_json:
try:
data_json = json.loads(decodeBLE(json.dumps(data_json)))
except TypeError: # Theengs Decoder can't decode the data
except TypeError: # Theengs Decoder can't decode the data.
data_json = {}

return data_json