3

I am new to Android programming, and have java concept, I want to know that how can I send and receive data using bluetooth without pairing or any password (Only if both device has my app installed), any suggestion?

Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
  • Sockets can skip the BT portion and go direct via Wifi (IP). It might be easier than navigating the searching + pairing steps of BT. – ethrbunny Jun 27 '11 at 14:12
  • I have the same problem, I am trying to recive information from range finder by bluetooth without pairing the devices ( and I am 100% sure that it is possible ). Have You get any solution ? –  Apr 21 '17 at 05:44
  • 1
    @SalutAmigo, This question I asked in 2011, and now it's 2017. It was not possible at that time due to security set by Android. I have no idea, how library behaving now. So, read their official documentation about it. Thanks. – Chatar Veer Suthar Apr 21 '17 at 06:06

2 Answers2

2

As far as I know it's impossible to send or receive data over RFCOMM sockets without pairing. I've tried this extensively with an application that I'm developing. My results are:

  • If the two devices are paired and discoverable, bi-directional communication is possible
  • If the two devices are paired, but the "server" device (the one the android device is trying to connect to) is set to be not discoverable, then bi-directional communication is still possible
  • If the two devices are not paired, but the "server" device is discoverable, then a pairing request is still required before bi-directional communication is possible. This means that RFCOMM client sockets (i.e., those from Android) require the devices to be paired. This was tested on a Samsung Captivate running Android 2.2. I find this very strange, as I can understand requiring pairing before allowing RFCOMM server sockets, but requiring pairing for client sockets is a bit stringent.

As @ethrbunny mentioned you can also just use WiFi, setup parallel server/client threads on each device, and send whatever you want. To discover services on a local network you can optionally use zeroconf.

zeitkunst
  • 505
  • 5
  • 11
1

I got the the following from this Google groups post by Kristopher Micinski. Hope it helps.

I believe the key to getting this to work is in the mUuid list.

Take a close look at what this is doing:

for (int i = 0; i < Connection.MAX_SUPPORTED && myBSock == null; i++) {
    for (int j = 0; j < 3 && myBSock == null; j++) {
        myBSock = getConnectedSocket(myBtServer, mUuid.get(i));
        if (myBSock == null) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                Log.e(TAG, "InterruptedException in connect", e);
            }
        }
    }
}


What this code does is looks to connect to the device, but how does it do so? It tries the socket multiple times, using multiple UUIDs for the session.

In essence it means that we can use UUID only once. So instead this application implements using seven UUIDs, then the server listens and accepts each UUID on the server side, this is what is done with the following code:

for (int i = 0; i < Connection.MAX_SUPPORTED && maxConnections > 0; i++) {
    BluetoothServerSocket myServerSocket = mBtAdapter.listenUsingRfcommWithServiceRecord(srcApp, mUuid.get(i));
    BluetoothSocket myBSock = myServerSocket.accept();
    myServerSocket.close(); // Close the socket now that the
    // connection has been made.

    String address = myBSock.getRemoteDevice().getAddress();

    mBtSockets.put(address, myBSock);
    mBtDeviceAddresses.add(address);
    Thread mBtStreamWatcherThread = new Thread(new BtStreamWatcher(address));
    mBtStreamWatcherThread.start();
    mBtStreamWatcherThreads.put(address, mBtStreamWatcherThread);
    maxConnections = maxConnections - 1;
    if (mCallback != null) {
        mCallback.incomingConnection(address);
    }
}

Now, on the client side of things what is done? The client does not know how many active connections the server currently has.

If we have some agreed upon order that the clients must use we can simply use this, however, in our case, we simply just try each UUID in sequence until we "find the right one."

Short version: -- Use multiple UUIDs, you can only use one at once. So define seven (max for piconet usage) and try each one until you find the right one.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
itsMePaki
  • 11
  • 1
  • but dear I want to send and receive both, like I send one string to Android then that android will also send me ,,,,, means both client and server be a device, so what do u say? – Chatar Veer Suthar Jul 01 '11 at 04:58
  • For what you're (apparently) trying to do it won't matter who is client/server once they are connected. They will just swap messages. Both processes will have some blocking process on 'socket->read' that traps characters until a full message is rec'd. Then deal with the message and respond. – ethrbunny Jul 01 '11 at 17:49