Sandrocottus
Sandrocottus

Reputation: 533

Python code to connect to BLE module using BLED112 dongle and read/ write to GATT services-characteristics

I have a BLED112 dongle and a BLE device. My BLE device has one service and two characteristics. I need to develop a python script which uses the BLED112 to scan for nearby BLE devices and connect to my BLE device. Then i need to find the Service-Characteristics and read-write to them.

I found support for BGAPI and PYGATT in github, but they seem very complicated.

Please help me with a simple example to achieve my task.

Thanks a lot in advance.

Upvotes: 2

Views: 1407

Answers (1)

Sandrocottus
Sandrocottus

Reputation: 533

I was able to add a textbox and button to get the comport and scan for the nearby devices Following is my code:-

import pygatt
import re 
import tkinter as tk
from functools import partial

def call_result(label_result, n1):
    num1 = str(n1.get())
    label_result.config(text="Comport is %s" % num1)
    adapter = pygatt.BGAPIBackend(serial_port=str(num1))
    adapter.start()
    listOfDevices = adapter.scan()
    print (listOfDevices)
    return

root = tk.Tk()
root.geometry('800x400+100+200')
root.title('Project')
number1 = tk.StringVar()
labelTitle = tk.Label(root, text="Project").grid(row=0, column=2)
labelNum1 = tk.Label(root, text="Enter Comport of dongle").grid(row=1, column=0)
labelResult = tk.Label(root)
labelResult.grid(row=7, column=2)
entryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2)
call_result = partial(call_result, labelResult, number1)
buttonCal = tk.Button(root, text="Get ComPort", command=call_result).grid(row=3, column=0)

root.mainloop()

Upvotes: 2

Related Questions