@Override public void onClick(View v) { /* botones de centreo */ if (v.getId() == R.id.btn_adelante) { try { connectionFuture.get().write(("a").getBytes()); displayedTextBox.setText("adelante"); } catch (IOException e) { debug("Write failed."); } } if (v.getId() == R.id.btn_atras) { try { connectionFuture.get().write(("b").getBytes()); displayedTextBox.setText("atras"); } catch (IOException e) { debug("Write failed."); } } if (v.getId() == R.id.btn_izquiera) { try { connectionFuture.get().write(("c").getBytes()); displayedTextBox.setText("izquierda"); } catch (IOException e) { debug("Write failed."); } } if (v.getId() == R.id.btn_derecha) { try { connectionFuture.get().write(("d").getBytes()); displayedTextBox.setText("derecha"); } catch (IOException e) { debug("Write failed."); } } if (v.getId() == R.id.btn_stop) { try { connectionFuture.get().write(("e").getBytes()); displayedTextBox.setText("stop"); } catch (IOException e) { debug("Write failed."); } } if (v.getId() == R.id.button_transmit) { synchronized (transmitTextBox) { Editable transmitText = transmitTextBox.getText(); String text = transmitText.toString(); transmitText.clear(); try { // Disable and block until this is ready connectionFuture.get().write(text.getBytes()); displayedTextBox.setText("CMD : " + text); debug("Wrote message: " + text); } catch (IOException e) { debug("Write failed."); } } } }
@SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.CUPCAKE) private void onSelectDeviceActivityResult(int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); // debug("GRD"); debug("extras"); // debug("Connecting to: " + address); Log.i("BlueDuino", "Creating connection"); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); connectionFuture = new BluetoothConnection.ConnectionFuture(device, readHandler); if (connectionFuture.failed()) { debug("Connection failed"); } else { final BluetoothConnection.ConnectionFuture localConnection = connectionFuture; final Button localButton = transmitButton; Log.i("BlueDuino", "Starting AsyncTask"); new AsyncTask<Integer, Integer, Boolean>() { public Boolean doInBackground(Integer... params) { localConnection.block(); Log.i("BlueDuino", "done blocking for connection"); return localConnection.failed(); } public void onPostExecute(Boolean failed) { if (!failed) { localButton.setEnabled(true); transmitButton.setText("Enviar"); } } }.execute(); // connection = connectionFuture.get(); // debug("Established connection to: " + address); // try { // connection.write("+RR-".getBytes()); // debug("Writing message"); // } catch (IOException e) { // debug("Write failed."); // } } } }