Scientia est Potentia

ㅁㄴㅇ 본문

01 HardWare/019 HW_etc.

ㅁㄴㅇ

박 현규 2022. 6. 15. 00:24

from bluepy import btle
import time

class MyDelegate(btle.DefaultDelegate):
    def __init__(self):
        btle.DefaultDelegate.__init__(self)
        # ... initialise here

    def handleNotification(self, cHandle, data):
        #print("\n- handleNotification -\n")
        print(data)
        # ... perhaps check cHandle
        # ... process 'data'

# Initialisation  -------

p = btle.Peripheral("34:AB:95:5A:10:A2")   

# Setup to turn notifications on, e.g.
svc = p.getServiceByUUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
ch_Tx = svc.getCharacteristics("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")[0]
ch_Rx = svc.getCharacteristics("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")[0]

p.setDelegate( MyDelegate())

setup_data = b" "
p.writeCharacteristic(ch_Rx.valHandle+1, setup_data)

while True:
    message = bytes(b'a')
    message1 = bytes(b'b')
    
    try:
        ch_Tx.write(message, True)
        ch_Tx.write(message1, True)
        time.sleep(5)
        
    except btle.BTLEException:
        print("btle.BTLEException");