如何在android应用程序中释放inStream Buffer?

时间:2014-04-03 18:18:04

标签: java android bluetooth buffer handler

我开发了一个通过蓝牙与其他设备进行通信的应用程序。 设备不断发送数据,但应用程序形成一个点会停止处理这些数据。

数据是字符串。我从bluetoothsocket的输入流中获取它们然后将它们传递给我的Handler,以便它们显示在TextView上。这是我的主题:

public class ConnectThread extends Thread 
{
private String address;
private boolean connectionStatus;

ConnectThread(String MACaddress) {
    address = MACaddress;
    connectionStatus = true;
}


public void run() {
        try {
            BluetoothDevice device = btAdapter.getRemoteDevice(address);



            try { 

                     btSocket = device.createRfcommSocketToServiceRecord(SPP_UUID); 
            } catch (IOException e) { 
                connectionStatus = false;
            } 
        }catch (IllegalArgumentException e) {
            connectionStatus = false;
        }


       btAdapter.cancelDiscovery();

       try {
            btSocket.connect(); 
       } catch (IOException e1) {
            try {
                 btSocket.close();
                 connectionStatus = false;
            } catch (IOException e2) {
            }
       }
       try { 
            outStream = btSocket.getOutputStream(); 
          } catch (IOException e2) {
            connectionStatus = false;
          }
       try{
        inStream = btSocket.getInputStream();
    }catch (IOException e2){

    }
    if (connectionStatus) {
    mHandler.sendEmptyMessage(1);
    }else {
    mHandler.sendEmptyMessage(0);
    }
    int bytes;
    while (true) {
        try {
            byte[] b = new byte[64];  
            bytes = inStream.read(b);
            mHandler.obtainMessage(MESSAGE_RECEIVE, bytes, 
                                                -1, b).sendToTarget();
           } catch (IOException e) {
               break;
           }
       }
}
}

这是我的处理程序:

mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
     if (myProgressDialog.isShowing()) {
            myProgressDialog.dismiss();
            myclip.stop();
       }

        if (msg.what == 1) {

            connectStat = true;
            if(multi){
                connect_button_m.setText(R.string.connected);
            }else{
                connect_button.setText(R.string.connected);
            }

            microcOut = 0;
            ledStat = false;
            write(microcOut);
        }else if(msg.what == 2){
            byte[] readBuf = (byte[]) msg.obj;
            String strIncom = new String(readBuf, 0, msg.arg1);
             sb.append(strIncom); 
             int endOfLineIndex = sb.indexOf("/");
             if (endOfLineIndex > 0) {
                 String sbprint = sb.substring(0, endOfLineIndex);
                 sb.delete(0, sb.length()); 
                 if(multi){
                     incoming_m.setText("Distance:" + sbprint);

                 }else{
                     incoming.setText("Distance:" + sbprint);
                 }
             }else{
                 if(multi){
                     incoming_m.setText("No incoming data");
                 }else{
                     incoming.setText("No incoming data");
                 }
             }
        }
        else {
         failToast.show();
        }
    }
};

功能应该是它应该是的,除了它看起来好像缓冲区已满并且不能再存储任何东西了。

0 个答案:

没有答案