简体   繁体   中英

android bluetooth application unresponsive during phone call

I have an application which communicates with a bluetooth device via async task if I receive a phone call and during the call I return to the app the screen dims and the application is unresponsive back button doesn't work... and no ANR dialog is shown any ideas?

here is the code which handles the connection:

@Override
protected Object doInBackground(Object... params) {
    //boolean protocolUpdated;
    int read = 0;                                      // The amount of bytes read from the socket.
    byte[] buff = new byte[MessageHandler.BUFFERSIZE]; // The data buffer.
    byte[] tmpSend = null;                             // Misc bytes arrays returned from ProtocolParser as answers to send after decoding calls.
    in = null;
    out = null;

    try {
        if (Float.parseFloat(version) > 2.2){
            Method m = dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
            sock = (BluetoothSocket) m.invoke(dev, 1);
        }

        else sock = dev.createRfcommSocketToServiceRecord(UUID_RFCOMM_GENERIC); // UUID is constant for serial BT devices.
        sock.connect(); // connect to the BT device. This is rather heavy, may take 3 secs.
        sendMessage(MESSAGE_CONNECTION_ESTABLISHED);
        in = sock.getInputStream();
        out = sock.getOutputStream();

        timer = new Timer();
        startFinishTimer();         //initialize finish timer

        while(read != -1) {     // read = -1 means EOF.
            do {                // as long as there is anything to send in the send queue - send it.
                tmpSend = parser.nextSend();
                if(tmpSend != null){
                    String msg = parseMessage(tmpSend);
                    Log.d("Writing:",msg);
                    out.write(tmpSend);
                }
            } while(tmpSend != null);
            read = in.read(buff);       // read. This is a blocking call, to break this, interrupt the thread.
            timer.cancel();             
            startFinishTimer();         //read is a blocking call so timer should be restarted only after read bytes.
            parser.parse(buff,read);    // parse the read message using the logic in the ProtocolParser derived class.
            tmpSend = parser.getPool(); // if pool ack is required - send it.
            if (tmpSend != null){

                Log.d("Writing:",parseMessage(tmpSend));
                out.write(tmpSend);

            }
            if (read != 0){
                Log.d("Read:",parseMessage(buff));
                tmpSend = parser.getAnswer(); // if answer is required (based on message) - send it.
                if(tmpSend != null){
                    out.write(tmpSend);
                }
            }
            else {
                Exception e = new IOException();
                throw e;
            }
        }
    }catch (IOException e){
        e.printStackTrace();
        Log.d("Connection: ", "Bluetooth Connection CRASHED!");
        sendMessage(MESSAGE_CONNECTION_LOST);
    }catch (Exception e){
        e.printStackTrace();
    } 
    return null;
}

Actually there is not enough context to find your problem.

Make sure that you launch this task from Main thread in other case PostExecute will be attached to wrong thread, you could get a race.

Make sure that you don't send same message to multiple handlers in your code. Message it's a linked list and your could get ANR in that case.

Get /data/anr/traces.txt to make sure that it's not ANR.

You could make sure by time in the beginning of the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM