Android Question show different name of value in spinner keeping value

hi, i have created an app to turn off a relay.
I have a problem, I need a spinner to be able to put a display name and run a different one.
I explain, I have two activities, one is called settings and the other is main.
In configuration I put the ip addresses to which I execute a command. The first two IP addresses are the ones that I can select later in the spinner, to execute the command to one or the other.
The problem is that the ip addresses are very similar and there are times that I make a mistake when selecting them, I need the name shown in the spinner to be a different one than the value it actually has

Settings:
Settings.jpg


Main:

Main.png



Main Spinner:


Main Spinner.png



I need a text to be displayed instead of that ip (such as my name), but the value should really be the ip

Thanks :)
 

josejad

Expert
Licensed User
Longtime User
Using a map:


i.e.:
B4X:
Dim MapRelays As Map
MapRelay.Initialize 
MapRelay.Put("Relay1", "12.26.0.206")
MapRelay.Put("Relay2", "12.16.0.206")

For Each relay As String In MapRelays.Keys
  Log(k)
 'Populate your Spinner
Spinner1.Add(relay) 
Next

After that, when you get the SelectedItem, you can search for it in the map, and get the IP

With B4XPreferencesDialog, see the examples and videos. You can use a PreferencesDialog better than a Spinner using the "options" type.

Try it and ask any question you have.
 
Upvote 0
@José J. Aguilar
the point is that the ip addresses may vary depending on what you put in the settings.
In the example you have defined some IPs that are always the same

in spanish:
te lo pongo en español pq he visto que eres de sevilla
el tema es que las direcciones ip pueden variar dependiendo de lo que ponga en el archivo settings.
en el ejemplo has definido unas ip que siempre son las mismas
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Ok, so it depends on the way you save the ip addresses in the config activity: a file, sqlite, kvs?

Post in english here, there’s a specific spanish forum.

Si señor, ando por Sevilla. De dónde eres tú? Intenta también no preguntar a alguien en concreto (en este caso a mi) para que así otros se sientan libres de contestarte.

saludos,
 
Upvote 0
a file, this is my settings file.
I dont know if its the best code but it work

Settings:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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 EditText1 As EditText
    Dim EditText2 As EditText
    Dim EditText3 As EditText
    Dim EditText4 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Settings")
    EditText1.Text = Main.HostName1  
    EditText2.Text = Main.HostName2
    EditText3.Text = Main.HostName3
    EditText4.Text = Main.HostName4
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button2_Click
    Activity.Finish
End Sub

Sub Button1_Click
    Dim s As List

    Main.HostName1 = EditText1.Text
    Main.HostName2 = EditText2.Text
    Main.HostName3 = EditText3.Text
    Main.HostName4 = EditText4.Text

    s.Initialize
    s.Add(Main.HostName1)
    s.Add(Main.HostName2)
    s.Add(Main.HostName3)
    s.Add(Main.HostName4)

    File.WriteList(File.DirInternal, "AppSettings", s)
    Activity.Finish
End Sub



and this is my main file

main:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim HostName1 As String
    Dim HostName2 As String
    Dim HostName3 As String
    Dim HostName4 As String
    Dim ViewExtChs As Boolean
    Dim Timer1 As Timer
    Dim sock As Socket
    Dim AStreams As AsyncStreams
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 ImageView1 As ImageView
    Dim ImageView2 As ImageView
    Dim ImageView3 As ImageView
    Dim ImageView4 As ImageView
    Dim Label1 As Label
    Dim Label2 As Label
    Dim Label3 As Label
    Dim Label5 As Label
    Dim Panel1 As Panel
    Dim sockBusy As Boolean
    Dim busyTime As Long
    Dim ModuleConnected As Boolean
    Dim CommandBuff As String
    Dim NewState As String
    Dim StateBuff As String
    Dim Spinner1 As Spinner
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Dim s As List
   
    Activity.LoadLayout("Main")
    Activity.AddMenuItem("Configuracion", "mnuSettings")
   
    If FirstTime Then
       
        If File.Exists(File.DirInternal, "AppSettings") Then
            s = File.ReadList(File.DirInternal, "AppSettings")
            HostName1 = s.Get(0)
            HostName2 = s.Get(1)
            HostName3 = s.Get(2)
            HostName4 = s.Get(3)
        Else
            HostName1 = "172.26.0.206"
            HostName2 = "172.16.0.206"
            HostName3 = "172.26.0.6"
            HostName4 = "172.16.0.6"
        End If
   
        Timer1.Initialize("Timer1", 5000)
    End If
    Spinner1.Add(HostName1)
    Spinner1.Add(HostName2)
