Newbie question on Events

Vader

Well-Known Member
Licensed User
Longtime User
Ok, so I am using the serial library. Version 1.21.

Port is an instance of Serial. PairedMACAddress is a string containing the MAC Address to connect to (e.g. "00:12:06:01:53:65")

I have a sub where I connect to a specific MAC Address:
B4X:
Private Sub ConnectToPairedDevice
   If PairedMACAddress <> "" Then
      Log("Connecting to specified device")
      Port.Connect(PairedMACAddress) 'Attempt Connection
   Else
   End If
End Sub

I have another Sub where I want to process the connection state:
B4X:
Private Sub Port_Connected (Success As Boolean)
   Log("Connection attempt:" & Success)
End Sub

The problem is that the Port_Connected Sub is never entered.

What am I doing wrong?
 

Vader

Well-Known Member
Licensed User
Longtime User
B4X:
'Activity module
Public Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules (Public).
   
   Public Port As Serial
 
Upvote 0

Vader

Well-Known Member
Licensed User
Longtime User
Sorry. It is just in a generic Sub, called from Activity_Create. Should it be in one of Process_Globals or Globals?
 
Upvote 0

Vader

Well-Known Member
Licensed User
Longtime User
B4X:
Public Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules (Public).
   
   Public Port As Serial

B4X:
Public Sub Activity_Create(FirstTime As Boolean)
   Private PhoneInfo As Phone
   Private Model As String = PhoneInfo.Model.ToLowerCase
   
   Activity.LoadLayout("Test") ' Form.Load(Test)
      

   
   If FirstTime Then
      RestoreApplicationState
      InitialiseSerialPort
      GetPairedMACAddress

B4X:
Private Sub RestoreApplicationState
   'Log("State about to be restored...")
   If StateManager.RestoreState(Activity, "Main", 60) = False Then        
      'Set the default values            
   End If
End Sub

Private Sub InitialiseSerialPort
   ' Initialise the Serial port
   Try
      Port.Initialize("Port_Connected")
   Catch
      Log("Error initialising Serial port")
   End Try
End Sub

Private Sub GetPairedMACAddress
   PairedMACAddress = StateManager.GetSetting("PairedMACAddress")
   Log("Retrieved the following pairing: " & PairedMACAddress)
End Sub

B4X:
Private Sub ConnectToPairedDevice
   If PairedMACAddress <> "" Then
      Log("Connecting to specified device")
      Port.Connect(PairedMACAddress) 'Attempt Connection
   End If
End Sub

Private Sub Port_Connected (Success As Boolean)
   Log("Connection attempt:" & Success)
End Sub

Log:
** Activity (main) Create, isFirst = true **
STATE loaded
Retrieved the following pairing: 00:12:06:01:53:65
Bluetooth is enabled.
Connecting to specified device
** Activity (main) Resume **
 
Upvote 0

Vader

Well-Known Member
Licensed User
Longtime User
Oops. you may not have enough info. Here's the whole Activity_Create ...

B4X:
Public Sub Activity_Create(FirstTime As Boolean)
   Private PhoneInfo As Phone
   Private Model As String = PhoneInfo.Model.ToLowerCase
   
   Activity.LoadLayout("Test") ' Form.Load(Test)
   
   lblSerial.Text = "Hello World"
   
   ' *****************************************************************************
   ' Licensing
   If Model = "sdk" Then
      lc_Allow
   Else
      'CheckLicense
   End If
   ' *****************************************************************************
   
   If FirstTime Then
      RestoreApplicationState
      InitialiseSerialPort
      GetPairedMACAddress
      
      
      If Port.IsEnabled Then
         Log("Bluetooth is enabled.")
         
         If PairedMACAddress = "" Then
            ListPairedDevices
         End If
         
         Try
            ConnectToPairedDevice
         Catch
            Log("***** ERROR connecting to device *****")
         End Try
         
      Else
         Log("Bluetooth not enabled.")
         ToastMessageShow("Enable Bluetooth and restart application.  Closing application.", True)
         Activity.Finish
      End If
      
   End If
   
End Sub
 
Upvote 0

Vader

Well-Known Member
Licensed User
Longtime User
Ok, so that works.

The argument for Port.Initialize() is "Eventname".
Maybe it should be "InstanceName" as per the following signature:

Port.Initialize(InstanceName as string)

Isn't it strange that the object needs to be told it's own name to initialise it?

Anyway, thanks Erel.
 
Upvote 0
Top