[Ketai library bluetooth] Can I know if it is connected?

[Ketai library bluetooth] Can I know if it is connected?

Hello. Nice to meet you.

The reference source code is as follows. (Example code)
http://ketai.org/examples/bluetoothcursors/
http://ketai.org/reference/net/ketaibluetooth/

Connect to Bluetooth through

bt.connectToDeviceByName(selection);
fig

At this time, is there a variable or method to find out whether the connection is successful?

1 Like

If the information provided by the ketai library in your original post is insufficient, one possibility is to add onBluetoothDataEvent():

 //Call back method to manage data received
void onBluetoothDataEvent(String who, byte[] data) {
  println("Connected.");
}
2 Likes

Dear svan @svan

Thank you for your reply.

By the way,
Is there a way to check if it’s still connected?
(Or maybe just keep checking it periodically?)

Your answer has changed.
I understand what you mean.
Thank you.

Your answer has changed.
I couldn’t get the first answer to work correctly. It was reporting the connection as false when I called connectToDeviceByName() because the connect thread had not yet fired apparently. If it could have been checked a few seconds later (after the connect thread had fired) it would have worked. There is also an isConnected boolean with KBluetoothConnection but I was unsuccessful in calling that also, for reasons unclear to me. There is a jar file for KBluetoothConnection in the library. With the technique that I posted, you know that you are connected when you see data being printed in the console log. Ideally isConnected could be checked periodically; the data is there if we can just get to it.

Alternate method:

Add a button to your app to check for connection device with this code:

  ArrayList<String> devices = bt.getConnectedDeviceNames();
  for (String device : devices) {
    println("connected to :",device);
  }
2 Likes

@svan

Thanks for your thoughts.

boolean BLUE_EN = false;


if(TIMER_CHECK < millis()){
   TIMER_CHECK = millis() + (long)5000;
   BLUE_EN = bt.connectToDeviceByName(selection);
}

I solved it like this. Thank you.

2 Likes