Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created August 7, 2017 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuc-arc-f/b5cd0e6f8f6901922571654d6d33160a to your computer and use it in GitHub Desktop.
Save kuc-arc-f/b5cd0e6f8f6901922571654d6d33160a to your computer and use it in GitHub Desktop.
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