Ritchie
unread,Feb 24, 2012, 10:02:09 AM2/24/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to bluecove-users
Hi,
I'm having a problem with connecting to multiple devices using
Bluecove 2.1.1 SNAPSHOT on a Windows 7 system. Below is some of my
code:
////////////////////////////////////////////////////////////////////////////
package Threading.TwoIMU;
import java.util.Vector;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
public class BluetoothConnection implements DiscoveryListener {
// object used for waiting
private static Object lock;
// vector containing the devices discovered
private static Vector<RemoteDevice> vecDevices;
static public void getLocalHost() throws BluetoothStateException {
vecDevices = new Vector<RemoteDevice>();
lock = new Object();
// Create new instance of class.
BluetoothConnection client = new BluetoothConnection();
// Display local device address and name.
LocalDevice localDevice = LocalDevice.getLocalDevice();
// Find devices.
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
// Start search for devices
agent.startInquiry(DiscoveryAgent.GIAC, client);
// Wait until search is complete.
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String getIMUURL(String IMUName) {
int index = 0;
// How many devices were found.
RemoteDevice[] count = new RemoteDevice[vecDevices.size()];
// Loop through devices looking for element containing IMU number/
name.
for (int i = 0; i < vecDevices.size(); i++) {
count[i] = vecDevices.elementAt(i);
if (count[i].toString().contains(IMUName)) {
index = i;
}
}
// Remote device created for device at saved element.
RemoteDevice remoteDevice = (RemoteDevice)
vecDevices.elementAt(index);
String connectionURL = ("btspp://" +
remoteDevice.getBluetoothAddress() + ":
1;authenticate=false;encrypt=false;master=false");
return connectionURL;
}
// methods of DiscoveryListener
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
// add the device to the vector
if (!vecDevices.contains(btDevice)) {
vecDevices.addElement(btDevice);
}
}
// implement this method since services are not being discovered
public void servicesDiscovered(int transID, ServiceRecord[]
servRecord) {
if (servRecord != null && servRecord.length > 0) {
}
synchronized (lock) {
lock.notify();
}
}
// implement this method since services are not being discovered
public void serviceSearchCompleted(int transID, int respCode) {
synchronized (lock) {
lock.notify();
}
}
// Releases lock when search is complete.
public void inquiryCompleted(int discType) {
synchronized (lock) {
lock.notify();
}
}
}
////////////////////////////////////////////////////////////////////////////
In a main method I'm calling the getLocalHost method once, then
creating two threads where each tries to open a connection after they
obtain the URL for each of the devices from the getIMUURL method
above.
This URL is then passed into:
StreamConnection getStream = (StreamConnection) Connector.open(url);
But I believe that because I'm trying to connect to two devices at
exactly the same time (parallel) it's resulting in this error:
javax.bluetooth.BluetoothConnectionException: Failed to connect;
[10048] Only one usage of each socket address (protocol/network
address/port) is normally permitted.
What am I doing wrong? Creating one thread and connecting has no
problems, so it's just the concurrency which is resulting in problems.
I should also mention that one device connects, it's just the other
device that fails.