提问人:CodyOnStack 提问时间:11/3/2023 更新时间:11/3/2023 访问量:87
使用 Python 和 BLE 连接两个设备时出现问题
Problem at connecting of two devices using Python and BLE
问:
我有一个在 PC-1 上运行的客户端应用程序和一个在 PC-2 上运行的服务器应用程序。 Server-App 运行良好,但是当我在启动服务器后运行客户端时。 选择要连接的 PC 后,我收到此错误,我不知道为什么。
Searching for devices....
-----------------------------------
1: Name: 56-39-A4-BA-3D-72, Address: 56:39:A4:BA:3D:72
2: Name: 4A-6F-0B-2C-8B-FF, Address: 4A:6F:0B:2C:8B:FF
3: Name: meinLappi, Address: 9C:FC:E8:73:87:EA
4: Name: SFS/BE1-OPT3001, Address: E9:4F:5E:48:6E:31
5: Name: ZL54C, Address: CD:7C:49:45:8A:97
6: close
Select a device (1-6): 3
No Device Service found
以下是客户端的代码:
import asyncio
from bleak import BleakClient, BleakScanner
import dbus
from gi.repository import GLib
import sys
from dbus.mainloop.glib import DBusGMainLoop
bus = None
mainloop = None
BLUEZ_SERVICE_NAME = 'org.bluez'
DBUS_OM_IFACE = 'org.freedesktop.DBus.ObjectManager'
DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties'
GATT_SERVICE_IFACE = 'org.bluez.GattService1'
GATT_CHRC_IFACE = 'org.bluez.GattCharacteristic1'
SVC_UUID = "12634d89-d598-4874-8e86-7d042ee07ba7"
MAC_UUID = "322e774f-c909-49c4-bd7b-48a4003a967f"
svcUUID = None
macUUID = None
def generic_error_cb(error):
print('D-Bus call failed: ' + str(error))
mainloop.quit()
def mac_cb(value):
if len(value) != 1:
print('Invalid dvice information value: ' + repr(value))
return
print('mac')
def process_chrc(chrc_path):
chrc = bus.get_object(BLUEZ_SERVICE_NAME, chrc_path)
chrc_props = chrc.GetAll(GATT_CHRC_IFACE,
dbus_interface=DBUS_PROP_IFACE)
uuid = chrc_props['UUID']
if uuid == MAC_UUID:
global macUUID
macUUID = (chrc, chrc_props)
else:
print('Unrecognized characteristic: ' + uuid)
return True
def process_di_service(service_path, chrc_paths):
service = bus.get_object(BLUEZ_SERVICE_NAME, service_path)
service_props = service.GetAll(GATT_SERVICE_IFACE,
dbus_interface=DBUS_PROP_IFACE)
uuid = service_props['UUID']
if uuid != SVC_UUID:
return False
print('Device Information Service found: ' + service_path)
# Process the characteristics.
for chrc_path in chrc_paths:
process_chrc(chrc_path)
global svcUUID
svcUUID = (service, service_props, service_path)
return True
async def find_devices():
scanner = BleakScanner()
devices = await scanner.discover()
return devices
def interfaces_removed_cb(object_path, interfaces):
if not svcUUID:
return
if object_path == svcUUID[2]:
print('Service was removed')
mainloop.quit()
async def talk_to_device(device):
# Set up the main loop.
DBusGMainLoop(set_as_default=True)
global bus
bus = dbus.SystemBus()
global mainloop
mainloop = GLib.MainLoop()
om = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, '/'), DBUS_OM_IFACE)
om.connect_to_signal('InterfacesRemoved', interfaces_removed_cb)
objects = om.GetManagedObjects()
chrcs = []
# List characteristics found
for path, interfaces in objects.items():
if GATT_CHRC_IFACE not in interfaces.keys():
continue
chrcs.append(path)
# List sevices found
for path, interfaces in objects.items():
if GATT_SERVICE_IFACE not in interfaces.keys():
continue
chrc_paths = [d for d in chrcs if d.startswith(path + "/")]
if process_di_service(path, chrc_paths):
break
if not svcUUID:
print('No Device Service found')
sys.exit(1)
async with BleakClient(device.address) as client:
await client.is_connected()
while True:
print("-----------------------------------")
print("Which information do you want?:")
print("1: Device MAC-Address")
print("2: Device name")
print("3: serial number")
print("4: ip-address")
print("else:")
print("___________________________________")
print("5: newConnection")
print("6: close")
print("-----------------------------------")
command = input("type 1,2,3,4,5 or 6: ")
print("-----------------------------------")
if command == "1":
mac = macUUID[0].ReadValue({}, reply_handler=mac_cb,
error_handler=generic_error_cb,
dbus_interface=GATT_CHRC_IFACE)
print(mac)
elif command == "2":
print("Code")
elif command == "3":
print("Code")
elif command == "4":
print("Code")
elif command == "5":
return True
elif command == "6":
return False
else:
print("Invalid command")
continue
async def main():
print("Searching for devices....")
while True:
devices = await find_devices()
if devices:
print("-----------------------------------")
for i, device in enumerate(devices):
print(f"{i+1}: Name: {device.name}, Address: {device.address}")
print(f"{len(devices)+1}: close")
device_num = input(f"Select a device (1-{len(devices)+1}): ")
if device_num.isdigit() and 1 <= int(device_num) <= len(devices):
device_num = int(device_num) - 1
device = devices[device_num]
should_continue = await talk_to_device(device)
if not should_continue:
break
elif device_num == str(len(devices)+1):
break
else:
print("Invalid input")
else:
print("No devices found")
if __name__ == "__main__":
asyncio.run(main())
我想使用 BLE 和 Python 连接两台 PC,以便在它们之间交换数据。 我的代码的许多部分都基于这个项目:
https://github.com/bluez/bluez/blob/master/test/example-gatt-server https://github.com/bluez/bluez/blob/master/test/example-gatt-client https://github.com/PunchThrough/espresso-ble
我认为 service-uuid 有问题
感谢我能得到的每一个帮助
答: 暂无答案
评论