javaandroidbluetoothrfcommspp

Android to PC RFCOMM Channel


So im working an an App that directly connect via Bluetooth to accept String commands from an RFCOMM channel and send responses.

So those are the 2 Classes i am currently working with

Bluetooth_Manager

import android.bluetooth.*;
import android.content.Intent;

public class Reader_BluetoothManager {

    protected Reader_MainScreen main;
    protected BluetoothAdapter bAdapter;
    private Reader_AcceptThread bAccept;

    public Reader_BluetoothManager(Reader_MainScreen main) {
        this.main = main;
        initiate();
    }

    public void initiate(){
        checkForBluetooth();
        enableBluetooth();
    }

    public void log(String l){
        main.log(l);
    }

    public void checkForBluetooth(){
        bAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bAdapter == null) {
            main.log("No Bluetooth supported!");
            return;
        }
        main.log("Bluetooth supported...");
    }

    public void enableBluetooth(){
        if (!bAdapter.isEnabled()) {
            main.log("Bluetooth not enabled... requesting activation");
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            main.startActivityForResult(enableBtIntent, main.BLUETOOTH_ENABLE_CODE);
        }
    }

    public void enableConnection(){
        main.log("Connecting...");
        if (bAccept != null)
            bAccept.interrupt();
        bAccept = new Reader_AcceptThread(this);
        bAccept.start();
    }

    public void stopConnection(){
        if (bAccept != null)
            bAccept.interrupt();
    }
}

And AcceptThread

import java.io.IOException;
import java.util.UUID;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;

public class Reader_AcceptThread extends Thread{

    private BluetoothServerSocket serverSocket;
    private BluetoothSocket socket;
    private BluetoothDevice device;
    private Reader_BluetoothManager main;


    public Reader_AcceptThread(Reader_BluetoothManager main) {
        this.main = main;
    }

    public void run(){
        try {
            serverSocket = main.bAdapter.listenUsingRfcommWithServiceRecord("XXXXXX", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            main.log("checkPoint 1");
            socket = serverSocket.accept();
            main.log("checkPoint 2");
            device = socket.getRemoteDevice();
            main.log("checkPoint 3");
            main.log("connection established...");
            main.log(socket.toString());
        } catch (IOException e) {
            main.log("error...");
            main.log(e.toString());
            main.main.stopConnection();
            interrupt();
        }
        while (!isInterrupted()){
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                interrupt();
                break;
            }
            main.log("ping");
        }
    }
}

I constructed these according to the Google Docs Guide. Also on my Laptop i am doing the following:

enter image description here

And this is, what is happening on my Phone: enter image description here

Here is, what is happening, when im trying to connect from my Windows PC: enter image description here

As you can see on the phones screenshot i am stuck at socket = serverSocket.accept().


Solution

  • The UUID you are using is the UUID of the service - if you want to use the default RFCOMM channel you should use the SPP UUID "00001101-0000-1000-8000-00805F9B34FB" instead of the UUID.randomUUID();

    serverSocket = main.bAdapter.listenUsingRfcommWithServiceRecord("Connection Test", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));