2

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().

Torhan Bartel
  • 550
  • 6
  • 33

1 Answers1

2

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"));
  • Ok thank you.. i didnt know i had to use a fixed UUID for this.. I was able to connect from the Windows PC. I am right, when i try to send messages via `echo TEXT > COM4` ? – Torhan Bartel Feb 21 '14 at 12:17
  • Yeah, you can do it this way or use a HyperTerminal and connect through it. Wouldn't mind having the answer accepted as well ;) – Krzysztof Dubrowski Feb 21 '14 at 12:28
  • mind another question?.. I now do have a Com Port "COM4" from my PC to my Phone, but the phone is still stuck on `checkPoint1` . When i try `echo Hello > COM4` the cmd says: "System cannot find that file". I also tried a connection via putty.. But putty abort with "Unable to open connection to COM4, Unable to open serial port" – Torhan Bartel Feb 21 '14 at 13:02
  • you will have to open an input stream on the socket obtained socket.getInputStream(); then putty should be able to connect – Krzysztof Dubrowski Feb 21 '14 at 13:31
  • But im not even getting past socket = serverSocket.accept(); :( – Torhan Bartel Feb 21 '14 at 13:37
  • I will have to look at that later - I don't have my Bluetooth adapter available right now. What you are describing now seems like you might need to pair the devices first from the system menu. It sometimes happens. have you tried that? – Krzysztof Dubrowski Feb 21 '14 at 13:42
  • They are paired. Thank you for your help though :) – Torhan Bartel Feb 21 '14 at 13:45
  • I will try to look into that when I have access to my Bluetooth adapter – Krzysztof Dubrowski Feb 21 '14 at 13:49
  • I got it working now.. i dont know how, but after some more trial and error it worked :D – Torhan Bartel Feb 21 '14 at 17:38