-1

I am in the process of making an embedded device that will be configurable through Bluetooth. I have decided that OBEX is the way to go (a similar device is configurable through ethernet using FTP and I want to use the same approach). The embedded side of the bluetooth connection is done. I can upload photo's from my phone to the embedded system, so that works.

Then came the task of creating a windows application for configuring the device. I have never felt the need to keep up with the evolution of windows applications, so I am probably going to end up making an MFC application, but for starters, I will settle for even less: a console app.

I find it extremely difficult to find any C code for Windows that does Bluetooth, but I did come across OpenObex, a project on SourceForge that is not very active (last release 18 months ago) but looked promising. I downloaded the code, read the build instructions and ignored them completely (did not like to work with cmake) and instead make a Visual C++ 2010 project which includes all sources of the library and a test application.

The first thing you want to do with this application is to connect. And there I run into behavior that requires in-depth knowledge of Bluetooth that I cannot seem to google together.

The call to connect() gives GetLastError() return 0x2750, which I found means "host down". Google tells me that in case of windows/bluetooth this means that the host is in 'Disconnected Mode', and I think this also follows from the logs that I made.

First: bluetoothd on the embedded system tells me this (Some of the logging I added myself, in case you go look it up):

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

After this, it takes about a second before the windows application decides 'connect failed', and after that, bluetoothd tells me:

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

The 'Disconnected Mode' may be a direct result of a message sent by the embedded device, because hcidump tells me, among other things, this:

< 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

In timing, this is simultaneous to the windows client deciding 'connection failed'.

A lot of text (sorry! :) ) but I would really appreciate any help that can get me further in doing OBEX from C on windows.

MD XF
  • 7,860
  • 7
  • 40
  • 71
Henk Kok
  • 191
  • 2
  • 11
  • "I am probably going to end up making an MFC application." Sorry I can't make a positive comment but here is the question [Is the Microsoft Foundation Class Library obsolete?](https://www.quora.com/Is-the-Microsoft-Foundation-Class-Library-obsolete) . . . and another comment thread says "There are still programmers making a good living writing/maintaining COBOL and Fortran!" – Weather Vane Nov 24 '17 at 20:04
  • Although my current solution is C, I am using Visual C++ and any solution that requires C++ is workable. For that reason I added C++ to the keywords and caption, but apparently that was not appreciated. – Henk Kok Nov 24 '17 at 21:50
  • I don't understand why I only get comments on eventually going to MFC. OpenObex, which is the basis of my endeavours at the moment, is based on winsock. It goes all the way with socket(SOCK_STREAM, AF_BLUETOOTH, BTHPROTO_RFCOMM), bind(...), and finally the connect() function which does connect, but then immediately disconnects because 'Host Down'/'Disconnected Mode', making connect() return this error. My knowledge of bluetooth ends here and google has failed to fill the gap. My question is to fill that gap so I can get passed the 'Disconnected Mode' problem. – Henk Kok Nov 25 '17 at 08:36

1 Answers1

1

It turns out the issue is with the way the client connects. Connecting to a service can be done in two ways: by port number or by a service class ID. OpenObex assumes the service is found on a port that defaults to 4 but can be set from the command line. For this to work properly, you need to do the service discovery of the device yourself. A better way is to use the service class ID, which will make the server determine the port from the GUID which is reserved for OBEXFileTransfer (00001106-0000-1000-8000-00805f9b34fb)

I changed the code of OpenObex to do exactly this, and the call to connect() now returns with success. I am running into new issues after this, but at least the connecting now works!

Henk Kok
  • 191
  • 2
  • 11