gpt4 book ai didi

使用 C 语言编写 Windows 10 OBEX 客户端

转载 作者:行者123 更新时间:2023-11-30 19:32:07 25 4
gpt4 key购买 nike

我正在制作一个可通过蓝牙进行配置的嵌入式设备。我决定 OBEX 是可行的方法(可以使用 FTP 通过以太网配置类似的设备,我想使用相同的方法)。蓝牙连接的嵌入端就完成了。我可以将手机中的照片上传到嵌入式系统,这样就可以了。

然后是创建用于配置设备的 Windows 应用程序的任务。我从来没有觉得有必要跟上 Windows 应用程序的发展,所以我可能最终会制作一个 MFC 应用程序,但对于初学者来说,我会满足于更少:控制台应用程序。

我发现很难找到支持蓝牙功能的 Windows C 代码,但我确实遇到了 OpenObex,这是 SourceForge 上的一个项目,虽然不是很活跃(最后一次发布是在 18 个月前),但看起来很有前途。我下载了代码,阅读了构建说明并完全忽略了它们(不喜欢使用 cmake),而是创建了一个 Visual C++ 2010 项目,其中包含库的所有源代码和一个测试应用程序。

您想要使用此应用程序做的第一件事就是连接。在那里,我遇到了需要深入了解蓝牙知识的行为,但我似乎无法在谷歌上一起搜索。

对 connect() 的调用使 GetLastError() 返回 0x2750,我发现这意味着“主机关闭”。 Google 告诉我,在 Windows/蓝牙的情况下,这意味着主机处于“断开连接模式”,我认为这也从我制作的日志中得出。

首先:嵌入式系统上的蓝牙告诉我这一点(我自己添加了一些日志记录,以防万一你去查找):

bluetoothd[195]: src/adapter.c(7795):connected_callback() hci0 device 24:FD:52:14:C2:6B connected eir_len 12
bluetoothd[195]: src/adapter.c(808):btd_adapter_find_device() Trying to find device
bluetoothd[195]: src/device.c(4116):device_addr_type_cmp() Comparing bf:9f:27:e2:87:d0 to 6b:c2:14:52:fd:24
bluetoothd[195]: src/device.c(3736):device_create() dst 24:FD:52:14:C2:6B
bluetoothd[195]: src/device.c(3646):device_new() address 24:FD:52:14:C2:6B
bluetoothd[195]: src/device.c(3682):device_new() Creating device /org/bluez/hci0/dev_24_FD_52_14_C2_6B
bluetoothd[195]: src/adapter.c(1148):adapter_create_device() g_slist_append device 0x1228730
bluetoothd[195]: src/adapter.c(7805):connected_callback() continuing connected callback
bluetoothd[195]: src/adapter.c(7810):connected_callback() going to eir_parse
bluetoothd[195]: src/adapter.c(7816):connected_callback() set class
bluetoothd[195]: src/device.c(3821):device_set_class() /org/bluez/hci0/dev_24_FD_52_14_C2_6B 0x02010C
bluetoothd[195]: src/adapter.c(7821):connected_callback() Going to add connection
bluetoothd[195]: src/adapter.c(3960):adapter_add_connection() g_slist_append device 0x1228730
bluetoothd[195]: src/device.c(3787):btd_device_device_set_name() /org/bluez/hci0/dev_24_FD_52_14_C2_6B HCK06

此后,Windows 应用程序大约需要一秒钟才决定“连接失败”,之后,bluetoothd 告诉我:

bluetoothd[195]: src/adapter.c(7033):new_link_key_callback() hci0 new key for 24:FD:52:14:C2:6B type 4 pin_len 0 store_hint 0
bluetoothd[195]: src/adapter.c(808):btd_adapter_find_device() Trying to find device
bluetoothd[195]: src/device.c(4116):device_addr_type_cmp() Comparing bf:9f:27:e2:87:d0 to 6b:c2:14:52:fd:24
bluetoothd[195]: src/device.c(4116):device_addr_type_cmp() Comparing 6b:c2:14:52:fd:24 to 6b:c2:14:52:fd:24
bluetoothd[195]: src/adapter.c(1228):btd_adapter_get_device() Found device 0x1228730
bluetoothd[195]: src/adapter.c(808):btd_adapter_find_device() Trying to find device
bluetoothd[195]: src/device.c(4116):device_addr_type_cmp() Comparing bf:9f:27:e2:87:d0 to 6b:c2:14:52:fd:24
bluetoothd[195]: src/device.c(4116):device_addr_type_cmp() Comparing 6b:c2:14:52:fd:24 to 6b:c2:14:52:fd:24
bluetoothd[195]: src/adapter.c(1228):btd_adapter_get_device() Found device 0x1228730
bluetoothd[195]: src/device.c(5485):device_bonding_complete() bonding (nil) status 0x00
bluetoothd[195]: src/device.c(5542):device_bonding_complete() setting timer for reverse service discovery
bluetoothd[195]: src/adapter.c(1635):resume_discovery()

bluetoothd[195]: src/device.c(4286):device_probe_profiles() Probing profiles for device 24:FD:52:14:C2:6B
bluetoothd[195]: profiles/network/connection.c:connection_register() /org/bluez/hci0/dev_24_FD_52_14_C2_6B id 4373
bluetoothd[195]: profiles/network/connection.c:create_peer() Registered interface org.bluez.Network1 on path /org/bluez/hci0/dev_24_FD_52_14_C2_6B
bluetoothd[195]: src/service.c(117):btd_service_ref() 0x123f160: ref=2
bluetoothd[195]: profiles/network/connection.c:connection_register() id 4373 registered
bluetoothd[195]: src/service.c(104):change_state() 0x123f160: device 24:FD:52:14:C2:6B profile network-panu state changed: unavailable -> disconnected (0)
bluetoothd[195]: src/device.c(2284):device_svc_resolved() /org/bluez/hci0/dev_24_FD_52_14_C2_6B err 0

“断开连接模式”可能是嵌入式设备发送消息的直接结果,因为 hcidump 告诉我,除其他外,以下内容:

< ACL data: handle 12 flags 0x00 dlen 8
L2CAP(d): cid 0x0040 len 4 [psm 3]
RFCOMM(s): *DM*: cr 1 dlci 8 pf 1 ilen 0 fcs 0xe3

在时间上,这与 Windows 客户端决定“连接失败”同时发生。

很多文字(抱歉!:)),但我真的很感激任何可以帮助我进一步在 Windows 上从 C 进行 OBEX 的帮助。

最佳答案

事实证明问题出在客户端的连接方式上。连接到服务可以通过两种方式完成:通过端口号或通过服务类 ID。 OpenObex 假定在默认为 4 的端口上找到该服务,但可以从命令行进行设置。为了使其正常工作,您需要自己进行设备的服务发现。更好的方法是使用服务类 ID,这将使服务器根据为 OBEXFileTransfer 保留的 GUID 确定端口 (00001106-0000-1000-8000-00805f9b34fb)

我更改了 OpenObex 的代码来执行此操作,并且对 connect() 的调用现在成功返回。此后我遇到了新问题,但至少连接现在可以工作了!

关于使用 C 语言编写 Windows 10 OBEX 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47479279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com