android蓝牙应用程序在通话过程中无响应

android蓝牙应用程序在通话过程中无响应,android,bluetooth,Android,Bluetooth,我有一个通过异步任务与蓝牙设备通信的应用程序 如果我接到一个电话,在通话中我会返回应用程序 屏幕变暗,应用程序无响应 后退按钮不起作用。。。并且不显示ANR对话框 有什么想法吗 以下是处理连接的代码: @Override protected Object doInBackground(Object... params) { //boolean protocolUpdated; int read = 0; /

我有一个通过异步任务与蓝牙设备通信的应用程序 如果我接到一个电话,在通话中我会返回应用程序 屏幕变暗,应用程序无响应 后退按钮不起作用。。。并且不显示ANR对话框 有什么想法吗

以下是处理连接的代码:

@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;
}

事实上,没有足够的背景来发现你的问题

确保您从主线程启动此任务,否则PostExecute将连接到错误的线程,您可能会遇到竞争

确保在代码中不向多个处理程序发送相同的消息。 消息是一个链表,在这种情况下,您可能会得到ANR

获取/data/anr/traces.txt以确保它不是anr


您可以在文件开头确定时间。

实际上没有足够的上下文来查找您的问题

确保您从主线程启动此任务,否则PostExecute将连接到错误的线程,您可能会遇到竞争

确保在代码中不向多个处理程序发送相同的消息。 消息是一个链表,在这种情况下,您可能会得到ANR

获取/data/anr/traces.txt以确保它不是anr


您可以在文件的开头确定时间。

谢谢您的回复,谢尔盖。我不确定你是否注意到了,但这个问题是在大约两年前提出的。可悲的是,这个项目已经完成了一段时间,说实话,我甚至都不记得到底是什么问题……谢谢你的回复,谢尔盖。我不确定你是否注意到了,但这个问题是在大约两年前提出的。可悲的是,这个项目已经完成了一段时间,说实话,我甚至都不记得到底是什么问题。。。