Hi Vlad,
Thanks for your quick reply!
I've tried again the example above, and it worked, it was a mistake
of mine.
Now I'm trying to do device discover on one adapter and OBEX push
on other. But I'm unable to initialize the stacks, I get
IllegalArgumentException:
BlueCove stack already initialized.
In fact I'm a bit confused on where to call static methods to
BlueCoveImpl.
I've trying to code something like the API example.
The test I'm trying follows below.
RootThread2 should do the device discovery task, and RootThread3
should
push a text file to an arbitrary device.
The problem arises when BlueCoveImpl.getThreadBluetoothStackID() is
called.
RootThread2 calls this method first on the constructor. When called on
RootThread3's constructor, the IllegalStateException is thrown.
----------- 8< ------------- Java code
--------------------------------------
package mypack;
import java.io.IOException;
import java.io.OutputStream;
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;
import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;
import com.intel.bluetooth.BlueCoveImpl;
public class MultipleAdaptersTest2 {
public static void main(String[] args) throws BluetoothStateException
{
RootThread2 rootThread = new RootThread2();
Thread r = new Thread(rootThread);
RootThread3 rootThread3 = new RootThread3();
Thread r3 = new Thread(rootThread3);
r.run();
r3.run();
}
}
final class RootThread3 implements Runnable {
private final Object id;
public RootThread3() throws BluetoothStateException {
BlueCoveImpl.useThreadLocalBluetoothStack();
BlueCoveImpl.setConfigProperty("bluecove.deviceID", "1");
this.id = BlueCoveImpl.getThreadBluetoothStackID();
}
public void run() {
Thread t2 = new Thread(new Runnable() {
public void run() {
BlueCoveImpl.useThreadLocalBluetoothStack();
System.out.println("T2: " + id);
BlueCoveImpl.setThreadBluetoothStackID(id);
try {
new ObexPutClient().run();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t2.run();
}
private class ObexPutClient {
public void run() throws IOException, InterruptedException {
BlueCoveImpl.useThreadLocalBluetoothStack();
BlueCoveImpl.setThreadBluetoothStackID(BlueCoveImpl.getCurrentThreadBluetoothStackID());
String serverURL = "btgoep://111111111111:6";
System.out.println("Connecting to " + serverURL);
ClientSession clientSession = (ClientSession)
Connector.open(serverURL);
HeaderSet hsConnectReply = clientSession.connect(null);
if (hsConnectReply.getResponseCode() !=
ResponseCodes.OBEX_HTTP_OK) {
System.out.println("Failed to connect");
return;
}
HeaderSet hsOperation = clientSession.createHeaderSet();
hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
hsOperation.setHeader(HeaderSet.TYPE, "text");
//Create PUT Operation
Operation putOperation = clientSession.put(hsOperation);
// Send some text to server
byte data[] = "Hello world!".getBytes("iso-8859-1");
OutputStream os = putOperation.openOutputStream();
os.write(data);
os.close();
putOperation.close();
clientSession.disconnect(null);
clientSession.close();
}
}
}
final class RootThread2 implements Runnable {
private final Object id;
public RootThread2() throws BluetoothStateException {
BlueCoveImpl.useThreadLocalBluetoothStack();
BlueCoveImpl.setConfigProperty("bluecove.deviceID", "0");
this.id = BlueCoveImpl.getThreadBluetoothStackID();
}
public void run() {
Thread t1 = new Thread(new Runnable() {
public void run() {
BlueCoveImpl.useThreadLocalBluetoothStack();
System.out.println("T1: " + id);
BlueCoveImpl.setThreadBluetoothStackID(id);
try {
DiscoveryAgent agent =
LocalDevice.getLocalDevice().getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, new Listener());
} catch (BluetoothStateException e) {
e.printStackTrace();
}
}
});
t1.run();
}
private final class Listener implements DiscoveryListener {
public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1) {
System.out.println("Discovered: " + arg0.getBluetoothAddress());
}
public void inquiryCompleted(int arg0) {
System.out.println("Completed");
}
public void serviceSearchCompleted(int arg0, int arg1) {
System.out.println("Service search completed");
}
public void servicesDiscovered(int arg0, ServiceRecord[] arg1) {
System.out.println("Services discovered");
}
}
}
----------- 8< ------------- Java code
--------------------------------------
This is the stack trace I get when running this program:
BlueCove version 2.0.3-SNAPSHOT on bluez
Exception in thread "main" java.lang.IllegalArgumentException:
BlueCove Stack already initialized
at
com.intel.bluetooth.BlueCoveImpl.setConfigProperty(BlueCoveImpl.java:
777)
at
net.infomobile.bluecast.server.RootThread3.<init>(MultipleAdaptersTest2.java:
42)
at
net.infomobile.bluecast.server.MultipleAdaptersTest2.main(MultipleAdaptersTest2.java:
27)
BlueCove stack shutdown completed
Line 42 is: BlueCoveImpl.setConfigProperty("bluecove.deviceID", "1");
Sorry for the big post...!
Maybe you could provide me another example, and I'll try to find
where is the error on my code.
Thanks again,
Rodolpho