Android Question BluetoothAdmin discovery started calls

SteveTerrell

Active Member
Licensed User
Longtime User
BA 3.82

I have:
Dim bAdmin As BluetoothAdmin
in a service. It is initialized as
bAdmin.Initialize("bAdmin") '

In my code I start a discovery with
B4X:
       If bAdmin.StartDiscovery = False Then
            Log("error starting discovery")
        Else
            btManager.LogGoToState(3)
        End If

I detect the results with
B4X:
Sub bAdmin_DiscoveryStarted
    Log("bAdmin_DiscoveryStarted sub")
    btManager.EventRun(Main.EVT_DiscoveryStarted,Null)
End Sub
Sub bAdmin_DiscoveryFinished
    Log("bAdmin_DiscoveryFinished sub")
    btManager.EventRun( Main.EVT_DiscoveryFinished,Null)
End Sub

btManager is my state machine helper class.

In my log I get:

15:35:56:783::main 4->5
Start discovery set true
ignoring event: badmin_discoverystarted
15:35:56:838::btManager 1->3
bAdmin_DiscoveryStarted sub
15:35:57:158::main 5->11
ignoring event: badmin_discoverystarted
bAdmin_DiscoveryStarted sub
ignoring event: badmin_discoveryfinished
bAdmin_DiscoveryFinished sub
15:36:09:441::btManager 4->1
15:36:09:673::main 11->4

This, and breakpoints, suggest that the bAdmin_DiscoveryStarted sub is called twice. There are also the three ignoring event messages in the log.

Is this as it should be?
 

SteveTerrell

Active Member
Licensed User
Longtime User

Yes

Here is a cut down version of the tutorial/my code

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4ABTTest
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Type NameAndMac (name As String, mac As String)
    Dim bAdmin As BluetoothAdmin
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub
Sub Activity_Create(FirstTime As Boolean)
    bAdmin.Initialize("bAdmin")                                    '
End Sub
Sub Activity_Resume
        If bAdmin.IsEnabled = False Then
            If bAdmin.Enable = False Then
                Log("Error enabling Bluetooth adapter.")
            Else
                Log("Enabling Bluetooth adapter...")
            End If
        Else
            Log(" Bluetooth adapter already enabled")
            bAdmin_StateChanged(bAdmin.STATE_ON)    ' pretend state has just changed to on
        End If

End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub bAdmin_StateChanged(newState As Int)
    Select newState
Case bAdmin.STATE_OFF
    Log("bt admin state change: STATE_OFF")

Case bAdmin.STATE_ON
    Log("bt admin state change: STATE_ON")
        Log("Start discovery set true")
        If bAdmin.StartDiscovery = False Then
            Log("error starting discovery")
        Else
            Log("starting discovery")
        End If
   
Case bAdmin.STATE_TURNING_OFF
    Log("bt admin state change: STATE_TURNING_OFF")

Case bAdmin.STATE_TURNING_ON
    Log("bt admin state change: STATE_TURNING_ON")
   
    End Select
   
End Sub
Sub bAdmin_DiscoveryStarted
    Log("bAdmin_DiscoveryStarted sub")
End Sub
Sub bAdmin_DiscoveryFinished
    Log("bAdmin_DiscoveryFinished sub")
End Sub
Sub btAdmin_DeviceFound(Name As String, MacAddress As String)
    Log("Device found"&Name & ":" & MacAddress)
End Sub

And here is the log

B4X:
Installing file.
PackageAdded: package:b4a.BTTest
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Bluetooth adapter already enabled
bt admin state change: STATE_ON
Start discovery set true
starting discovery
ignoring event: badmin_discoverystarted
bAdmin_DiscoveryStarted sub
ignoring event: badmin_discoverystarted
bAdmin_DiscoveryStarted sub
ignoring event: badmin_discoveryfinished
bAdmin_DiscoveryFinished sub

Note: There are no views in the example and no BT devices in range.
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
You should only initialize badmin when FirstTime is true.

Well spotted! I cut it down too much.

Fixed but doesn't seem to have changed anything though

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4ABTTest
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Type NameAndMac (name As String, mac As String)
    Dim bAdmin As BluetoothAdmin
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    bAdmin.Initialize("bAdmin")   
    End If
End Sub
Sub Activity_Resume
        If bAdmin.IsEnabled = False Then
            If bAdmin.Enable = False Then
                Log("Error enabling Bluetooth adapter.")
            Else
                Log("Enabling Bluetooth adapter...")
            End If
        Else
            Log(" Bluetooth adapter already enabled")
            bAdmin_StateChanged(bAdmin.STATE_ON)    ' pretend state has just changed to on
        End If

End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub bAdmin_StateChanged(newState As Int)
    Select newState
Case bAdmin.STATE_OFF
    Log("bt admin state change: STATE_OFF")

Case bAdmin.STATE_ON
    Log("bt admin state change: STATE_ON")
        Log("Start discovery set true")
        If bAdmin.StartDiscovery = False Then
            Log("error starting discovery")
        Else
            Log("starting discovery")
        End If
   
Case bAdmin.STATE_TURNING_OFF
    Log("bt admin state change: STATE_TURNING_OFF")

Case bAdmin.STATE_TURNING_ON
    Log("bt admin state change: STATE_TURNING_ON")
   
    End Select
   
