Just to make it short. I want to connect to three devices and simultaneously read data from all three. I know how to connect to one, and I can read the data from this. But what I want is to connect the other two and read them. Any suggestions or other example that I can follow.
Here is a piece of code that is similar to mine, but shorter. This code can connect to the same device using mac. So, how to change this to simultaneously connect to three devices and display the data that they send?
Please help me really need it because I am stuck in this part of my project. Thanks for the help.
private boolean connected = false;
private BluetoothSocket sock;
private InputStream in;
public void test() throws Exception {
if (connected) {
return;
}
BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
getRemoteDevice("00:14:C5:A1:02:67");
Method m = zee.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
sock = (BluetoothSocket)m.invoke(zee, Integer.valueOf(1));
devicecon.setText("Connecting......");
sock.connect();
devicecon.setText("Connected");
in = sock.getInputStream();
byte[] buffer = new byte[50];
int read = 0;
Log.d("ZeeTest", "++++ Listening...");
try {
while (true) {
read = in.read(buffer);
connected = true;
StringBuilder buf = new StringBuilder();
for (int i = 0; i < read; i++) {
int b = buffer[i] & 0xff;
if (b < 0x10) {
buf.append("0");
}
buf.append(Integer.toHexString(b)).append(" ");
}
Log.d("ZeeTest", "++++ Read "+ read +" bytes: "+ buf.toString());
}
} catch (IOException e) {}
Log.d("ZeeTest", "++++ Done: test()");
source
share