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

**URL:** https://discourse.processing.org/t/ketai-library-bluetooth-can-i-know-if-it-is-connected/45066
**Category:** Processing for Android
**Created:** [September 19, 2024, 11:26pm UTC](https://discourse.processing.org/t/ketai-library-bluetooth-can-i-know-if-it-is-connected/45066 "2024-09-19T23:26:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [September 19, 2024, 11:26pm UTC](https://discourse.processing.org/t/ketai-library-bluetooth-can-i-know-if-it-is-connected/45066/1 "2024-09-19T23:26:54Z")

</div>

[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/examples/bluetoothcursors/)  
[http://ketai.org/reference/net/ketaibluetooth/](http://ketai.org/reference/net/ketaibluetooth/)

Connect to Bluetooth through

> bt.connectToDeviceByName(selection);  
> ![fig](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/d/9d9b97e354c7a2e232e858d466fd9b35bf251350.jpeg)

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

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [September 20, 2024, 12:45am UTC](https://discourse.processing.org/t/ketai-library-bluetooth-can-i-know-if-it-is-connected/45066/2 "2024-09-20T00:45:00Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [September 20, 2024, 4:33am UTC](https://discourse.processing.org/t/ketai-library-bluetooth-can-i-know-if-it-is-connected/45066/3 "2024-09-20T04:33:40Z")

</div>

Dear svan @svan

Thank you for your reply.

1. 

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

1. 

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

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [September 20, 2024, 5:36am UTC](https://discourse.processing.org/t/ketai-library-bluetooth-can-i-know-if-it-is-connected/45066/4 "2024-09-20T05:36:37Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [September 20, 2024, 8:29am UTC](https://discourse.processing.org/t/ketai-library-bluetooth-can-i-know-if-it-is-connected/45066/5 "2024-09-20T08:29:53Z")

</div>

@svan

Thanks for your thoughts.

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