Here is my code:
Server side (querying and asking the clients to connect):
Code:
public class BluetoothServer implements Runnable, DiscoveryListener {
private LocalDevice local_device;
private UUID uuid;
private DiscoveryAgent disc_agent;
private Vector devices;
private List deviceList;
private ServiceRecord[] service;
private RemoteDevice[] otherDevice;
DataOutputStream[] output;
DataInputStream[] input;
private boolean deviceChosen;
private boolean connect;
Thread t;
String url;
StreamConnection[] conn;
public boolean connected;
public int numberOfClients;
int globalIndex;
public BluetoothServer(int num) {
this.numberOfClients = num;
this.connected = false;
this.otherDevice = new RemoteDevice[this.numberOfClients];
this.output = new DataOutputStream[this.numberOfClients];
this.input = new DataInputStream[this.numberOfClients];
this.service = new ServiceRecord[this.numberOfClients];
this.conn = new StreamConnection[this.numberOfClients];
globalIndex = 0;
devices = new Vector();
deviceChosen = false;
connect = false;
deviceList = new List("Devices discovered", List.EXCLUSIVE);
t = new Thread(this);
t.start();
}
public void run() {
try {
local_device = LocalDevice.getLocalDevice();
disc_agent = local_device.getDiscoveryAgent();
local_device.setDiscoverable(DiscoveryAgent.GIAC);
disc_agent.startInquiry(DiscoveryAgent.GIAC, this);
uuid = new UUID(0x0003);
while (!deviceChosen) {
}
for (int i = 0; i < numberOfClients; i++) {
if (connect) {
int transationID = disc_agent.searchServices(new int[]{0x0100}, new UUID[]{uuid}, otherDevice[i], this);
}
while (globalIndex != i + 1) {
}
}
} catch (BluetoothStateException e) {
} catch (IOException e) {
}
}
public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1) {
try {
devices.addElement(arg0);
} catch (Exception ex) {
}
}
public void inquiryCompleted(int discType) {
if(discType ==INQUIRY_COMPLETED){
if (devices.size() > 0) {
deviceList.deleteAll();
int index = 0;
for (int i = 0; i < devices.size(); i++) {
try {
RemoteDevice rd = (RemoteDevice) devices.elementAt(i);
//Assuming the devices that we need to connect have their names beginning with "Dev"
if (rd.getFriendlyName(false).toString().substring(0, 3).equals("Dev")) {
otherDevice[index] = rd;
index++;
if (index >= numberOfClients) {
deviceChosen = true;
connect = true;
break;
}
}
} catch (IOException e) {
}
}
} else {
deviceChosen = true;
}
}
}
public void serviceSearchCompleted(int transID, int respCode) {
if (respCode == SERVICE_SEARCH_COMPLETED) {
try {
createConnection();
} catch (Exception e) {
}
}
}
private void createConnection() {
try {
url = service[globalIndex].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
conn[globalIndex] = (StreamConnection) Connector.open(url);
output[globalIndex] = conn[globalIndex].openDataOutputStream();
input[globalIndex] = conn[globalIndex].openDataInputStream();
globalIndex++;
if (globalIndex >= numberOfClients) {
connected = true;
}
} catch (Exception e) {
}
}
public void servicesDiscovered(int arg0, ServiceRecord[] servRecord) {
service[globalIndex] = servRecord[0];
}
}
Client Side (waiting for the Server to query and connect):
Code:
public class BluetoothClient implements Runnable {
private StreamConnectionNotifier con;
Thread t;
public boolean connected;
StreamConnection conn;
DataInputStream in;
DataOutputStream out;
public BluetoothClient() {
this.connected = false;
t = new Thread(this);
t.start();
}
public void run() {
createConnections();
}
private void createConnections() {
UUID uuid = new UUID(0x0009);
LocalDevice localDevice;
try {
localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC);
con = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + uuid + ";name=batalha_emulator;authorize=false");
conn = con.acceptAndOpen();
in = conn.openDataInputStream();
out = conn.openDataOutputStream();
connected = true;
} catch (Exception e) {
}
}
}
Can someone please help?