Go Down

Topic: Processing errors with multiple commands (Read 272 times) previous topic - next topic


Dec 30, 2018, 06:29 am Last Edit: Dec 30, 2018, 07:42 am by yarin4444
I'm starting to work with the processing to run applications in Android that will be integrated with the arduino. When I try to run the following code (which should work) in processing, android mode, I get the errors:

My goal in the end is to link the android app that I'm building in processing and arduino with bluetooth.
Full code:
Code: [Select]

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
import android.view.Gravity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import java.util.UUID;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Log;

import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;

boolean foundDevice=false; //When true, the screen turns green.
boolean BTisConnected=false; //When true, the screen turns purple.

//Get the default Bluetooth adapter
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

/*The startActivityForResult() within setup() launches an
 Activity which is used to request the user to turn Bluetooth on.
 The following onActivityResult() method is called when this
 Activity exits. */
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data){
 if(resultCode == RESULT_OK){
 ToastMaster("Bluetooth has been switched ON");
 } else {
 ToastMaster("You need to turn Bluetooth ON !!!");

/* Create a BroadcastReceiver that will later be used to
 receive the names of Bluetooth devices in range. */
BroadcastReceiver myDiscoverer = new myOwnBroadcastReceiver();
/* Create a BroadcastReceiver that will later be used to
 identify if the Bluetooth device is connected */
BroadcastReceiver checkIsConnected = new myOwnBroadcastReceiver();

void setup(){
 /*IF Bluetooth is NOT enabled, then ask user permission to enable it */
 if (!bluetooth.isEnabled()) {
 Intent requestBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 startActivityForResult(requestBluetooth, 0);
 /*If Bluetooth is now enabled, then register a broadcastReceiver to report any
 discovered Bluetooth devices, and then start discovering */
 if (bluetooth.isEnabled()) {
 registerReceiver(myDiscoverer, new IntentFilter(BluetoothDevice.ACTION_FOUND));
 registerReceiver(checkIsConnected, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
 //Start bluetooth discovery if it is not doing so already
 if (!bluetooth.isDiscovering()){

void draw(){
 //Display a green screen if a device has been found,
 //Display a purple screen when a connection is made to the device
 background(170,50,255); // purple screen
 }else {
 background(10,255,10); // green screen

/* This BroadcastReceiver will display discovered Bluetooth devices */
public class myOwnBroadcastReceiver extends BroadcastReceiver {
 public void onReceive(Context context, Intent intent) {
 String action=intent.getAction();
 ToastMaster("ACTION:" + action);
 //Notification that BluetoothDevice is FOUND
 //Display the name of the discovered device
 String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
 ToastMaster("Discovered: " + discoveredDeviceName);
 //Display more information about the discovered device
 BluetoothDevice discoveredDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 ToastMaster("getAddress() = " + discoveredDevice.getAddress());
 ToastMaster("getName() = " + discoveredDevice.getName());
 int bondyState=discoveredDevice.getBondState();
 ToastMaster("getBondState() = " + bondyState);
 String mybondState;
 case 10: mybondState="BOND_NONE";
 case 11: mybondState="BOND_BONDING";
 case 12: mybondState="BOND_BONDED";
 default: mybondState="INVALID BOND STATE";
 ToastMaster("getBondState() = " + mybondState);
 //Change foundDevice to true which will make the screen turn green
 //Connect to the discovered bluetooth device (SeeedBTSlave)
 ToastMaster("Connecting you Now !!");
 ConnectToBluetooth connectBT = new ConnectToBluetooth(discoveredDevice);
 //Connect to the the device in a new thread
 new Thread(connectBT).start();
 //Notification if bluetooth device is connected
 ToastMaster("CONNECTED _ YAY");
 BTisConnected=true; //turn screen purple
public class ConnectToBluetooth implements Runnable{
 private BluetoothDevice btShield;
 private BluetoothSocket mySocket = null;
 private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
 public ConnectToBluetooth(BluetoothDevice bluetoothShield) {
 btShield = bluetoothShield;
 mySocket = btShield.createRfcommSocketToServiceRecord(uuid);
 }catch(IOException createSocketException){
 //Problem with creating a socket
 public void run() {
 /* Cancel discovery on Bluetooth Adapter to prevent slow connection */
 /*Connect to the bluetoothShield through the Socket. This will block
 until it succeeds or throws an IOException */
 } catch (IOException connectException){
 mySocket.close(); //try to close the socket
 }catch(IOException closeException){
 /* Will cancel an in-progress connection, and close the socket */
 public void cancel() {
 try {
 } catch (IOException e){

/* My ToastMaster function to display a messageBox on the screen */
void ToastMaster(String textToDisplay){
 Toast myMessage = Toast.makeText(getApplicationContext(),
 myMessage.setGravity(Gravity.CENTER, 0, 0);


Please edit your post and change [quote] to [code] and [/quote] to [/code]
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up