没有从输入流获取可用字节

时间:2013-12-20 07:49:11

标签: android

我有与串口通信的程序 这是获取蓝牙插座的代码

public void ManageConnectedSocket(BluetoothSocket socket)
{
    //final byte delimiter;
    mmSocket = socket;
    //  Volt.setText(mmSocket.toString());
    //  Toast.makeText(getApplicationContext(),mmSocket.toString(), Toast.LENGTH_LONG).show();
    // Get the input and output streams, using temp objects because
    // member streams are final 
    try{
        mmInStream =mmSocket.getInputStream(); 
        mmOutStream = mmSocket.getOutputStream();
    }catch (IOException e){
        //Log exception
    }   
    data(mmSocket);        
} 

data()是一个获取可用字节的函数

public void data(BluetoothSocket socket)
{
    stopWorker = false;
    readBufferPosition = 0;
    readBuffer = new byte[1024];
    int bytesAvailable=0;

    while(!stopWorker)
    {
        try {
            bytesAvailable = mmInStream.available(); 
            Volt.setText( bytesAvailable);
        }catch(Exception e){
            //Log exception
        }   
        stopWorker = true;
    }//while
}

我只是尝试显示edittext伏特中可用的字节数,但不会显示任何字符

1 个答案:

答案 0 :(得分:0)

InputStream.available()提供非常弱的保证,不应该依赖于读取流。 http://developer.android.com/reference/java/io/InputStream.html#available%28%29

知道有多少字节的唯一方法是读取它们