在C中编码Windows 10 Obex客户端



我正在制作可以通过蓝牙配置的嵌入式设备。我认为OBEX是要走的方法(使用FTP通过以太网配置类似的设备,我想使用相同的方法)。蓝牙连接的嵌入式侧已完成。我可以将照片从手机上传到嵌入式系统,以便有效。

然后是创建用于配置设备的Windows应用程序的任务。我从来没有觉得有必要跟上Windows应用程序的演变,因此我可能最终会制作MFC应用程序,但是对于初学者,我会花费更少的时间:一个游戏机应用程序。

我发现很难找到有蓝牙的Windows的任何C代码,但是我确实遇到了OpenObex,这是一个在SourceForge上的项目,它不是很活跃(18个月前的最后一个版本),但看起来很有希望。我下载了代码,阅读构建说明并完全忽略了它们(不喜欢使用CMAKE),而是制作一个Visual C 2010项目,其中包括库的所有来源和测试应用程序。

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

调用Connect()给GetLastror()返回0x2750,我发现这意味着"主机向下"。Google告诉我,在Windows/Bluetooth的情况下,这意味着主机处于"断开连接模式",我认为这也从我制作的日志中遵循。

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

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上进行OBEX进一步的帮助。

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

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