APDE crashes on running sketch using Ketai Bluetooth example

In Preview mode, clicked on the Play button. Then a lot of text scrolling through console. The screen flashed once and a popup showed:
“APDE Sketch Preview has stopped”

In App mode, clicked on the Play button. Then a lot of text scrolling through console. It showed:
“Do you want to install an update to this existing application? Your existing data will not be lost. It does not require any special access”
Clicked on Install and the progress bar is shown. At the end a popup showed:
“BluetoothCursors has stopped.”

Not sure what was updating. Play Store doesn’t show any update for ADPE is required.

HI, @bigboss97
I hope you have access to two phones. Below you have a send code and a receiver code for each individual phone. Give permission to Bluetooth on both phones by choosing Settings/Sketch permmissions/ BLUETOOTH and also BLUETOOTH_ADM

The receiver code.

import android.content.Intent;
import android.os.Bundle;
import ketai.net.bluetooth.*;
import ketai.ui.*;

KetaiBluetooth bt;
KetaiList klist;
boolean isConfiguring = true;
String str;

ArrayList devicesDiscovered = new ArrayList();

void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}

void onActivityResult(int requestCode, int resultCode, Intent data) {
  bt.onActivityResult(requestCode, resultCode, data);
}

void setup() {
  fullScreen();
  background(0, 0, 255);
  textAlign(CENTER);
  textSize(40);
  bt.start();
}

void draw() {
if (isConfiguring) { 
    klist = new KetaiList(this, bt.getPairedDeviceNames());
    isConfiguring = false; 
    }
}

void onKetaiListSelection(KetaiList klist) {
  String selection = klist.getSelection();
  bt.connectToDeviceByName(selection);
  klist = null;
}


void onBluetoothDataEvent(String who, byte[] data) {
  if (isConfiguring) {
    return;
  }
  if (data != null){
    str = new String(data);
    background(0, 0, 255);
    text(str, width/2, height/2);
  }
}

The sender code.

import android.content.Intent;
import android.os.Bundle;
import ketai.net.bluetooth.*;
import ketai.ui.*;

KetaiBluetooth bt;
KetaiList klist;
boolean isConfiguring = true;
String  str;
int tel;

ArrayList devicesDiscovered = new ArrayList();

void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}

void onActivityResult(int requestCode, int resultCode, Intent data) {
  bt.onActivityResult(requestCode, resultCode, data);
}

void setup() {
  fullScreen();
  orientation(PORTRAIT);
  background(0, 0, 255);
  bt.start();
  isConfiguring = true;
  frameRate(2);
  textSize(120);
  textAlign(CENTER);
}

void draw() {
  if (isConfiguring) { 
    background(0, 0, 255);
    klist = new KetaiList(this, bt.getPairedDeviceNames());
    isConfiguring = false;
  } else {
    String t = str(tel);
    char[] a = t.toCharArray();
    // a = append(a, '/'); 
    byte[] data = new byte [a.length];
    for (int i = 0; i < a.length; i++) {
      data[i] = byte(a[i]);
    }
    bt.broadcast(data);
    tel++;
    background(0, 0, 255);
    text(t, width/2, height/2);
  }
}

void onKetaiListSelection (KetaiList klist) {
  String selection = klist.getSelection();
  bt.connectToDeviceByName(selection);
  klist = null;
}

void onBluetoothDataEvent(String who, byte[] data) {
  if (isConfiguring) {
    return;
  }
  if (data != null){
  str = new String(data);
  }
}
1 Like

I copied the sender code to my phone.
Opened a new sketch and pasted the codein it. It asked me to install update when I ran it (as described before). Then it crashed :frowning:

Did you give the Bluetooth permission?
What is your Android version?
Please comment out the lines of the else part in the if block in the draw() function, and try once more.
Do you have another device with other Android version?

I’m using Android v8.0.0 It’s the latest what I’ve got.
I’ll go through the code and identify where exactly it crashes. Then I’ll get back to you. Thank you for your time :slight_smile:

