Android Tutorial [B4XPages] Bluetooth Chat Example

1592143666965.png


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.

1687418029667.png
 

Attachments

  • BluetoothChat.zip
    18.2 KB · Views: 1,379
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:
Copying updated assets files (10)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 25 (B4XMainPage)
java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10302, packageName = b4a.example, attributionTag = null, token = android.os.BinderProxy@858f210, next = null }: enable
at android.os.Parcel.createExceptionOrNull(Parcel.java:3023)
at android.os.Parcel.createException(Parcel.java:3007)
at android.os.Parcel.readException(Parcel.java:2990)
at android.os.Parcel.readException(Parcel.java:2932)
at android.bluetooth.IBluetoothManager$Stub$Proxy.enable(IBluetoothManager.java:724)
at android.bluetooth.BluetoothAdapter.enable(BluetoothAdapter.java:2436)
at anywheresoftware.b4a.objects.Serial$BluetoothAdmin.Enable(Serial.java:392)
at b4a.example.b4xmainpage._initialize(b4xmainpage.java:860)
at b4a.example.b4xpagesmanager._initialize(b4xpagesmanager.java:152)
at b4a.example.main._activity_create(main.java:418)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
at b4a.example.main.afterFirstLayout(main.java:105)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8741)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.bluetooth.BluetoothManagerService.checkPermissionForDataDelivery(BluetoothManagerService.java:5446)
at com.android.server.bluetooth.BluetoothManagerService.checkConnectPermissionForDataDelivery(BluetoothManagerService.java:5464)
at com.android.server.bluetooth.BluetoothManagerService.checkBluetoothPermissions(BluetoothManagerService.java:1792)
at com.android.server.bluetooth.BluetoothManagerService.enable(BluetoothManagerService.java:2120)
at android.bluetooth.IBluetoothManager$Stub.onTransact(IBluetoothManager.java:275)
** Activity (main) Resume **

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.
 
Top