InputStream.Read(byte)返回ioexception:操作已取消

时间:2013-04-18 04:53:46

标签: android bluetooth inputstream ioexception

我开发了一个应用程序,它通过蓝牙连接Android设备和工具。我已经在它们之间建立了连接,但是我遇到了IOException的问题。 它说错误是取消操作。这不是我的计划。

protected void connect(BluetoothDevice device) {

    try {
        Log.d(TAG, "³¢ÊÔÁ¬½Ó");
        // Create a Socket connection: need the server's UUID number of
        // registered
        Method m = device.getClass().getMethod("createRfcommSocket",
                new Class[] { int.class });
        socket = (BluetoothSocket) m.invoke(device, 1);
        socket.connect();
        Log.d(TAG, ">>Client connectted");
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
        final byte[] bytes = new byte[2048];            
        int read = -1;

        while (true) {
            synchronized (obj1) {
                Log.d("masuk while","masuk");

                 // I got the problem here ----!!!!!

                read = inputStream.read(bytes);

                 // -------------------END -----------

                Log.d(TAG, "read:" + read);
                if (read > 0) {
                    final int count = read;
                    String str = SamplesUtils.byteToHex(bytes, count);
                    // Log.d(TAG, "test1:" + str);

                    String hex = hexString.toString();


                    if (hex == "") {
                        hexString.append("<--");
                    } else {
                        if (hex.lastIndexOf("<--") < hex.lastIndexOf("-->")) {
                            hexString.append("\n<--");
                        }
                    }
                    hexString.append(str);
                    hex = hexString.toString();
                    Log.d(TAG, "test2:" + hex);
                    if (hex.length() > maxlength) {
                        try {
                            hex = hex.substring(hex.length() - maxlength,
                                    hex.length());
                            hex = hex.substring(hex.indexOf(" "));
                            hex = "<--" + hex;
                            hexString = new StringBuffer();
                            hexString.append(hex);
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e(TAG, "e", e);
                        }
                    }
                    _handler.post(new Runnable() {
                        public void run() {
                            if (cbxHexView.isChecked()) {
                                sTextView.setText(hexString.toString());
                            } else {
                                                                    //if(!bufferStrToHex(hexString.toString(),false).contains("--")){

                                sTextView
                                        .setText(bufferStrToHex(
                                                hexString.toString(), false)
                                                .trim());
                                temp.put(idxTemp, bufferStrToHex(hexString.toString(),false));
                                idxTemp++;
                            }
                            Log.d(TAG, "ScrollY: " + mScrollView.getScrollY());   
                            int off = sTextView.getMeasuredHeight() - mScrollView.getHeight();   
                            if (off > 0) {   
                                mScrollView.scrollTo(0, off);   
                            }    
                        }
                    });
                }
            }

        }


    } catch (Exception e) {
        Log.e(TAG, ">>", e);
        Log.d("error catch:",e.toString());
        return;
    } finally {
        if (socket != null) {
            try {
                Log.d(TAG, ">>Client Socket Close");
                socket.close();
                socket = null;
                this.finish();
                return;
            } catch (IOException e) {
                Log.e(TAG, ">>", e);
            }
        }
    }

}

实际上我在使用Android版本大约2.x或3.x之前已经尝试过了。但是,我尝试使用4.0.4,问题就出现了。我对此感到困惑。谢谢你的帮助。

- 更新日志CAT --- 这是我正在运行的程序的日志猫

04-18 13:07:08.562: E/MonitorActivity(7448): >>
04-18 13:07:08.562: E/MonitorActivity(7448): java.io.IOException: Operation Canceled
04-18 13:07:08.562: E/MonitorActivity(7448):    at android.bluetooth.BluetoothSocket.readNative(Native Method)
04-18 13:07:08.562: E/MonitorActivity(7448):    at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
04-18 13:07:08.562: E/MonitorActivity(7448):    at  android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
04-18 13:07:08.562: E/MonitorActivity(7448):    at java.io.InputStream.read(InputStream.java:163)
04-18 13:07:08.562: E/MonitorActivity(7448):    at com.iteadstudio.MonitorActivity.connect(MonitorActivity.java:320)
04-18 13:07:08.562: E/MonitorActivity(7448):    at com.iteadstudio.MonitorActivity$3.run(MonitorActivity.java:135)
04-18 13:07:08.562: D/error catch:(7448): java.io.IOException: Operation Canceled
04-18 13:07:08.562: D/MonitorActivity(7448): >>Client Socket Close
04-18 13:07:08.572: A/libc(7448): @@@ ABORTING: INVALID HEAP ADDRESS IN dlfree
04-18 13:07:08.572: A/libc(7448): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)

当我点击错误时,它指向我上面提到的语句。 on read = inputstream.read(bytes);希望能帮助你。

0 个答案:

没有答案