Android Bluetooth Lire InputStream

Je suis en train de lire la série de données envoyées à partir d'un module bluetooth externe sur mon HTC Sensation, mais quand je l'appelle InputStream.disponible() - renvoie la valeur 0, donc je ne peux pas effectuer une itération sur les octets reçus et appels InputStream.lire(byteArray).

Quelqu'un pourrait m'aider à résoudre ce problème?

Ce que je dois vérifier pour d'octets disponibles avant de les lire?

Je prie de m'excuser pour mon techniquement inacurrate post.

Voici mon code:

public class BluetoothTest extends Activity
{
TextView myLabel;
TextView snapText;
EditText myTextbox;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button openButton = (Button)findViewById(R.id.open);
Button closeButton = (Button)findViewById(R.id.close);
Button chkCommsButton  = (Button)findViewById(R.id.chkCommsButton);
Button offButton = (Button)findViewById(R.id.offButton);
myLabel = (TextView)findViewById(R.id.mylabel);
snapText = (TextView)findViewById(R.id.snapText);
//Open Bluetooth
openButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try 
{
findBT();
openBT();
}
catch (IOException ex) { }
}
});
//Close Bluetooth
closeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try 
{
closeBT();
}
catch (IOException ex) { }
}
});
//Check Comms     - multicast all SNAP nodes and pulse their  BLUE led
chkCommsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
chkcommsButton();
} catch (Exception e) {
//TODO: handle exception
}
}
});
//Off Button    - set strip to all OFF
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
offButton();
} catch (Exception e) {
//TODO: handle exception
}
}
});
}
void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
myLabel.setText("No bluetooth adapter available");
}
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("BTNODE25"))    //Change to match RN42 - node name
{
mmDevice = device;
Log.d("ArduinoBT", "findBT found device named " + mmDevice.getName());
Log.d("ArduinoBT", "device address is " + mmDevice.getAddress());
break;
}
}
}
myLabel.setText("Bluetooth Device Found");
}
void openBT() throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);        
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
beginListenForData();
myLabel.setText("BT  << " + mmDevice.getName()  + " >> is now open ");
}
void closeBT() throws IOException
{
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
myLabel.setText("Bluetooth Closed");
}
void beginListenForData()
{
final Handler handler = new Handler(); 
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{                
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try 
{
int bytesAvailable = mmInputStream.available();  
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
snapText.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
} 
catch (IOException ex) 
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
void offButton() throws IOException
{
mmOutputStream.write("0".getBytes());
}
void chkcommsButton() throws IOException
{
mmOutputStream.write("1".getBytes());
}

}

Simplement se débarrasser de lui et de bloc dans la méthode de lecture. Au moment où vous êtes juste de fumer le CPU et, accessoirement, le drainage de la batterie, lorsqu'elle est disponible() est égale à zéro, ce qui est la plupart du temps dans n'importe quelle plateforme.
Je ne comprends pas ce que tu veux dire par qui.

OriginalL'auteur | 2014-04-03