iOS Question Handling button clicks and then sending data and prefix mode

Tom2525

Member
Licensed User
Longtime User
This code appears to be the same for B4i and B4A. Not sure what's the best way to go about this. I have buttons on my app and each one represents a number. What I'd like to do is determine what button was pushed and then send the data. It seems like I must at least do this:

B4X:
sub button1_click
Buffer = 1
end sub

sub button2_click
Buffer = 2
end sub

sub button3_click
Buffer = 3
end sub

.....and so on. Instead of putting this in each sub:
B4X:
buffer = sNewLine.GetBytes("UTF8")
AStreams.Write(buffer)

Is there a way to handle the button pushes and put the code above in a separate area? I know I can put it here
B4X:
Sub Client_Connected(ConStatus As Boolean)
but sometimes more data will be sent without closing the connection so the client_connected sub work.

In verbose terms, I want to determine what button was pushed and then have one area where the data is sent.

Also, I have a question about prefix mode. Both side of the communication are controlled by my app and the instructions say to put a header with the amount of bytes in the packet. Is it as simple as:
B4X:
7"testing"

I looked at the example here:
https://www.b4x.com/android/forum/threads/asyncstreams-tutorial.7669/

but didn't see where to add the header.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should always try to avoid such code.
Set the EventName of all buttons to Button.
Set the number of each button as the tag property.

B4X:
Sub Button_Click
 Dim b As Button = Sender
 Dim number As Int = b.Tag
 AStreams.Write(...)
End Sub

You don't need to add the header. It is handled internally by AsyncStreams. You only need to manually handle the header if you are not using B4X.
 
Upvote 0

Tom2525

Member
Licensed User
Longtime User
I'm confused about a couple of things.

1.
You should always try to avoid such code.
What specifically do I need to avoid? Creating a sub for each button click?

2.
B4X:
Sub Button_Click
Dim b AsButton = Sender
Dim number As Int = b.Tag
AStreams.Write(...)End Sub
I should have mentioned that a string is sent as well. Here's exactly what I need to do as shown with the code I'm using now:

B4X:
Sub button1_click
SOCKET1.Initialize("client")
SOCKET1.Connect(ip, port, 500)
Dim buffer() As Byte
sNewLine = ("..1$$%*13") & CRLF
buffer = sNewLine.GetBytes("UTF8")
AStreams.Write(buffer)
End sub

Also, with regards to prefix mode, the information sent from the app to the server doesn't matter but the server isn't B4X. It's a microcontroller. If I understand you correctly, I don't need to do anything special when I send the data from the app but when I send data from the microcontroller to the app, I would only need to add a number in the beginning of the packet, which specifies the amount of bytes in the packet. Is that correct? Is this the correct way to initialize in prefix mode:
B4X:
AStreams.Initializeprefix(SOCKET1.InputStream,false,SOCKET1.OutputStream, "astreams")

It won't work in prefix mode but will in normal mode. At the start of my packet, which is being sent from the server to B4i, I'm sending a 4 byte array of 0017 (sending more than 17 bytes but wanted to shorten it to see if it would work). Is that the correct thing to do?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Creating a sub for each button click?
Yes.

Here's exactly what I need to do as shown with the code I'm using now:
This code will not work properly.

You need to call Socket1.Connect and then wait for the Connected event. Then you should initialize AsyncStreams.

The length is encoded as 4 bytes at the beginning of each message. If the length is 17 then it should be 0, 0, 0, 17.
 
Upvote 0

Tom2525

Member
Licensed User
Longtime User
It's still not working. I don't need to include the commas, do I? As an alternative, all packets sent can be terminated with any character. Is there something easier to use instead of prefix mode?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…