Android Question Result of bluetooth REQUEST_DISCOVERABLE

iz0ndg

Active Member
Licensed User
Longtime User
Hi, I have an App with Bluetooth connection to another device.
To listen to the bluetooth I have this piece of code, from the Examples of Erel:
B4X:
Public Sub ListenForConnections
   'this intent makes the device discoverable for 300 seconds.
   Dim i As Intent
   i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
   i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
   StartActivity(i)
   serial.Listen
End Sub

How do I know if the user has refused permission?

Thanks for your help
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Relevant code:
B4X:
Sub btnAllowConnection_Click
   Dim i As Intent
   i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
   i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
   StartActivityForResult(i)
   Wait For ion_Event (MethodName As String, Args() As Object)
   If Args(0) > 0 Then 'resultCode = RESULT_OK
       Log("Discovery allowed")
       Starter.Manager.ListenForConnections
   Else
       Log("Discovery denied")
   End If
End Sub

Sub StartActivityForResult(i As Intent)
   Dim jo As JavaObject = GetBA
   ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
   jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
   Dim jo As JavaObject
   Dim cls As String = Me
   cls = cls.SubString("class ".Length)
   jo.InitializeStatic(cls)
   Return jo.GetField("processBA")
End Sub

Updated Bluetooth example with this code is attached.
The other change is to remove the intent from Manager.ListForConnections.
 

Attachments

  • Bluetooth.zip
    12.7 KB · Views: 248
Upvote 0
Top