I’ve commented out the contents of setup() and draw(). It stopped crashing when I commented out the “new” line.

void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
//  bt = new KetaiBluetooth(this);
}

I checked my app permission. There are following:
Body Sensors, Calendar, Camera, Contacts Location, Microphone, Phone, SMS, Storage
I can’t find bluetooth.

Tap on the three dots top right screen to open overflow menu. Choose “Sketch Properties”, and then Sketch Permissions. All the permissions should be there.

I checked the boxes BLUETOOTH and BLUETOOTH_ADMIN and enabled bluetooth of my phone. It still crashes :frowning:

OK.
Let’s try it different.
But for this test yo need two phones and pair them. On one phone you load the code below and change the string MY_PHONE with the string that appears as paired device on the other phone you want to connect to. (You see them in your phone :Settings/Bluetooth).
On the other phone you install an app named “Serial Bluetooth Terminal”, that you can find on Google’s Play store. In that app you tap on the menu top left and choose “Devices”. There you must see the name of the other phone if it is paired. Tab on the name of that phone and the Terminal will open. Then you run the sketch’s code below on the other phone. Now tap on the icon in the bluetooth terminal app, that looks like two hearts left of the trash can (top right).If everything goes well you will see ,"Connecting to (paired phone’s name) ", and then “Connected”. Nothing more will happen, it’s just to verify the connection.

import ketai.net.bluetooth.*;

KetaiBluetooth bt;

void setup() {
   bt = new KetaiBluetooth(this);
   bt.getPairedDeviceNames();
   bt.connectToDeviceByName("MY_PHONE");
   bt.start();
}

Thank you for your response. I think that I need a little bit more time for this test :slight_smile: I’ll get back to you.

You can already try installing the code above on one phone. If it not crashes it will say in the console that the device could not be found. If it not crashes it’s already a big step forward.

I tried it with my bluetooth speaker. I know it’s paired because I’ve been using RFO-BASIC to send BT commands to the speaker.
Actually, the device does not matter because it already crashed in the first line while generating the object with “new” :frowning_face:

So your device does work with Bluetooth using the RFO-BASIC app?
I tried that language before I found Processing.
I really liked it, because Basic is my background, but it is incredible slow with graphics.
The only thing the code above does is creating a Ketai Bluetooth object. There are two possibilities left; or the Ketai lib does not work with Android OREO, or the APDE does not write the permissions in the Android manifest file.
In the f irst case I could write the code in plain Android java but I would need your phone to test it. In the second case I can guide you to the build folder, but to acsess that, your phone needs to be rooted. Is it?
Another thing you could try, is to load your sketch with an usb cable on your PC using Processing for Android Mode.

Yes, I’ve done some BT stuff with RFO and no problems:

Did you mean running Processing in PC version and with USB connecting to my phone? Do I need to set my phone to developer mode or something?

1 Like

Yes, tapping 7 times on the “About this device”, bar in “Settings”, will create a new, “Developer Options” bar, where you set the usb options, to be able to use the MTP mode (Media Transfer Protocol.). I had a look at the Basic’s forum. It’s pretty quiet now, and the app isn’t available on the GPlay store anymore. A pity.
Anyhow, I would give it a try on PC, and let us know the result.

I installed Processing (3.5.3) and Ketai on my PC and ran this:

import ketai.net.bluetooth.*;
KetaiBluetooth bt;

void setup()
{
  bt= new KetaiBluetooth(this);
//  bt.getPairedDeviceNames();
  }

Then I received “Debugger busy…”

java.lang.NoClassDefFoundError: android/content/BroadcastReceiver
	at sketch_200104a.setup(sketch_200104a.java:24)
	at processing.core.PApplet.handleDraw(PApplet.java:2425)
	at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Caused by: java.lang.ClassNotFoundException: android.content.BroadcastReceiver
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 4 more