End Sub

Sub Activity_Resume
    Spinner1.Clear
    Spinner1.Add(HostName1)
    Spinner1.Add(HostName2)
    Timer1.Enabled = True
    ModuleConnected = False
   
    Panel1.Visible = False

    Timer1_Tick
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Timer1.Enabled = False
   
    If sockBusy Then
        If AStreams.IsInitialized Then
            AStreams.Close
        End If
       
        If sock.IsInitialized Then
            sock.Close
        End If
        sockBusy = False
    End If          
End Sub


Sub Timer1_Tick
    If sockBusy Then
        If sock.Connected Then
            If DateTime.Now - busyTime > DateTime.TicksPerSecond * 10 Then
                If AStreams.IsInitialized Then
                    AStreams.Close
                End If
       
                sock.Close
                sockBusy = False
            End If
        End If
    Else
        Timer1.Interval = 1000
   
        sockBusy = True
        sock.Initialize("sock")
        sock.Connect(Spinner1.SelectedItem, 6722, 10000)
        busyTime = DateTime.Now
    End If
End Sub

Sub sock_Connected (Successful As Boolean)
    If Successful Then
        ModuleConnected = True
        AStreams.Initialize(sock.InputStream, sock.OutputStream, "AStreams")
        If CommandBuff = "" Then
            AStreams.Write("00".GetBytes("ASCII"))
        Else
            AStreams.Write(CommandBuff.GetBytes("ASCII"))
            CommandBuff = ""
        End If
    Else
        SetModuleOffline
    End If
End Sub

Sub AStreams_Error
    AStreams.Close
    SetModuleOffline
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    NewState = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
    AStreams.Close
    ParseModuleState
    sock.Close  
    Panel1.Visible = False
   
    sockBusy = False
End Sub

Sub mnuSettings_Click
    StartActivity("Settings")  
End Sub

Sub SetModuleOffline
    ModuleConnected = False
    StateBuff = ""
   
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_disable.png")
    ImageView1.Invalidate
    ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_disable.png")  
    ImageView2.Invalidate
   
    ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")  
    ImageView3.Invalidate
    ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")  
    ImageView4.Invalidate
   
    Panel1.Visible = False
   
   
    sock.Close
    sockBusy = False
End Sub



Sub ParseModuleState
    If StateBuff = "" Or NewState.CharAt(0) <> StateBuff.CharAt(0) Then
        If NewState.CharAt(0) = "1" Then
            ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView1.Invalidate
    End If
   
    If StateBuff = "" Or NewState.CharAt(1) <> StateBuff.CharAt(1) Then
        If NewState.CharAt(1) = "1" Then
            ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView2.Invalidate
    End If
   
    If StateBuff = "" Or NewState.CharAt(2) <> StateBuff.CharAt(2) Then
        If NewState.CharAt(2) = "1" Then
            ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView3.Invalidate
    End If

    If StateBuff = "" Or NewState.CharAt(3) <> StateBuff.CharAt(3) Then
        If NewState.CharAt(3) = "1" Then
            ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView4.Invalidate
    End If

   
    StateBuff = NewState
End Sub

Sub ImageView1_Click
    If ModuleConnected Then
        Panel1.Visible = True

       
        If StateBuff.CharAt(0) = "0" Then
            CommandBuff = "11"
        Else
            CommandBuff = "21"
        End If      
       
        Timer1.Interval = 250
    End If
End Sub

Sub ImageView2_Click
    If ModuleConnected Then
        Panel1.Visible = True
       
        If StateBuff.CharAt(1) = "0" Then
            CommandBuff = "12*"
        Else
            CommandBuff = "22*"
        End If      
       
        Timer1.Interval = 250
    End If  
