I'm trying to read serial data sent from an external bluetooth module to my HTC Sensation but when I call InputStream.available() - it returns 0 so I can't iterate over the bytes recieved and call InputStream.read(byteArray).
Could somebody help me solve this problem?
Do I need to check for available bytes before reading them?
I apologise for my technically inacurrate post.
Here's my 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()); } }