1. import the ble module below
2. on you phone (iphone) settings > bluetooth and connect PYBD device (may be advertised as ESP32)
3. On the iphone, open the Health app.
4. Send a new value to the iphone HEART RATE: ble.send(88)
If anybody else has any useful code snippets for BLE, would be interested to see / test it !
ble.py
Code: Select all
import bluetooth
bt = bluetooth.BLE()
bt.active(True)
UUID_HR = const(0x2A37)
_IRQ_ALL = const(0xffff)
HR_SERVICE = bluetooth.UUID(0x180D)
HR_CHAR = (bluetooth.UUID(UUID_HR), bluetooth.FLAG_READ|bluetooth.FLAG_NOTIFY,)
CID = None
((HR,),) = bt.gatts_register_services(((HR_SERVICE, (HR_CHAR,),),))
def bt_irq(event, data):
# >> bt irq 1 (64, 1, b'Y\xeb\n@uZ')
global CID
CID,EID,DATA = data
print('connected > evt:{} id:{} eid:{} data:{}'.format(event,CID,EID,DATA))
bt.irq(bt_irq, _IRQ_ALL)
def adv_encode(adv_type, value):
return bytes((len(value) + 1, adv_type,)) + value
def adv_encode_name(name):
return adv_encode(0x09, name.encode())
def adv():
bt.gap_advertise(100, adv_encode(0x01, b'\x06') + adv_encode(0x03, b'\x0d\x18') + adv_encode(0x19, b'\xc1\x03') + adv_encode_name('esp32hr'))
def send(val=0):
# b'\x00\x42'
ba = bytearray(2)
ba[1]=val
bt.gatts_notify(64, HR, ba)
adv()