End Sub

Sub ImageView3_Click
    StartActivity("Ping1")
End Sub

Sub ImageView4_Click
    StartActivity("Ping2")
End Sub

soy de Cáceres jejeje, un placer verte por aquí, sé que hay foro en español pero también ví que no hay apenas actividad
I'm from Cáceres hehehe, a pleasure to see you here, I know there is a forum in Spanish but I also saw that there is hardly any activity
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
One way of doing it could be with the map as in my previous post. Just add the hostnames to the map after your get them.

i.e., add in the line #56
B4X:
For i = 0 To s.Size - 1
   MapRelay.Put("Hostname" & i , s.Get(i))
Next
 
Upvote 0
In this code, appear me an error that say something about k, i think something like k variable or something...
In line Log(k)

B4X:
Dim MapRelays As Map
MapRelay.Initialize
MapRelay.Put("Relay1", "12.26.0.206")
MapRelay.Put("Relay2", "12.16.0.206")

For Each relay As String In MapRelays.Keys
  Log(k)
 'Populate your Spinner
Spinner1.Add(relay)
Next
 
Upvote 0
I can't get it to work...
At the moment I have this, I do not know if I have done it correctly, it is a function that I had never used before and I do not know how to use
i put it all on Activity_Create on line #63


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim HostName1 As String
    Dim HostName2 As String
    Dim HostName3 As String
    Dim HostName4 As String
    Dim ViewExtChs As Boolean
    Dim Timer1 As Timer
    Dim sock As Socket
    Dim AStreams As AsyncStreams
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 ImageView1 As ImageView
    Dim ImageView2 As ImageView
    Dim ImageView3 As ImageView
    Dim ImageView4 As ImageView
    Dim Label1 As Label
    Dim Label2 As Label
    Dim Label3 As Label
    Dim Label4 As Label
    Dim Panel1 As Panel
    Dim sockBusy As Boolean
    Dim busyTime As Long
    Dim ModuleConnected As Boolean
    Dim CommandBuff As String
    Dim NewState As String
    Dim StateBuff As String
    Dim Spinner1 As Spinner
    Dim MapRelay As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Dim s As List
   
    Activity.LoadLayout("Main")
    Activity.AddMenuItem("Configuracion", "mnuSettings")
    
 

    If FirstTime Then
       
        If File.Exists(File.DirInternal, "AppSettings") Then
            s = File.ReadList(File.DirInternal, "AppSettings")
            HostName1 = s.Get(0)
            HostName2 = s.Get(1)
            HostName3 = s.Get(2)
            HostName4 = s.Get(3)
        Else
            HostName1 = "172.26.0.206"
            HostName2 = "172.16.0.206"
            HostName3 = "172.26.0.6"
            HostName4 = "172.16.0.6"
        End If
       
        Timer1.Initialize("Timer1", 5000)
    End If
   
    MapRelay.Initialize
    MapRelay.Put("Relay1", "12.26.0.206")
    MapRelay.Put("Relay2", "12.16.0.206")

    For Each relay As String In MapRelay.Keys
        Log(relay)
        'Populate your Spinner
        Spinner1.Add(relay)
       
    Next
    For i = 0 To s.Size - 1
        MapRelay.Put("HostName" & i , s.Get(i))
    Next
   
   
End Sub

Sub Activity_Resume
    Spinner1.Clear
    'Spinner1.Add(HostName1)
    'Spinner1.Add(HostName2)
    Timer1.Enabled = True
    ModuleConnected = False
   
    Panel1.Visible = False

    Timer1_Tick
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Timer1.Enabled = False
   
    If sockBusy Then
        If AStreams.IsInitialized Then
            AStreams.Close
        End If
       
        If sock.IsInitialized Then
            sock.Close
        End If
        sockBusy = False
    End If          
End Sub


Sub Timer1_Tick
    If sockBusy Then
        If sock.Connected Then
            If DateTime.Now - busyTime > DateTime.TicksPerSecond * 10 Then
                If AStreams.IsInitialized Then
                    AStreams.Close
                End If
       
                sock.Close
                sockBusy = False
            End If
        End If
    Else
        Timer1.Interval = 1000
   
        sockBusy = True
        sock.Initialize("sock")
        sock.Connect(Spinner1.SelectedItem, 6722, 10000)
        busyTime = DateTime.Now
    End If
