Android Question Sending data from app to another how?

camolas

Member
Licensed User
Longtime User
Hi,
Im using a python scrit to read incoming sms from a usb gsm modem ( for now i cant put to work in b4a ) in the py i have this to creat a " tunel"
B4X:
import sys
from socket import socket, AF_INET, SOCK_DGRAM

SERVER_IP   = '127.0.0.1'
PORT_NUMBER = 5000
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))

mySocket = socket( AF_INET, SOCK_DGRAM )

while True:
        mySocket.sendto('cool',(SERVER_IP,PORT_NUMBER))
sys.exit()

How can i "catch" the data to a b4a and display it?
Thankz
 
Last edited:

camolas

Member
Licensed User
Longtime User
Hi,

Im very new to android and b4a i try with some examples from you but no sucess, can you please macke a example? please please

Thankz
 
Last edited:
Upvote 0

camolas

Member
Licensed User
Longtime User
Im but was i say i cant put it to work, im on this for five days and nothing... i ascke please give me example to work with my py code please..
 
Upvote 0

camolas

Member
Licensed User
Longtime User
Hi,
I connect with the device using pyserial from ttyUSB2 port to the usb gsm modem and i read if there is any sms ( works fine) then if there is one i creat the "tunel" on
SERVER_IP = '127.0.0.1'PORT_NUMBER = 5000 as the example at the start off this topic, can read ok with this py script
B4X:
#
#receiver py 
#

from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )

print ("Test server listening on port {0}\n".format(PORT_NUMBER))

while True:
        (data,addr) = mySocket.recvfrom(SIZE)
        print data
sys.ext()
the two scripts work fine in android, now i need to macke a b4a app replacing the receiver py script please give me a and....

Thankz
 
Upvote 0

camolas

Member
Licensed User
Longtime User
Is the Network tutorial for read data from one app to another in same android device or for pc to android?
What i need is a b4a server client to work with my py scrit (py scipt is runing in my tablet and the b4a app client is for runing in the same tablet to get data from py..)

Thankz
 
Last edited:
Upvote 0

camolas

Member
Licensed User
Longtime User
ok i have done a lot o testing... and cant get it!!!

Example from Network tutorial:

B4X:
Sub Process_Globals
    Dim Socket1 As Socket
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Socket1.Initialize("Socket1")
    Socket1.Connect("nist1-ny.ustiming.org" , 13, 20000)
End Sub

Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If
    Dim tr As TextReader
    tr.Initialize(Socket1.InputStream)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append(tr.ReadLine) 'read at least one line
    Do While tr.Ready
        sb.Append(CRLF).Append(tr.ReadLine)
    Loop
    Msgbox("Time received: " & CRLF & sb.ToString, "")
    Socket1.Close
End Sub

I change the "Socket1.Connect("nist1-ny.ustiming.org" , 13, 20000)" in
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Socket1.Initialize("Socket1")
    Socket1.Connect("nist1-ny.ustiming.org" , 13, 20000)
End Sub

for "Socket1.Connect("127.0.0.1" , 5000, 20000)" dont work then for "Socket1.Connect("127.0.0.1" , 13, 5000)" i get in both changes this error "Error conecting - libcore.io.ErrnoExcepction: isConnected failed: ECONNREFUSED (Conection refused)" :confused:

I can see the py script runing from SL4A Service i get the time that service is runing 29min the ip 127.0.0.1:39264 and pid 3404

Please give a and on this im geting crazy...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
127.0.0.1 is localhost... The device running that code... So you want to connect to a server running on your device???
 
Upvote 0

camolas

Member
Licensed User
Longtime User
Hi,
I want to connect in the same device a app the reads data from localhost 127.0.0.1 (my py script)
Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hi,
I want to connect in the same device a app the reads data from localhost 127.0.0.1 (my py script)
Thanks

Your py-script is running on your device?
127.0.0.1 is localhost. It works when your py is running on your pc and you call this ip from a browser on your PC...

But you cannot use this ip from your device to connect to your pc!!!
 
Upvote 0

camolas

Member
Licensed User
Longtime User
Hi,

Yes my py script is running on my device and (from sl4a) and i need to read in the same device what the py sends... any ideas?
Thx
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i have NO plan from python.
I just know that 127.0.0.1 cannot be used from an other device to connect to my device. the other device should use the internet-ip from the servers device. And the serverside (py) must listen to requests on the internet-ip too.
 
Upvote 0

camolas

Member
Licensed User
Longtime User
Hi,

Using TestClientSocket and changing the ip to "127.0.0.1" and port to "port = 5000" (for my py sever in above) i get this error "Error conecting - libcore.io.ErrnoExcepction: isConnected failed: ECONNREFUSED (Conection refused)"

B4X:
'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim CltSock As Socket
    Dim Astreams As AsyncStreams
    ' If you install the "SocketServer" on local machine - change this ip to "127.0.0.1"
    'Dim ip As String : ip = "192.168.0.13"
    Dim ip As String : ip = "127.0.0.1"
  
    'Dim port As Int: port = 9092
    Dim port As Int: port = 5000
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim btn_client As Button
    Dim lbl_status As Label
    Dim EditText1 As EditText
    Dim txt_out As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("ClientForm")
    EditText1.Text = ip
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
  
    lbl_status.Text = msg
  
End Sub

Sub btn_client_Click
    CltSock.Initialize("Client")
    CltSock.Connect(ip,port,20000)
  
End Sub

Sub Client_Connected(ConStatus As Boolean)
    If ConStatus = True Then
        Msgbox("Connection Successful","")
        Astreams.InitializePrefix(CltSock.InputStream, False, CltSock.OutputStream, "AStreams")
    Else
        Msgbox(LastException.Message, "Error connecting")
      
    End If  

End Sub

Sub EditText1_EnterPressed
    ip = EditText1.Text
End Sub
Sub txt_out_EnterPressed
     If Astreams.IsInitialized = False Then Return
    If txt_out.Text.Length > 0 Then
        Dim sNewLine As String
        sNewLine = txt_out.text & CRLF
        Dim buffer() As Byte
        buffer = sNewLine.GetBytes("UTF8")
        Astreams.Write(buffer)
      
          ToastMessageShow("Sending:" & sNewLine,False)
    End If
End Sub

Please help me

Thx
 
Upvote 0
Top