RFCOMM connection between two Android devices?

人盡茶涼 提交于 2019-11-30 16:35:11

OK, I am newbie, but I can try to help. So here is my experience, I managed to connect two devices using reflection. My Android phone is receiving data using method listenUsingInsecureRfcommOn, while other devices are masters in communication and send the data over BT SPP. I had a problem with this method since it makes no visible SDP record, so I could not detect it with other devices. Because of that, I made small sniffer using Bluecove and Java SE that tries to connect to every port in given range. Here's the code:

package application.test;

import static java.lang.System.out;

import java.io.InputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public class RfCommClient {

    public static void main(String args[]) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        String add = "btspp://8C71F894A36D:";
        String par = ";authenticate=false;encrypt=false;master=true";
        String url = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd;HH-mm-ss-SSS");
        for (int i = 1; i < 15; ++i) {
            try {
                url = add + i + par;
                out.format("Time: %s, port = %d\n", sdf.format(System.currentTimeMillis()), i);
                StreamConnection conn = (StreamConnection) Connector.open(url);
                PrintStream ops = new PrintStream(conn.openOutputStream());
                ops.println("Hi there...");

                // response
                Thread.sleep(1000);

                InputStream is = conn.openInputStream();
                byte[] resp = new byte[5];
                int r = is.read(resp);

                out.println("r = " + r + ", response = " + new String(resp, "US-ASCII"));

                Thread.sleep(10 * 1000);
                conn.close();
            } catch (Exception e) {
                out.println("Exception occured, time = " + sdf.format(System.currentTimeMillis()) + ", i = " + i);
                //e.printStackTrace();
            }
        }

    }
}

What I've learned it that some ports are taken, and that some ports can not be uses (as documentation says, e.g. port 0). For example, port 2 I believe was taken, because when I send some data to it I receive 5 chars back beginning with ERR :).

While, on the other hand, my thread is still waiting?! :) That leads us to another thing I noticed, ports (or channels) are not always mapped to desired number. For example, to me often happened that I want to send something on port 15, but on Android, thread waiting on port 9 received the data :) So I suggest, check which port is really allocated! You can achieve that using the code I posted. And another thing, here is a link to channelPicker function, which selects channel when ordinary API is used, if I am not mistaken, inside some constants should represent reserved channels.

I just noticed something, my code for registering port is slightly different, here is how I do it:

        Method m = cba.getDeclaredMethod("listenUsingInsecureRfcommOn", int.class);
        ss = (BluetoothServerSocket) m.invoke(BluetoothAdapter.getDefaultAdapter(), port);

Anyway, I know that this is probably too late, but, maybe someone in future has similar question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!