Android System.err 与蓝牙断开连接时

标签 android bluetooth

我正尝试尽可能优雅地断开与蓝牙设备的连接,但 Android 坚持向我抛出系统错误:

09-02 15:16:16.748: W/System.err(31038): java.io.IOException: Operation Canceled
09-02 15:16:16.748: W/System.err(31038):    at android.bluetooth.BluetoothSocket.readNative(Native Method)
09-02 15:16:16.748: W/System.err(31038):    at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
09-02 15:16:16.748: W/System.err(31038):    at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:60)
09-02 15:16:16.748: W/System.err(31038):    at com.bluetooth.BluetoothRemoteControlApp$ConnectedThread.run(BluetoothRemoteControlApp.java:207)

我使用的是 BluetoothChat 示例程序的修改版本,我的连接线程是这样的:

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;

        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;

        connected = true;
    }

    public void run() {
        byte[] buffer = new byte[1024];
        int bytes;
        byte ch;

        while(connected) {
            try {
                bytes = 0;
                while((ch = (byte) mmInStream.read()) != '\n')
                {
                    buffer[bytes++] = ch;
                }

                busy = false;

                String msg = new String(buffer, "UTF-8").substring(0, bytes - 1);

                Log.d(TAG, "Read: " + msg);

                if(activityHandler != null) {
                    activityHandler.obtainMessage(BT_READ, bytes, 0, msg).sendToTarget();
                }

            }
            catch(IOException e) {
                //e.printStackTrace();
                Log.w(TAG, "Cancelling");
                break;
            }
        }
    }

    public void write(byte[] buffer) {
        // . . .
    }

    public void cancel() {
        Log.i(TAG, "Cancelling connected thread");
        try {
            mmSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在断开连接期间,我调用了抛出系统错误的取消方法。我应该担心并尝试修复它吗?我真的不喜欢在我的日志中看到错误...

最佳答案

我认为您不必太担心这个日志,因为您正在取消并且日志本身显示Operation Cancelled

我认为您在运行方法中使用 e.printStacktrace 打印日志。这就是您看到此日志的原因。

关于Android System.err 与蓝牙断开连接时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12236372/

相关文章:

java - 将核心对象存储到 SQLite 数据库中

android - Android View.postDelayed 是否泄漏引用?

android - 如何在Android上强制关闭应用程序,例如系统关闭

java - BLE GATT 服务器中的服务和配置文件有什么区别

安卓 2.1 : Bluetooth Encryption

java - 开始使用基于计算机 (linux) 的蓝牙设备,使其可被周围的手机发现

java - 将 string.xml 中的值从一个 Activity 传递到另一个 Activity ?

android - 监听 Tab/Android 中的 Switch 状态变化

ios - 如何更改蓝牙弹出消息语言?

java - Android蓝牙开发,如何获取UUID?