B4J Question ABMaterial Class Instantiation

walterf25

Expert
Licensed User
Longtime User
Hi all, I need some help understanding how to use a standar B4J class in an ABMaterial project, basically I have a project where I have a written a class that opens a Serial Port and starts receiving data from a device, This all works perfect so far, until I navigate to a different page, the same class is instantiated, but the problem is that the previous page continues to receive serial data as well, In regular B4J and B4A I know this wouldn't be a problem because the previous pages or activities would be paused.

Is there a special way that classes work with ABMaterial framework that I may not be aware of?

This is causing an issue for me because for example if I'm in a different page I expect to receive specific data that can only be processed in the current page, but instead the previous page also continues receiving data and my whole data processing gets entirely thrown off.

Here's part of my code where the data is received and then sent to a sub which gets declared in each specific page.
Serial Receiver:
private Sub ProcessIncomingData(data() As Byte)
    Try
    If data <> Null Then
        Dim buffer() As Byte = data
        Log("buffer data lenght: " & buffer.Length)
        dataQueue.Append(buffer)
        Log("dataQueue lenght: " & dataQueue.Length)
    End If

    Log("processingdata = " & processingData)
    If (processingData = True) And (data <> Null) Then
        Log("processing data, returning......")
         Return
    End If
   
    Dim offset As Int = 0
    Dim dd() As Byte = dataQueue.SubArray2(ToUnsigned(offset), ToUnsigned(offset + 4))
    Dim dd2(dd.Length) As Int
    For i = 0 To dd.Length - 1
        dd2(i) = ToUnsigned(dd(i))
    Next
    Do While (dataQueue.Length > (offset+4)) And Not((isValidTLV(dd2)))
        offset = offset + 1
    Loop

    If offset > 0 Then
        dataQueue.Remove(0, offset)
    End If
   
    processingData = True
   
    If dataQueue.Length >= 4 Then
        Dim mode As Int = ToUnsigned(dataQueue.ToArray(0)) '''ToUnsigned(dataQueue.ToArray(0))
        Dim lenght As Int =ToUnsigned(dataQueue.ToArray(1)) + Bit.ShiftLeft(ToUnsigned(dataQueue.ToArray(2)), 8) + Bit.ShiftLeft(ToUnsigned(dataQueue.ToArray(3)), 16)
        Log("lenght1: " & lenght)
        If dataQueue.Length >= (lenght + 4) Then
            Dim payload() As Byte = dataQueue.SubArray2(4, (lenght + 4))
            If mode = 6 Then
            CallSubDelayed2(mCaller, mEventName&"_processdata", payload)   'These two subs
            else if mode = 0 Then
            CallSubDelayed2(mCaller, mEventName&"_pushmessage", payload)
            End If
            dataQueue.Remove(0, lenght + 4)
            ProcessIncomingData(Null)
        Else
            processingData = False
        End If
    Else
        '''dataQueue.Clear
        processingData = False
    End If
    Catch
        Log("error: " & LastException)
        processingData = False
    End Try
End Sub

And this is the Initialize Sub

B4X:
Public Sub Initialize(callback As Object, EventName As String)
    mCaller = callback
    mEventName = EventName
    mAstream.Initialize(Main.serial.GetInputStream, Main.serial.GetOutputStream, "Astream")
    dataQueue.Initialize
    rpc.Initialize(mEventName)
End Sub

To summarize, it seems like the class even after being instantiated in different pages, it makes the call to the wrong page, for example I have a page called nrfshell and another one called wifisettings, so the calls to the _processdata or _pushmessage get mixed up.

Any suggestions, I'm pretty sure I'm probably doing something wrong, but don't know what yet.

Thanks,
Walter
 

alwaysbusy

Expert
Licensed User
Longtime User
Is there a special way that classes work with ABMaterial framework that I may not be aware of?
There is a special way ABM caches pages (and classes attached to it) in case a user temporary loses his connection so ABM can try to recover from it.

You could try this in Websocket_Disconnected:
B4X:
ABM.RemoveMeFromCache(ABMShared.CachedPages, ABMPageId)
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
So I tried your suggestion but I am still seeing the raised subs from the class getting mixed up between the two pages.

I still think I must be missing something.

Any other ideas?

Walter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…