3

I have made a simple application listening to the Bluetooth serial communication with my Bluetooth module. Bluetooth module is in this case acting as a Server and my Android phone as a Client.

I was able to pair the devices and also to connect to it. However, I am not getting anything to the InputStream. Any idea why? Might it be related to the fact that the module has PIN 1234 and I have never hard coded the PIN (but I was able to connect)

Code of the listener Thread:

public class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
//private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
      //  tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        System.out.println("IO EXCEPTION: "+ e.toString());
    }

    mmInStream = tmpIn;
   // mmOutStream = tmpOut;
}

public void run() {
    byte[] buffer = new byte[1024];  // buffer store for the stream
    int bytes; // bytes returned from read()

    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            // Read from the InputStream
            bytes = mmInStream.read(buffer);
            // Send the obtained bytes to the UI activity
           processBytes(bytes);
        } catch (IOException e) {
            System.out.println("IO EXCEPTION: "+ e.toString());
            break;
        }
    }
}

EDIT:

I have just noticed this error: android bluetooth connection fail (isSocketAllowedBySecurityPolicy start : device null) But I have tried to get UUIDs, and I have the correct one:

00001101-0000-1000-8000-00805f9b34fb

EDIT2:

This might be my issue as well, since I am on SG3 and I use SPP BT module. Android Bluetooth SPP with Galaxy S3

EDIT3: I have also tried to change the:

 tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

with:

tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

as they suggest here: Bluetooth pairing without user confirmation

EDIT4: I have tried to use Android official sample for BT chatting. It had the same issue. I am sure my BT module works, because I have tried it on the PC, and also on the same SG3 with another BT terminal app. However, couple of BT terminals didn't work, I had to find the right one. Can there be any difference?

You should also know that I am sending IR signal to the IR receiver, which passes it to the BT module which then passes it to the mobile app. Baud rate of the IR receiver is 9600, same as IR LED.

Community
  • 1
  • 1
Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103

1 Answers1

2

Your code in the connected thread looks ok until your get to this point:

// Send the obtained bytes to the UI activity
processBytes(bytes);

So without seeing the rest of your code, I am assuming the problem is here. It's not getting to the catch, because there is no exception being thrown. But you are not handling how the bytes are being passed back to the UI correctly.

I notice you are using the code from the api here:
developer.android.com/guide/topics/connectivity/bluetooth

And the thing you are missing is this:

bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                    .sendToTarget();

I too, had exactly this problem when I wrote my first bluetooth app. I was trying to simplify the code sample and attempted to exclude the handler.

/**
 * The Handler that gets information back from the BTManager
 */
private final Handler mHandler = new Handler() {
    @Override public void handleMessage(android.os.Message msg) {
        switch (msg.what) {
            case Constants.MESSAGE_STATE_CHANGE:
                switch (msg.arg1) {
                    case BluetoothManager.STATE_CONNECTED:
                        //  TODO
                        break;
                    // todo .../
                }
                break;
            case Constants.MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                // construct a string from the valid bytes in
                // the buffer
                String readMessage =
                        new String(readBuf, 0, msg.arg1);
                // TODO display your string message
                break;
                // todo .../               
        }
    }
};

If need be you can download the sample developer.android.com/samples, it is important to manage the threads, I use sychroninized methods. Beyond seeing more of your code, this is the most likely guess.

If this doesn't answer your question, let me know.

  • Thank you for the answer, I will test it today and I will let you know as soon as I get it working. I am pretty sure that will be the answer. Thanks again :) – Ondrej Tokar Aug 20 '15 at 14:39
  • Hi again, I haven't tested the code yet, but just so you know, when I debug, the program never moves from this point: mmInStream.read(buffer); so I am not sure how the handler would help it. Anyway I am going to try. – Ondrej Tokar Aug 20 '15 at 16:49
  • I have tried to use the sample from android developers site. All works, except that I am unable to receive bytes. I had no time yesterday, but I will check it over the weekend and let you know. Thanks a lot. – Ondrej Tokar Aug 21 '15 at 06:37
  • I am debugging it right now. I am trying to make it work with the sample app. There is the same issue, it never receives the bytes. I am sure the BT module works, because I have tried it on PC and also I have used a BT serial terminal which works. However, there were couple of BT terminals which didn't work. Please check my last update which explains how the signal is transmitted. – Ondrej Tokar Aug 22 '15 at 15:28
  • 1
    I have just fixed it... The issue was a loose cable on the receiving device... Thank you for all the assistance. I have used the sample code as they handle all errors and it's without bugs. – Ondrej Tokar Aug 22 '15 at 20:21
  • Is it important to use Handler? Can i parse and write resived data to the (Global) int[] array? – Dmitry Lyalin Jun 01 '16 at 13:36
  • "This Handler class should be static or leaks might occur (null) less... (Ctrl+F1) " before i have "threads" and int flags)) Now my code become smaller but i get lags on debug, and message: "too much output to process" but App works fine on different devices... By the way my handler located inside " class MyRun implements Runnable {.." maybe this ok? I press (Ctrl+F1) - but i can understand if it main thread or not.. )) – Dmitry Lyalin Jun 02 '16 at 16:48
  • Yep )) i'll ask new question if problems will happen. – Dmitry Lyalin Jun 03 '16 at 08:38