End Sub

Sub sock_Connected (Successful As Boolean)
    If Successful Then
        ModuleConnected = True
        AStreams.Initialize(sock.InputStream, sock.OutputStream, "AStreams")
        If CommandBuff = "" Then
            AStreams.Write("00".GetBytes("ASCII"))
        Else
            AStreams.Write(CommandBuff.GetBytes("ASCII"))
            CommandBuff = ""
        End If
    Else
        SetModuleOffline
    End If
End Sub

Sub AStreams_Error
    AStreams.Close
    SetModuleOffline
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    NewState = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
    AStreams.Close
    ParseModuleState
    sock.Close  
    Panel1.Visible = False
   
    sockBusy = False
End Sub

Sub mnuSettings_Click
    StartActivity("Settings")  
End Sub


Sub SetModuleOffline
    ModuleConnected = False
    StateBuff = ""
   
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_disable.png")
    ImageView1.Invalidate
    ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_disable.png")  
    ImageView2.Invalidate
   
    ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")  
    ImageView3.Invalidate
    ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")  
    ImageView4.Invalidate
   
    Panel1.Visible = False
   
   
    sock.Close
    sockBusy = False
End Sub



Sub ParseModuleState
    If StateBuff = "" Or NewState.CharAt(0) <> StateBuff.CharAt(0) Then
        If NewState.CharAt(0) = "1" Then
            ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView1.Invalidate
    End If
   
    If StateBuff = "" Or NewState.CharAt(1) <> StateBuff.CharAt(1) Then
        If NewState.CharAt(1) = "1" Then
            ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView2.Invalidate
    End If
   
    If StateBuff = "" Or NewState.CharAt(2) <> StateBuff.CharAt(2) Then
        If NewState.CharAt(2) = "1" Then
            ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView3.Invalidate
    End If

    If StateBuff = "" Or NewState.CharAt(3) <> StateBuff.CharAt(3) Then
        If NewState.CharAt(3) = "1" Then
            ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView4.Invalidate
    End If

   
    StateBuff = NewState
End Sub

Sub ImageView1_Click
    If ModuleConnected Then
        Panel1.Visible = True

       
        If StateBuff.CharAt(0) = "0" Then
            CommandBuff = "11"
        Else
            CommandBuff = "21"
        End If      
       
        Timer1.Interval = 250
    End If
End Sub

Sub ImageView2_Click
    If ModuleConnected Then
        Panel1.Visible = True
       
        If StateBuff.CharAt(1) = "0" Then
            CommandBuff = "12*"
        Else
            CommandBuff = "22*"
        End If      
       
        Timer1.Interval = 250
    End If  
End Sub

Sub ImageView3_Click
    StartActivity("Ping1")
End Sub

Sub ImageView4_Click
    StartActivity("Ping2")
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Ok, let's see.

B4X:
Sub Process_Globals
    ...
    Dim HostName1 As String
    Dim HostName2 As String
    Dim HostName3 As String
    Dim HostName4 As String
    ...
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Dim s As List
    ....   
    If FirstTime Then
        If File.Exists(File.DirInternal, "AppSettings") Then
            s = File.ReadList(File.DirInternal, "AppSettings")
            HostName1 = s.Get(0)
            HostName2 = s.Get(1)
            HostName3 = s.Get(2)
            HostName4 = s.Get(3)
        Else
            HostName1 = "172.26.0.206"
            HostName2 = "172.16.0.206"
            HostName3 = "172.26.0.6"
            HostName4 = "172.16.0.6"
        End If

       '**********Here you got your hostnames set in their variables, now we put in a map the host name (HostNameX) with their ip address
       MapHosts.Initialize
       MapHosts.Put("HostName1", HostName1)
       MapHosts.Put("HostName2", HostName2)
       MapHosts.Put("HostName3", HostName3)
       MapHosts.Put("HostName4", HostName4)
      '***********************************************

        Timer1.Initialize("Timer1", 5000)
    End If


    For Each host As String In MapHosts.Keys
        'Populate your Spinner
        Spinner1.Add(host)
    Next
 
