0

我开发了一个应用程序,它通过蓝牙将 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);
            }
        }
    }

}

实际上,我在使用 2.x 或 3.x 的 android 版本之前已经尝试过,它是有效的。但是,我尝试使用 4.0.4,出现问题。我对此感到困惑。谢谢您的帮助。

--UPDATE LOG 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)

当我点击错误时,它直接指向我上面提到的语句。在读取 = inputstream.read(bytes); 希望这将是明确的帮助。

4

0 回答 0