Android Tutorial [B4XPages] Bluetooth Chat Example



Updated implementation, based on B4XPages of https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/#content.

The code is much simpler compared to the previous example.
Note that you can call Serial.Listen without making the device discoverable. This is useful for cases where the devices were already paired once.

Updates:

- Example updated with targetSdkVersion 33. Note the new method to enable Bluetooth if needed.
- Example updated with targetSdkVersion 31 requirements.
Note the new permissions in the manifest editor:
B4X:
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)
These are runtime permissions and are requested at runtime.

 

Attachments

  • BluetoothChat.zip
    18.2 KB · Views: 1,353
Last edited:

rraswisak

Active Member
Licensed User
Longtime User
I download the sample project and run on Redmi Note 8 Pro without any modification, but always getting error while starting discovery.

So, for those with android 10 and found Error starting discovery process just turn Location ON.
Now the sample project run as expected.
 
Last edited:

johnmie

Active Member
Licensed User
Longtime User
Dear Erel,
I just downloaded you Pages Chat program which should be very helpful in learning how to connect to bluetooth devices.
In fact it discovers other devices, but when clicking on them (pairing) I get this error:

java.io.IOException: read failed, socket might closed or timeout, read ret: -1

what have I missed or done wrong?

Thanks for your help and best regards,

john m.
 

johnmie

Active Member
Licensed User
Longtime User
You can only connect to other devices that listen for SPP connections. Run this app on two devices for this to work.
Great, thank you Erel.
But how can I chat with my girlfriend who uses an iPhone and what are the alternatives to SPP?
In fact what I really want is to read data from my kitchen scale.
Do you have a tutorial video on this and where would I find an example?
Thanks,
john m.
 

Beja

Expert
Licensed User
Longtime User
Example updated with targetSdkVersion = 29. The fine location permission is required now.

Hi Erel,
Thanks and I didn't run it yet, but just wondering why is the location required!!
 

Mitesh_Shah

Member
Licensed User
Longtime User
will try to test on my Phone its POCO x2 android version 11
its send data success fully
but when Revived Text, get Error , Connection Is Broken

On Receiver End Will Connect HC05 With PC Serial Terminal
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
I'd wish to add that in case of connecting to an already paired BT device, the only required permission is android.permission.BLUETOOTH_CONNECT
No need to ask for android.permission.ACCESS_FINE_LOCATION, since it's a permission that always raises some eyebrows
 

Filippo

Expert
Licensed User
Longtime User
Hi,

test with my Samsung Galaxy A13, Android 13.
I have not changed anything in this example.
When I start this example, I get this error message:

Edit:
With B4a 12.20 beta works without problems.
 
Last edited:

diego

Member
Licensed User
Longtime User
Hi, I have a problem with this example, in an Android 12 device (MIUI Global 13.0.1) the Serial_Connected event is not raised. In the "client" device it appears as connected and Chat Page is loaded, but in the target device there is no "Log("Serial_Connected: " & Success)"
 

diego

Member
Licensed User
Longtime User
Does it work if you switch the client and server? It might be a problematic device. Bluetooth is not perfect on all devices.
Yes, it works as server, the problem is about detecting connection after serial.Listen.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…