I'm working with this elegantly written piece of code from Matt Bell's blog, located here: http://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/
source located here: http://project-greengiant.googlecode.com/svn/trunk/Blog/Android%20Arduino%20Bluetooth Without chopping up the code too badly, I'm trying to fit in a way to elegantly send commands serially to the attached modem, each time waiting until a complete response is received before sending the next command. (I know this is not the Android way of doing things, and I could handle this in a heartbeat using other languages). Here's what I'm working with so far (you'll see I haven't made it very far, in fact this code works fantastically up until the point where I need to wait for the first command to complete before sending more commands). I omitted as much code as I could that doesn't pertain to this question. Or perhaps someone knows of a better example than the above source which already has this solved. Thanks in advance. > public class BluetoothTest extends Activity > { > TextView myLabel; > BluetoothAdapter mBluetoothAdapter; > BluetoothSocket mmSocket; > BluetoothDevice mmDevice; > OutputStream mmOutputStream; > InputStream mmInputStream; > int counter; > Thread workerThread; > byte[] readBuffer; > int readBufferPosition; > volatile boolean stopWorker; > > @Override > public void onCreate(Bundle savedInstanceState) > { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > myLabel = (TextView)findViewById(R.id.label); > > try > { > findBT(); > openBT(); > sendData("enable"); > //insert some code to wait for response before > sending (or handle that in the above line, or otherwise) > sendData("password"); > //insert some code to wait for response before > sending (or handle that in the above line, or otherwise) > sendData("conf t"); > //insert some code to wait for response before > sending (or handle that in the above line, or otherwise) > sendData("interface eth0"); > //insert some code to wait for response before > sending (or handle that in the above line, or otherwise) > //etc...etc...etc... > } > catch (IOException ex) { } > > } > > } > > 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.append("Bluetooth Opened" + "\n"); > } > > void beginListenForData() > { > final Handler handler = new Handler(); > final byte delimiter = 62; //This is the ASCII code for a > > character indicating all data received > > 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() > { > > myLabel.append(data); > > } > }); > } > else > { > readBuffer[readBufferPosition++] = b; > > } > } > } > } > catch (IOException ex) > { > stopWorker = true; > } > } > > } > }); > > workerThread.start(); > } > > void sendData(String msg0) throws IOException > { > msg0 += "\r"; > mmOutputStream.write(msg0.getBytes()); > myLabel.append("Data Sent" + "\n"); > } > > } > > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

