Using Serial with Processing for Android

I’m trying to use Bluetooth Classic from a Processing app (version 3.5.4) in the Android mode with a Serial library download from the internet “AndroidSerial”. Source code which fails is as follows:

import com.yourinventit.processing.android.serial.*;

void setup() {
  fullScreen();
}

void draw() {
  background(204);
  
 fill(0);
 rect(200,60,60,60);
 fill(255);
 triangle(205,65,255,65,230,115);
     
  if (mousePressed) {
    if ((mouseX > 200) && (mouseX < 260) && (mouseY > 60) && (mouseY < 120)) {
      println(Serial.list(this));
    } 
}
}

The app crashes when the triangle is pressed. Error message follows:

FATAL EXCEPTION: Animation Thread
Process: processing.test.serialtest, PID: 18741
java.lang.IllegalArgumentException: This library works only on Android.
	at com.yourinventit.processing.android.serial.SerialCommunicatorFacory.create(SerialCommunicatorFacory.java:67)
	at com.yourinventit.processing.android.serial.Serial.<init>(Serial.java:41)
	at com.yourinventit.processing.android.serial.Serial.<init>(Serial.java:50)
	at com.yourinventit.processing.android.serial.Serial.list(Serial.java:263)
	at processing.test.serialtest.serialTest.draw(serialTest.java:37)
	at processing.core.PApplet.handleDraw(PApplet.java:1852)
	at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:476)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:516)

I note that the library works only on Android, but the Android mode is selected and an Android sdk is installed on my system (macOS).

Hi.
Have you tried this version of the lib?

@noel very helpful. Latest version of library no longer causes crash. However, now I get the following error message and I’m not sure how to fix it:

Processing-Android USB Serial ERROR

Port:null
Check the following:
1) Make sure AndroidManifest.xm. contains <use-feature> tag for android.hardware.usb.host
2) USB Serial is connected to the device

I’m trying to get this demo to run on a Samsung tablet using an Arduino UNO connected to a Mac mini as the data source. The UNO has a classic Bluetooth shield which is sending test data (tested in another Processing java app and this is working). I have checked all the Bluetooth boxes under Android/Sketch Permissions but do not see “use-feature” in the manifest which is shown below:

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="processing.test.serialtest">
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28"/>
    <application android:icon="@mipmap/ic_launcher" android:label="serialTest">
        <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
</manifest>

How would I add a tag for android.hardware.usb.host to the manifest in Processing?

I greatly appreciate your help.

@svan ===

<manifest ...>
    <uses-feature android:name="android.hardware.usb.host" />

And the port number is properly listed of course?

@svan === it seems to be null…

@noel and @akenaton

Adding the “uses-feature” to the manifest in the project folder didn’t solve the problem. I still get the same error message, therefore I am unable to get the demo to give me a serial port list. However, I have looked in the tablet’s settings and can see that I have previously paired the Bluetooth shield for use with another app. Just to see what would happen, I unpaired it and tried to re-pair it. Now it does not show the shield as being available for pairing and I am unsure why. For now it appears that recognition of the port is the problem and will need more trouble-shooting. I appreciate your help. Will provide follow-up if I ever get it working.

Have you closed all other programs that could be using your serial port, for example arduino serial monitor, hyperterminal, etc. ?

This can help?

@noel - USBDeview appears to be a Windows app and I’m doing this on a Mac. The serial ports on a Mac may be displayed in Terminal using the command “ls /dev/cu.*”. At any rate, I think I’m going down the wrong road; what I need is a list of paired Bluetooth devices and devices available for pairing on my Android tablet. Therefore, I don’t think “println(Serial.list(this))” will work for this purpose. Hopefully, there is another way to find Bluetooth devices using Processing Android; I need to read some more. Thanks.

The following code will find the Bluetooth devices paired with my Samsung tablet:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import java.util.Set;

String[] devices = {};
 
 void findPairedBluetoothDevices() {
 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
 if (adapter == null) {
    println("Device doesn't support Bluetooth.");
 } else {
   Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
   if (pairedDevices.size() > 0) {
    // Get the name and address of each paired device.
     for (BluetoothDevice device : pairedDevices) {
       String deviceName = device.getName();
       String deviceHardwareAddress = device.getAddress(); // MAC address
       String deviceInfo = deviceName + "\n   " + deviceHardwareAddress;
       devices = append(devices, deviceInfo);
       println(deviceInfo);
     }       
  }
 }
}

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  findPairedBluetoothDevices();
}

void draw() {
  color BLUE = color(64,124,188);
  background(BLUE);
  fill(255);
  textSize(48);
  for(int j = 0; j < devices.length; j++) {
  text(devices[j], 130, 100 + (180 * j), 500, 180);
  }
}

2 Likes

Thanks for sharing…