用tkinter制作蓝牙扫描接口?



我使用Python蓝牙模块与Tkinter创建一个应用程序,用户可以选择连接到蓝牙。谁能帮我用Tkinter在树莓派上做一个蓝牙设备扫描接口?在界面上以列表的形式输出扫描设备。是否可以添加connect和pair parted?

import time
from time import sleep
import serial
import bluetooth
import sys
import tkinter as tk
import tkinter.font as font 
from tkinter import *
from tkinter import  ttk
from tkinter import messagebox
from tkinter import scrolledtext
import datetime
now = datetime.datetime.now()
main_window = tk.Tk()
main_window.title('Scan for nearby Bluetooth devices')
#changes the size of the screens window
window_width = 900
window_height = 500
screen_width = main_window.winfo_screenwidth()
screen_height = main_window.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
main_window.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')

main_window.grid_columnconfigure(1, weight=1)
main_window.grid_rowconfigure(1, weight=1)

text = tk.Text(main_window, height=30, width=30)
text.grid(row=1, column=1, sticky=tk.EW)
def get_time():
return now.strftime('%H:%M:%S')
text.insert('1.0','The time of the scan: ' + str(get_time()) + 'nn')
def button_clicked():

text['state'] = 'normal'
now = datetime.datetime.now()
def get_time():
return now.strftime('%H:%M:%S')
print()
text.delete('0.0', tk.END)

print("Scanning")  
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("Found {} devices.".format(len(nearby_devices)))
for addr, name in nearby_devices:
print("  {} - {}".format(addr, name))
return 

text.insert('1.0','The time of the scan: ' + str(get_time()) + 'nn')
text.insert('0.0', 'Amount of devices found: ' + str(len(out)) + 'nn')
text['state'] = 'disabled'
main_button = ttk.Button(
main_window,
text='Scan again',
command=lambda: button_clicked()
)
main_button.grid(row=0, column=1, sticky=tk.EW)
exit_button = ttk.Button(
main_window,
text='Exit',
command=lambda: main_window.quit()
)
exit_button.grid(row=2, column=1, sticky=tk.EW)
text['state'] = 'disabled'
main_window.mainloop()

这有帮助吗?

我在Raspberry Pi 4B上使用了这个。

nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print "found target bluetooth device with address ", target_address
else:
print "could not find target bluetooth device nearby"

相关内容

  • 没有找到相关文章

最新更新