BLE logging program with blueduino

I programmed a BLE data logging program by using python3, pexpect, and BLE CSR dongle. (based on your previous replies.)

However, frequent data loss occurs. For example, when I receive data from a commercial apps(Your android app “Bluo”), it has a fairly high sampling rate rate of 20 miliseconds. But, my program has a vely low sampling rate of 150 miliseconds. (It seems to be lost 5 times out of 6 data reception.)

I don’t know which part of the code is slow down to recive the data. (I looked for a smililar example, but I could not find the answer.) Could you tell me which part is wrong in my code? And please tell me how to solve this problem.

Thanks in advance!

Here’s my code.

import pexpect
import time
import sys

filename = "log.txt"

# BLE Setting
DEVICE = "50:F1:4A:FB:DE:4D" # My MAC ADDR

# BLE Connect
child = pexpect.spawn("gatttool -I") 
child.sendline("connect {0}".format(DEVICE))
child.expect("Connection successful", timeout=5)
print("Connected!")

# Data Logging until Press 'Ctrl+C'
print("Logging start")
orig_stdout = sys.stdout
sys.stdout = open(filename, 'w')

try:
    while True:
        child.sendline("char-read-hnd 0x0025")
        child.expect("Characteristic value/descriptor: ", timeout=10)
        child.expect("\r\n", timeout = 10)
        tmp=[]
        tmp=child.before.split()
        tmp2=""
        for i in tmp:
            tmp2+=str(unichr(int(i,16)))
        print(tmp2),
except KeyboardInterrupt:
    pass

sys.stdout.close()
sys.stdout = orig_stdout
print(" ")
print("Logging end")

I solved this problem by using notificiation handle

listening mode
char-write-req 0x0026 0100

exiting listening mode
char-write-req 0x0026 0000

Thanks for sharing your experience. The RX service is enabled “notify” handle for better data rate.