Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
python, BLE GATT client test ( central device/nanoPI)
# -*- coding: utf-8 -*-
from bluepy import btle
from bluepy.btle import BTLEException
import time
import datetime
import sys
import traceback
LIGHT_MAC_ADDR="00:00:00:00:00:00"
mCHAR_UUID = 0xFF01
#
def get2byteUUID(sUUID):
sRet=""
if(len(sUUID) >= 8):
sRet= "0x"+ sUUID[4:8]
return sRet
#
def getGATT_char(cRead ):
ret=""
for temp in cRead:
#print(temp )
ret +=temp
return ret
def mainLoop():
# connect
tPeripheral = btle.Peripheral(deviceAddr=LIGHT_MAC_ADDR)
# find-characteristic
tCharList = tPeripheral.getCharacteristics()
iLen = len(tCharList)
# print(iLen )
for tChar in tCharList:
# print tChar
if (tChar.properties==26):
#print tChar.properties
sUUID=get2byteUUID( str(tChar.uuid) )
iUUID =int(sUUID ,16 )
if(iUUID== mCHAR_UUID ):
print "#uuid="+ str(tChar.uuid )
cRead = tChar.read()
print("rdLen=" + str(len(cRead )))
sRead = getGATT_char(cRead )
print("sRead="+ sRead)
if __name__ == '__main__':
from datetime import datetime
while True:
sTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print("time=" +sTime)
mainLoop()
time.sleep(3.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment