# APDE crashes on running sketch using Ketai Bluetooth example

**URL:** <https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129>\
**Category:** Processing for Android\
**Created:** [December 5, 2019, 12:02am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129 "2019-12-05T00:02:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [December 5, 2019, 12:02am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/1 "2019-12-05T00:02:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 5, 2019, 5:34am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/2 "2019-12-05T05:34:33Z")

</div>

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.

```auto
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.

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [December 6, 2019, 2:33am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/3 "2019-12-06T02:33:10Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 6, 2019, 5:57am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/4 "2019-12-06T05:57:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [December 7, 2019, 12:03am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/5 "2019-12-07T00:03:55Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [December 7, 2019, 11:43am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/6 "2019-12-07T11:43:22Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 7, 2019, 11:50am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/7 "2019-12-07T11:50:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [December 8, 2019, 3:08am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/8 "2019-12-08T03:08:12Z")

</div>

I checked the boxes BLUETOOTH and BLUETOOTH\_ADMIN and enabled bluetooth of my phone. It still crashes ☹

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 8, 2019, 6:07am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/9 "2019-12-08T06:07:31Z")

</div>

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.

```auto
import ketai.net.bluetooth.*;

KetaiBluetooth bt;

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

```

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [December 8, 2019, 11:41am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/10 "2019-12-08T11:41:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 8, 2019, 11:45am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/11 "2019-12-08T11:45:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [December 11, 2019, 12:53am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/12 "2019-12-11T00:53:49Z")

</div>

> [@noel](#):
>
> ```auto
> import ketai.net.bluetooth.*;
> 
> KetaiBluetooth bt;
> 
> void setup() {
> bt = new KetaiBluetooth(this);
> bt.getPairedDeviceNames();
> bt.connectToDeviceByName("MY_PHONE");
> bt.start();
> }
> 
> ```

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” ☹

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 11, 2019, 6:14am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/13 "2019-12-11T06:14:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [December 11, 2019, 6:35am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/14 "2019-12-11T06:35:14Z")

</div>

> [@noel](#):
>
> So your device does work with Bluetooth using the RFO-BASIC app?
> 
> 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:

[![](https://img.youtube.com/vi/iuTsVWjvWLU/maxresdefault.jpg "Banner - Timebox Hacks Vol. 5") ](https://www.youtube.com/watch?v=iuTsVWjvWLU)

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?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 11, 2019, 10:26am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/15 "2019-12-11T10:26:01Z")

</div>

> [@bigboss97](#):
>
> developer mode or something?

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.

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [January 4, 2020, 11:59am UTC](https://discourse.processing.org/t/apde-crashes-on-running-sketch-using-ketai-bluetooth-example/16129/16 "2020-01-04T11:59:42Z")

</div>

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

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

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

```

Then I received “Debugger busy…”

```auto
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

```