End Sub

Private Sub Spinner1_ItemClick (Position As Int, Value As Object)
    Log(Value)
   'Now, we get the ip from the map
    Log(MapHosts.Get(Value))
End Sub

[code]
 
Upvote 0
ok i think we have something but not much.
I managed to make the map and it works correctly, but there is a problem
now when pressing the button to send the command to the ip, it does not work. I think the error is that the ip is taken from the file saved in settings
i think the error is from the line #207 to the end, where it says "CharAt (0)"

B4X:
#Region  Project Attributes
    #ApplicationLabel: Relé Theos
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim HostName1 As String
    Dim HostName2 As String
    Dim HostName3 As String
    Dim HostName4 As String
    Dim ViewExtChs As Boolean
    Dim Timer1 As Timer
    Dim sock As Socket
    Dim AStreams As AsyncStreams
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 ImageView1 As ImageView
    Dim ImageView2 As ImageView
    Dim ImageView3 As ImageView
    Dim ImageView4 As ImageView
    Dim Label1 As Label
    Dim Label2 As Label
    Dim Label3 As Label
    Dim Label4 As Label
    Dim Panel1 As Panel
    Dim sockBusy As Boolean
    Dim busyTime As Long
    Dim ModuleConnected As Boolean
    Dim CommandBuff As String
    Dim NewState As String
    Dim StateBuff As String
    Dim Spinner1 As Spinner
    Dim MapHosts As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Dim s As List
   
    Activity.LoadLayout("Main")
    Activity.AddMenuItem("Configuracion", "mnuSettings")
    
   
    If FirstTime Then
       
        If File.Exists(File.DirInternal, "AppSettings") Then
            s = File.ReadList(File.DirInternal, "AppSettings")
            HostName1 = s.Get(0)
            HostName2 = s.Get(1)
            HostName3 = s.Get(2)
            HostName4 = s.Get(3)
      Else
            HostName1 = "172.26.0.206"
            HostName2 = "172.16.0.206"
            HostName3 = "172.26.0.6"
            HostName4 = "172.16.0.6"
        End If
   
        '**********Here you got your hostnames set in their variables, now we put in a map the host name (HostNameX) with their ip address
        MapHosts.Initialize
        MapHosts.Put("Cáceres", HostName1)
        MapHosts.Put("Badajoz", HostName2)
        'MapHosts.Put("Hostname3", HostName3)
        'MapHosts.Put("Hostname4", HostName4)
        '***********************************************
   
        Timer1.Initialize("Timer1", 5000)
    End If
    'Spinner1.Add(HostName1)
    'Spinner1.Add(HostName2)
   
    For Each host As String In MapHosts.Keys
        'Populate your Spinner
        Spinner1.Add(host)
    Next
   
End Sub

public Sub Spinner1_ItemClick (Position As Int, Value As Object)
    Log(Value)
    'Now, we get the ip from the map
    Log(MapHosts.Get(Value))
End Sub

Sub Activity_Resume
    'Spinner1.Clear
    'Spinner1.Add(HostName1)
    'Spinner1.Add(HostName2)
    Timer1.Enabled = True
    ModuleConnected = False
   
    Panel1.Visible = False

    Timer1_Tick
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Timer1.Enabled = False
   
    If sockBusy Then
        If AStreams.IsInitialized Then
            AStreams.Close
        End If
       
        If sock.IsInitialized Then
            sock.Close
        End If
        sockBusy = False
    End If
End Sub


Sub Timer1_Tick
    If sockBusy Then
        If sock.Connected Then
            If DateTime.Now - busyTime > DateTime.TicksPerSecond * 10 Then
                If AStreams.IsInitialized Then
                    AStreams.Close
                End If
       
                sock.Close
                sockBusy = False
            End If
        End If
    Else
        Timer1.Interval = 1000
   
        sockBusy = True
        sock.Initialize("sock")
        sock.Connect(Spinner1.SelectedItem, 6722, 10000)
        busyTime = DateTime.Now
    End If