End Sub
Sub bAdmin_DiscoveryStarted
    Log("bAdmin_DiscoveryStarted sub")
End Sub
Sub bAdmin_DiscoveryFinished
    Log("bAdmin_DiscoveryFinished sub")
End Sub
Sub btAdmin_DeviceFound(Name As String, MacAddress As String)
    Log("Device found"&Name & ":" & MacAddress)
End Sub

Log is still

B4X:
(Intent) Intent { act=android.intent.action.MAIN flg=0x20000000 cmp=b4a.BTTest/.main }
no extras
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Bluetooth adapter already enabled
bt admin state change: STATE_ON
Start discovery set true
starting discovery
ignoring event: badmin_discoverystarted
bAdmin_DiscoveryStarted sub
ignoring event: badmin_discoverystarted
bAdmin_DiscoveryStarted sub
ignoring event: badmin_discoveryfinished
bAdmin_DiscoveryFinished sub
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
I immediately got an error about a wrong signature. The sub signature should be:
B4X:
Sub badmin_StateChanged (NewState As Int, OldState As Int)
 
End Sub

Thanks again, change made - here is the log

B4X:
LogCat connected to: B4A-Bridge: asus Nexus 7-
--------- beginning of /dev/log/main
<qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
Enabling debug mode 0
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
GC_CONCURRENT freed 228K, 3% free 9222K/9484K, paused 2ms+3ms, total 20ms
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
GC_CONCURRENT freed 292K, 4% free 9317K/9644K, paused 3ms+5ms, total 31ms
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.BTTest
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Bluetooth adapter already enabled
bt admin state change: STATE_ON
Start discovery set true
ignoring event: badmin_discoverystarted
starting discovery
bAdmin_DiscoveryStarted sub
ignoring event: badmin_discoverystarted
bAdmin_DiscoveryStarted sub
ignoring event: badmin_discoveryfinished
bAdmin_DiscoveryFinished sub

That change does not seem to have afected the "ignoring" and double start issue.
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Not sure if this helps but i did notice that the "ignoring" message is always bAdmin_... even if I initialize my bAdmin to a different name,
such as

bAdmin.Initialize("btManager")

I had of course used the same prefix on my call back subs (Sub btManager_DiscoveryStarted etc.)

Regarding your reply #8, I had never seen that error reported - neither in the compilation warnings or at run time, is there some additional checking that I should activate?
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Thanks, glad we are getting there! Turning on the filter hides them nicely.

The second call to my discovery started sub is a bit of a confusion. Is that B4A-Bridge as well (so will not happen under normal circumstances)?
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Ok, I can handle it with my state machine.

It may be worth warning users of this double call where a B4A-Bridge event can run user code.

Originally i reset the device list on the discovery started sub call but the device found sub calls seem to only happen after the first discovery started call so i was building a list that was reset by the second call that came from B4A_Bridge.

Are there any other known cases where internal B4A-Bridge operations can cause user subs to be executed?
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Sorry i don't understand.

Turning on the filter correctly stopped the "ignoring" message, i can see that but of course it did not stop my log lines being printed.

The "bAdmin_DiscoveryStarted sub" lines in the log are from the Discovery started sub code.

B4X:
Sub bAdmin_DiscoveryStarted
    Log("bAdmin_DiscoveryStarted sub")
End Sub

This sub is called twice as shown in the log and confirmed by hitting a breakpoint on the log line two times.
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
I have modified it slightly:
my BluetoothAdmin is now called btAdmin so there is no confusion with bAdmin
I have added a counter so it is easy to see how many times discovery started is called

Here is the log (with no device in range and filter on)
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Bluetooth adapter already enabled
bt admin state change: STATE_ON
Start discovery set true, call count zeroed
starting discovery
btAdmin_DiscoveryStarted sub call number 1
btAdmin_DiscoveryStarted sub call number 2
btAdmin_DiscoveryFinished sub: call count = 2
** Activity (main) Resume **

With a SPP device in range the log is

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Bluetooth adapter already enabled
bt admin state change: STATE_ON
Start discovery set true, call count zeroed
starting discovery
btAdmin_DiscoveryStarted sub call number 1
btAdmin_DiscoveryStarted sub call number 2
Device foundBT UART:98:D3:31:20:0B:EC
btAdmin_DiscoveryFinished sub: call count = 2

Please note the device was found after the second sub call, looks like my memory in post 14 was at fault!
 

Attachments

  • b4abttest.zip
    6.4 KB · Views: 277
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is called once here:
B4X:
** Activity (main) Resume **
Enabling Bluetooth adapter...
bt admin state change: STATE_TURNING_ON
bt admin state change: STATE_ON
Start discovery set true, call count zeroed
starting discovery
btAdmin_DiscoveryStarted sub call number 1
Device foundUSER-PC:00:02:72:C5:DB:0F
btAdmin_DiscoveryFinished sub: call count = 1

The Bluetooth events are raised when the system sends the relevant intents. Seems like your device sends duplicate intents for some reason.
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Presumably that is why B4A-Bridge also has the two "ignoring discovery started" messages as well.

Just for info it is a Google Nexus 7 (2014) with Android 4.4.4.

Sorry to waste your time with a "Nexus" problem. I do not like unexplained events because they often cause problems just when you don't need.
I now know to make sure the code handles both one and two start discovery calls so thank you for your help.

Steve
 
Upvote 0
Top