3

I'm developing an Android Bluetooth app that aim to speak using Bluetooth with a device we created, I successfully made everything work on most device on Android 3+ but it seems that android 2.3.x (which is our minimal requirement) doesn't act like the others. I also reproduced the same behavior on a Huawei Ascend P1.

What happens is that on Android side everything acts normal, I connect to the device and a pairing request is made if I'm not paired, I retrieve both in and outstream but when I use them they act like I'm speaking to myself on a loopback. Everything written on the outputstream is read on the inputstream. And of course the device does not see anything (It seems that it doesn't even know that an android phone is connected).

Here are some code sample from my sources (most of it is exactly as described in the android documentation):

Connection thread:

   private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        btSpeaking = true;
        BluetoothSocket tmp = null;
        mmDevice = device;
        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            tmp = device.createInsecureRfcommSocketToServiceRecord(BluetoothUuid.declareUuid.getUuid());
        } catch (IOException e) { }
        mmSocket = tmp;
    }

    public void run() {
        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }

        // Do work to manage the connection (in a separate thread)
        mConnected = new ConnectedThread(mmSocket);
        mConnected.start();
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

Read thread:

private 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) {
            WSLog.e(tag, e.getMessage(), e);
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
            try {
                writeSocket(RESET, empty);
            } catch (IOException e1) {
                return;
            }

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                                              for (int size = 0; size < framesize;) {
                        int read = mmInStream.read(frame, size, frame.length - size);
                        if (read < 0) {
                            return;
                        }
                        size = size + read;
                    }
                } catch (IOException e) {
                    return;
                }
            }

    }

Write socket:

private void writeSocket(byte comm, byte[] data) throws IOException {
        ByteBuffer bf = ByteBuffer.allocate(1 + data.length);
        bf.put(PROTO);
        bf.put(comm);
        bf.put(data);
        mmOutStream.write(bf.array());
    }

I really hope I'm not doing something obviously wrong but as I can't get why it doesn't work I had no choice but to ask for help.

Thanks, Martin

MoAdiB
  • 395
  • 2
  • 10
  • Ok we recently did some fix on the device bluetooth stack that wasn't working on 4.2 Jellybean and it seems that it corrected this problem. We couldn't determine what exactly was the problem causing this bug but acting only on the device side corrected it. Seems that android was misinterpreting somethings but the result in a loopback socket was a little confusing an led us to thing it was a problem on the android side... I'm not the one in charge of the device bt stack so I wont be able to give more details. – MoAdiB Nov 28 '12 at 10:29
  • if you have code for insucre connection pls comment here i required it pls – PankajAndroid Jul 04 '13 at 12:09

0 Answers0