Android Question Diffrent Activites

Arturs

Member
Licensed User
Longtime User
Hi

In main activity I initialize ast

B4X:
Sub Process_Globals
Dim ast As AsyncStreamsText
End Sub

In Main Activity I have the following Sub

B4X:
Sub ast_NewText(Text As String)
           
End Sub

The Sub is called when the new string was recived from BT.

I created second Activity and I would like to use the same Sub in second Activity.
It should be called when new string was received and I am in second activity.

All advice how to do would be appreciated

Regards
Artur
 

klaus

Expert
Licensed User
Longtime User
Should it work with event too ?
If you call the routine in an event routine no problem.
As said before you can call it from anywhere !
Then it may depend on what you want to do in the routine ast_NewText !
But as you don't show the code in this routine its impossible to give a concrete advice.
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
Ok.

I created modules accroding to your advice

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim ast As AsyncStreamsText

End Sub

Public Sub test(Text As String)
   
Log(Text)

End Sub

Public Sub ast_NewText(Text As String)


Log("Received")

End Sub

In main activity I created button event

B4X:
Sub test_but_Click

Common.test("Artur")

End Sub

It works perfectly in test case.


If I try to do it with Public Sub ast_NewText(Text As String) it does not work.

All the time my external device send to my mobile phone (String "TEST " &CRLF) but event ast_NewText(Text As String) it is not called. I am sure that mobile phone receives it.

I downloaded Class AsyncStreamsText.bas
from the link.
https://www.b4x.com/android/forum/t...eful-when-working-with-streams-of-text.27002/

It was published by Erel.
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
I do not call it. It should be called automaticaly when a string is received

I think the sub calls it

B4X:
Private Sub astreams_NewData (Buffer() As Byte)
    Dim newDataStart As Int = sb.Length
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
    Dim s As String = sb.ToString
    Dim start As Int = 0
    For i = newDataStart To s.Length - 1
        Dim c As Char = s.CharAt(i)
            If i = 0 AND c = Chr(10) Then '\n...
            start = 1 'might be a broken end of line character
            Continue
        End If
        If c = Chr(10) Then '\n
            CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
            start = i + 1
        Else If c = Chr(13) Then '\r
            CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
            If i < s.Length - 1 AND s.CharAt(i + 1) = Chr(10) Then '\r\n
                i = i + 1
            End If
            start = i + 1
        End If
    Next
    If start > 0 Then sb.Remove(0, start)
End Sub

It is piece of code of AsyncStreamsText.bas written by Erel
 
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
What are the solutions ?
Initialize ast in each activity
I am also stuck in a similar situation. I use RDC.

As of now, I initialise RDC in each activity. RDC.initialize(me, RDC_IP ). I actually don't know whether it is expected to be coded so ie initialise separate RDC objects on each activity module (as there is a call back module specified along with the initialize ie 'me' )

As of now, it is working fine, but if an error happens in RDC of any activity then RDC initialized separately on each activity also fails. Haven't figured out why it is so.

May be CallSubDelayed a solution to your requirement.
 
Last edited:
Upvote 0

Arturs

Member
Licensed User
Longtime User
My main problem is:

I use the following library:

- BluetoothAdmin
- Serial

and the below class
-AsyncStreamsText

I have three activites where I need to use serial,AsyncStreamsText,BT

If I use it in Main Activity everything is ok

B4X:
Sub Process_Globals
  'These global variables will be declared once when the application starts.
  'These variables can be accessed from all modules.
  Dim admin As BluetoothAdmin
  Dim serial1 As Serial
  Dim ast As AsyncStreamsText
End Sub

Unfortunately I do not know how to manage serial,AsyncStreamsText,BluetoothAdmin in other activities

- Initialize everything once again in each of them (but what to do with established connection in Main Activity)
- create service module but how to manage it


I have been looking for an example on the forum but still without success.


Regards
Artur
 
Last edited:
Upvote 0
Top