iOS Question About where BLE variables are placed

bagazhou

Member
Licensed User
Hi
I have a question . Where is BLE variables definition ?

1. place BLE variables in "Application_Start" , but can not raised StateChanged event
2. place BLE variables in "Process_Globals", all is ok

B4X:
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page


    Dim maps As Map
    Private ConnectedName As String
    Public ConnectedId As String
    Private ConnectedServices As List
    Public currentStateText As String = "UNKNOWN"
    Public currentState As Int
    Public Connected As Boolean = False
End Sub


Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    NavControl.NavigationBarVisible = False
    NavControl.ToolBarVisible = False


'    definition variable is here , can not raised StateChanged event
    Dim manager1 As BleManager
    Dim manager2 As BleManager
    Dim manager3 As BleManager
    manager1.Initialize("manager")
    manager2.Initialize("manager")
    manager3.Initialize("manager")

End Sub


'------------------------------------------------------------
' BLE Part
'------------------------------------------------------------
Sub Manager_StateChanged (State As Int)
    Log("-------------- Manager_StateChanged ---------------")
    Dim m As BleManager
    m = Sender
    LogColor("sender:" & Sender, Colors.Yellow)
    LogColor("m:" & m, Colors.Magenta)

End Sub


#if OBJC
#import <CoreBluetooth/CoreBluetooth.h>
#End If
 

Filippo

Expert
Licensed User
Hi
I have a question . Where is BLE variables definition ?

1. place BLE variables in "Application_Start" , but can not raised StateChanged event
2. place BLE variables in "Process_Globals", all is ok

B4X:
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page


    Dim maps As Map
    Private ConnectedName As String
    Public ConnectedId As String
    Private ConnectedServices As List
    Public currentStateText As String = "UNKNOWN"
    Public currentState As Int
    Public Connected As Boolean = False
End Sub


Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    NavControl.NavigationBarVisible = False
    NavControl.ToolBarVisible = False


'    definition variable is here , can not raised StateChanged event
    Dim manager1 As BleManager
    Dim manager2 As BleManager
    Dim manager3 As BleManager
    manager1.Initialize("manager")
    manager2.Initialize("manager")
    manager3.Initialize("manager")

End Sub


'------------------------------------------------------------
' BLE Part
'------------------------------------------------------------
Sub Manager_StateChanged (State As Int)
    Log("-------------- Manager_StateChanged ---------------")
    Dim m As BleManager
    m = Sender
    LogColor("sender:" & Sender, Colors.Yellow)
    LogColor("m:" & m, Colors.Magenta)

End Sub


#if OBJC
#import <CoreBluetooth/CoreBluetooth.h>
#End If

Please try like this:
B4X:
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page

'    definition variable is here , can not raised StateChanged event
    Private manager1 As BleManager

    Dim maps As Map
    Private ConnectedName As String
    Public ConnectedId As String
    Private ConnectedServices As List
    Public currentStateText As String = "UNKNOWN"
    Public currentState As Int
    Public Connected As Boolean = False
End Sub


Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    NavControl.NavigationBarVisible = False
    NavControl.ToolBarVisible = False

    manager1.Initialize("manager")
    btManager.Scan(Null)

End Sub


'------------------------------------------------------------
' BLE Part
'------------------------------------------------------------
Sub Manager_StateChanged (State As Int)
    Log("State=" & State )
End Sub
 
Upvote 0

bagazhou

Member
Licensed User
Thank you for your reply !
Because I need to connect multiple BLE devices(use multiple BleManager)
If so, the user connects to a new BLE device by pressing the Onclick button , can't use the following code ?

B4X:
Sub btnBleNew_Click
    Dim blemanager As BleManager
    blemanager.Initialize("manager")
End Sub
 
Last edited:
Upvote 0

Filippo

Expert
Licensed User
If I were you, I would implement the whole thing in a class. So each user can instantiate a new classe.
 
Upvote 0
Top