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 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 KB · Views: 1,130
Last edited:

andredamen

Member
Licensed User
Longtime User
I have a problem with this example: It sticks with
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
and don't get further in de program.

Bluetooth is on and the printer is detected in the tablet.

The first time I run the program it detected all the bluetooth devices. Later on it sticks with the wait for.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I have a problem with this example: It sticks with
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
and don't get further in de program.

Bluetooth is on and the printer is detected in the tablet.

The first time I run the program it detected all the bluetooth devices. Later on it sticks with the wait for.
Are you using latest version of B4A?
 

GMan

Well-Known Member
Licensed User
Longtime User
The reason that I'm asking is that there was a bug in a previous version of RuntimePermissions that can cause it. I recommend you to delete all the internal libraries folder and reinstall B4A.
I an "old" app (SDK 26) Bluetooth works fine, but now in SDK 31 i got this message in the log:
B4X:
java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission for AttributionSource { uid = 10575, packageName = msgoethe.fpideenfabrik, attributionTag = null, token = [email protected], next = null }: Starting discovery.
I am using this in the manifest

B4X:
'End of default text.
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)
and this in the code:
B4X:
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)

When i try to set this:
B4X:
rp.CheckAndRequest(rp.PERMISSION_BLUETOOTH_SCAN)
it is red underlined and the description for this say:
B4X:
Unknown member permission_bluetooth_scan

What am i missing ?
B4A is up to date
 

GMan

Well-Known Member
Licensed User
Longtime User
Thx for the hint, but i could not figure it out.

Here is my code i used before:

B4X:
Sub btnSearchForDevices_Click
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    'rp1.CheckAndRequest(rp1.PERMISSION_BLUETOOTH_SCAN)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission...", False)
        Return
    End If
    Dim success As Boolean = Starter.Manager.SearchForDevices
    If success = False Then
        ToastMessageShow("Error starting discovery process.", True)
    Else
        ProgressDialogShow2("Suche nach MS Goethe...", False)
    End If
End Sub

Here is the new modul:

B4X:
Sub btnSearchForDevices_Click
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result = False And rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) = False Then
        ToastMessageShow("No permission...", False)
        Return
    End If
    If phone.SdkVersion >= 31 Then
        For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT")
            rp.CheckAndRequest(Permission)
            Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
            If Result = False Then
                ToastMessageShow("No permission...", False)
                Return
            End If
        Next
    End If
    Dim success As Boolean = admin.StartDiscovery
    If success = False Then
        ToastMessageShow("Error starting discovery process.", True)
    Else
        CLV.Clear
        Dim Index As Int = ShowProgress
        Wait For Admin_DiscoveryFinished
        HideProgress(Index)
        If CLV.Size = 0 Then
            ToastMessageShow("No device found.", True)
        End If
    End If
End Sub

As i see, the new example is for B4XPages, i am using the "normal" code
 

GMan

Well-Known Member
Licensed User
Longtime User
I decided to make the whole enchilada new:
Spent more time for trying to re-configure the old app than it takes to make it new
 
Top