Skip to content

Instantly share code, notes, and snippets.

@HDR
Last active February 15, 2025 08:05
Show Gist options
  • Save HDR/02cec215db1e97349008e80a3116a189 to your computer and use it in GitHub Desktop.
Save HDR/02cec215db1e97349008e80a3116a189 to your computer and use it in GitHub Desktop.
An OBS script that displays your heart rate as reported by a Huawei smart watch in HR Data Broadcast Mode, requires bleak (pip install bleak)
#By HDR
#https://gist.github.com/HDR/02cec215db1e97349008e80a3116a189
#V1.0
import obspython as obs
import asyncio
import threading
from bleak import BleakScanner, BleakClient
source_name = "Heart Rate"
current_text = "Awaiting Connection..."
huawei_device = None
huawei_heartrate_char_uuid = "00002a37-0000-1000-8000-00805f9b34fb"
huawei_device_lock = threading.Lock()
async def handler(sender, data):
heart_rate = data[1]
global current_text
current_text = f"{heart_rate} BPM"
def update_text():
global current_text
source = obs.obs_get_source_by_name(source_name)
if source is None:
source = obs.obs_source_create("text_gdiplus", source_name, None, None)
scene = obs.obs_frontend_get_current_scene()
scene_item = obs.obs_scene_add(scene, source)
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, "text", current_text)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
obs.obs_source_release(source)
def script_description():
return "Gets your current heart rate from a Huawei smart watch and displays it on screen\n\nMade by HDR"
def script_update(settings):
global source_name
global current_text
source_name = obs.obs_data_get_string(settings, "source_name")
update_text()
def script_defaults(settings):
obs.obs_data_set_default_string(settings, "source_name", "Heart Rate")
def script_properties():
props = obs.obs_properties_create()
obs.obs_properties_add_text(props, "source_name", "Source Name", obs.OBS_TEXT_DEFAULT)
return props
def start_bluetooth():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(connect())
async def connect():
print("Scanning for a Huawei device in HR Data Broadcast mode")
devices = await BleakScanner.discover()
global huawei_device
for device in devices:
if "HUAWEI" in (device.name or ""):
with huawei_device_lock:
huawei_device = device.address
if huawei_device:
print(f"Found Huawei device address: {huawei_device}")
async with BleakClient(device) as client:
await client.start_notify(huawei_heartrate_char_uuid, handler)
while True:
await asyncio.sleep(0.5)
else:
global current_text
current_text = "Connection failed: Please enable HR Data Broadcast"
update_text()
return
def script_load(settings):
thread = threading.Thread(target=start_bluetooth)
thread.start()
obs.timer_add(update_text, 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment