I am trying to create a Bluetooth application that will simultaneously listen for incoming connections (as a server) and seek out available connections (as a client) using Java and BlueCove. The premise being that two Bluetooth devices (such as smart phones) can automatically discover one another and begin some sort of file exchange or other collaboration as soon as they're within transmission range (this is for an experiment in opportunistic networking).
I have a basic Bluetooth client running in its own thread and a basic Bluetooth server running in its own thread, and I'm trying to get them working at the same time. This has been halfway successful, but, for whatever reason, one device always ends up receiving incoming connections and never finding other devices, and one device ends up finding other connections, but never receiving them (both machines are running identical code, with identical Bluetooth dongles - one machine in Windows XP and the other in Windows 7). I have only tried this with two devices thus far.
I'm receiving exceptions, such as:
"java.io.IOException: Can't query remote device"
"java.io.IOException: Name Lookup error; [10108] No such service is known. The service cannot be found in the specified name space."
"Failed to connect; [10051] A socket operation was attempted to an unreachable network."
Both threads are having to make calls to LocalDevice and StreamConnection, so do I need to synchronize the threads' access to these resources or can the threads access these concurrently? Can I lock those objects, or do I need to cycle the threads, so that the client sleeps while the server is listening for connections, and vice versa (I'm trying to avoid just cycling them because the device discovery sequence in Bluetooth is so slow, and the client thread would be keeping the server tread waiting much of the time)?
I'm inexperienced with multi-threading and am still learning my way around Bluetooth, so if anyone has tips or info, I would really appreciate it.