End Sub

Sub sock_Connected (Successful As Boolean)
    If Successful Then
        ModuleConnected = True
        AStreams.Initialize(sock.InputStream, sock.OutputStream, "AStreams")
        If CommandBuff = "" Then
            AStreams.Write("00".GetBytes("ASCII"))
        Else
            AStreams.Write(CommandBuff.GetBytes("ASCII"))
            CommandBuff = ""
        End If
    Else
        SetModuleOffline
    End If
End Sub

Sub AStreams_Error
    AStreams.Close
    SetModuleOffline
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    NewState = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
    AStreams.Close
    ParseModuleState
    sock.Close
    Panel1.Visible = False
   
    sockBusy = False
End Sub

Sub mnuSettings_Click
    StartActivity("Settings")
End Sub


Sub SetModuleOffline
    ModuleConnected = False
    StateBuff = ""
   
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_disable.png")
    ImageView1.Invalidate
    ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_disable.png")
    ImageView2.Invalidate
   
    ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
    ImageView3.Invalidate
    ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
    ImageView4.Invalidate
   
    Panel1.Visible = False
   
   
    sock.Close
    sockBusy = False
End Sub



Sub ParseModuleState
    If StateBuff = "" Or NewState.CharAt(0) <> StateBuff.CharAt(0) Then
        If NewState.CharAt(0) = "1" Then
            ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView1.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView1.Invalidate
    End If
   
    If StateBuff = "" Or NewState.CharAt(1) <> StateBuff.CharAt(1) Then
        If NewState.CharAt(1) = "1" Then
            ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView2.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView2.Invalidate
    End If
   
    If StateBuff = "" Or NewState.CharAt(2) <> StateBuff.CharAt(2) Then
        If NewState.CharAt(2) = "1" Then
            ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView3.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView3.Invalidate
    End If

    If StateBuff = "" Or NewState.CharAt(3) <> StateBuff.CharAt(3) Then
        If NewState.CharAt(3) = "1" Then
            ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_on.png")
        Else
            ImageView4.Bitmap = LoadBitmap(File.DirAssets, "btn_off.png")
        End If
        ImageView4.Invalidate
    End If

   
    StateBuff = NewState
End Sub

Sub ImageView1_Click
    If ModuleConnected Then
        Panel1.Visible = True

       
        If StateBuff.CharAt(0) = "0" Then
            CommandBuff = "11"
        Else
            CommandBuff = "21"
        End If
       
        Timer1.Interval = 250
    End If
End Sub

Sub ImageView2_Click
    If ModuleConnected Then
        Panel1.Visible = True
       
        If StateBuff.CharAt(1) = "0" Then
            CommandBuff = "12*"
        Else
            CommandBuff = "22*"
        End If
       
        Timer1.Interval = 250
    End If
End Sub

Sub ImageView3_Click
    StartActivity("Ping1")
End Sub

Sub ImageView4_Click
    StartActivity("Ping2")
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Sorry, I can't help with that kind of questions, I haven't worked with streams or bytes.
If you write the logs of what you get or what you're trying to send, maybe someone else can help.

Just one thing:
B4X:
sock.Connect(Spinner1.SelectedItem, 6722, 10000)

If I'm not wrong, Spinner1.SelectedItem will be "Cáceres" or "Badajoz", if you want to send the IP, don't you should send?:
B4X:
sock.Connect(MapHosts.Get(Spinner1.SelectedItem), 6722, 10000)
 
Upvote 0
ok, thats perfect, works so good. but now comes the second part

Since I have the app, an error appears from time to time when the mobile screen has been off with the app active, I attach a photo of the error. (photo with black background)
also I do not know for what reason I received another different error when I edited the ip from settings (photo with white background)


WhatsApp Image 2021-01-29 at 00.19.27.jpeg
WhatsApp Image 2021-01-29 at 00.19.27 (1).jpeg



Thank you very much to all of you for the help, especially to @José J. Aguilar
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
This error is unrelated, you should start a new post. Having said that, you should move your socket code to the Starter service (or a custom service/class managed by the
starter service).
 
Upvote 0
Top