Android Question caller ID send via UDP to PC

dpetrinic

Member
Licensed User
Longtime User
HI to all,

would like to have small application able to send caller id (incoming number) on android phone to PC. On PC visual basic application is waiting on UDP port for data.

I make small application but caller ID is not show up at any time. I'm using Samsung Galaxy alpha with android 4.4.4

main:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

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

Sub Process_Globals

    Dim UDPSocket1 As UDPSocket
    Dim Server As ServerSocket
    Dim PE As PhoneEvents
    Dim PhoneId As PhoneId
End Sub

Sub Service_Create
   PE.InitializeWithPhoneState("PE",PhoneId)

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 ButtonSend As Button
    Dim EditData As EditText
    Dim EditDest As EditText
    Dim LabelIP As Label
    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private EditText1 As EditText
    Private EditText2 As EditText
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label
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("Layout1")
    Activity.LoadLayout("CALLER_ID")
    Msgbox("OVO JE TEST!", "")
    Dim IP As String
  
    If FirstTime Then
        UDPSocket1.Initialize("UDP", 5000, 8000)
        IP = Server.GetMyWifiIP
        StartService(sPhone)
    End If
    'Label2.Text=IP    '        LabelIP.Text = IP
    EditDest.Text="192.168.1.255"
       
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button3_Click
    Label1.Text="BUTTON3_click"
End Sub

Sub Button2_Click
    Label1.Text="BUTTON2_click"
End Sub

Sub Button1_Click
    Label1.Text="BUTTON1_click"
End Sub



Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)

  

      If State="RINGING" Then   ' Ringing state.
             Msgbox("RINGING: ","jebiga" )
               Label2.Text=IncomingNumber
               ButtonSend_Click
     End If
    
            
       'your code ..
End Sub

service:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#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 PE As PhoneEvents
     ' Dim PhoneId As PhoneId
End Sub
Sub Service_Create
     'PE.InitializeWithPhoneState("PE",PhoneId)
    PE.Initialize("PE")
End Sub
Sub Service_Start (StartingIntent As Intent)

End Sub
Sub Service_Destroy

End Sub
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    Log("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber)
    Log(Intent.ExtrasToString)
    Msgbox("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber, "")
      If State="RINGING" Then   ' Ringing state.
             Msgbox("RINGING: ","jebiga" )
              ' Label2.Text=IncomingNumber
               'ButtonSend_Click
     End If
'    Log(Intent.ExtrasToString)
End Sub
.

any help will be very appreciated.
Thanks,
Damir
 

mangojack

Expert
Licensed User
Longtime User
there is a working example HERE ...

as @Erel stated .. remove mdgbox from service.
Remove Sub Service_Create from Main activity
Remove Sub PE_PhoneStateChanged from Main activity
 
Upvote 0

dpetrinic

Member
Licensed User
Longtime User
Hi to all,
thanks for replies. I change code on way that I use working example + addition part for UDP send , and it is working only when application is on top, but not in background. In fact when it is in background UDP packet is send immediately after HANG UP if call is picked up.
Is there a way to have it working during ringing what so ever if is phone is answered or not even if phone is in standby. There is no any protection for phone to be used (user code or other).

B4X:
#Region  Project Attributes
    #ApplicationLabel: Incomingph
    #VersionCode: 1
    #VersionName: 1.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#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 UDPSocket1 As UDPSocket
    Dim Server As ServerSocket
   
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.

    Private start_btn As Button
    Private stop_btn As Button
   
    Dim LabelIP As Label
    Private Button1 As Button
   
   
    Private Label1 As Label
   
    Dim data() As Byte
   
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("Main")
   
    Dim IP As String
  
    If FirstTime Then
                          UDPSocket1.Initialize("UDP", 5000, 8000)
                          IP = Server.GetMyWifiIP
       
                          LabelIP.Text=IP    '        LabelIP.Text = IP
                        
                         Button1_Click
    End If
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub stop_btn_Click
    StopService("phdetect")
End Sub
Sub start_btn_Click
    StartService("phdetect")
End Sub

Sub toastphn(incomph As String)
Dim Packet As UDPPacket
 
    ToastMessageShow ("Incoming Number is "&incomph, True)
     data =incomph.GetBytes("UTF8")                       'prepare message with incoming number
   
   
       UDPSocket1.Initialize("UDP", 5000, 8000)
        IP = Server.GetMyWifiIP
       
      LabelIP.Text=IP    '        LabelIP.Text = IP
     'EditDest.Text="192.168.1.255"   
    Packet.Initialize(data, "192.168.1.255", 5000)
    UDPSocket1.Send(Packet)


End Sub


Sub Activity_KeyPress (KeyCode As Int) As Boolean
  Dim Answ As Int
  If KeyCode = KeyCodes.KEYCODE_BACK Then
    Answ = Msgbox2("Do you want to Quit?", _
      "A T T E N T I O N", "Yes", "", "No", Null)
    If Answ = DialogResponse.NEGATIVE Then
      Return True
    End If
  End If
  Return False
End Sub

Sub Button1_Click
    Dim Packet As UDPPacket
  
      UDPSocket1.Initialize("UDP", 5000, 8000)
        IP = Server.GetMyWifiIP
       
      LabelIP.Text=IP    '        LabelIP.Text = IP
     
    data =Label1.Text.GetBytes("UTF8")                       ' EditData.text.GetBytes("UTF8")    OVDE SALJEM BROJ KOJI JE ZVAO
    Packet.Initialize(data, "192.168.1.255", 5000)
    UDPSocket1.Send(Packet)
    'ToastMessageShow("Message send = ", False)
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    'Msgbox("Message received: " & msg, "")
End Sub


B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals

   Dim PE As PhoneEvents
   Dim PhoneId As PhoneId
   Dim incomph As String
End Sub

Sub Service_Create
PE.InitializeWithPhoneState("PE",PhoneId)
End Sub

Sub Service_Start (StartingIntent As Intent)
Dim noti As Notification 'shows in the notification window
noti.Initialize
noti.Icon = "icon"
'noti.Notify(1)
noti.Sound = True
noti.Vibrate = True
noti.Light = False
'this runs the next Activity (smstext)
noti.SetInfo("IncomingPh","Running",Main)
Service.StartForeground(1, noti)
End Sub

Sub Service_Destroy
PE.StopListening()
Service.StopForeground(1)
End Sub
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
Select State
Case "RINGING"
CallSubDelayed2(Main, "toastphn", IncomingNumber)
End Select
End Su

please note that is my second program for android in 4 years, now have time to play with it even I purchase it long time ago.
Thanks,
Damir
 
Upvote 0

dpetrinic

Member
Licensed User
Longtime User
Hi to all,

two days ago I ask for help and here I am with half solution, and I still need help.
Program is working when running in foreground, but problem is when it is in state of Activity_pause, it is never send data when
B4X:
CallSub2(Main, "toastphn", IncomingNumber)
which should be started before PHONE start ringing, but after phone stop ringing command
B4X:
CallSubDelayed2(Main, "toastphn", IncomingNumber)
is executed. at least I have notice of caller id, but what I really need is that I have caller ID on screen before I pick up phone.
here it is LOG
** Activity (main) Pause, UserClosed = false **
PhoneStateChanged, State = RINGING, IncomingNumber = +3859944XXXXX
Bundle[{incoming_number=+3859944XXXXX, state=RINGING}]
ringing
vracen iz maina-
sending message to waiting queue (CallSubDelayed - toastphn)
PhoneStateChanged, State = IDLE, IncomingNumber =
Bundle[{state=IDLE}]
running waiting messages (1)
broj poslan-1
broj poslan-2
broj poslan-3
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
PhoneStateChanged, State = RINGING, IncomingNumber = +3859944XXXX
Bundle[{incoming_number=+3859944XXXXX, state=RINGING}]
ringing
vracen iz maina-
sending message to waiting queue (CallSubDelayed - toastphn)
PhoneStateChanged, State = IDLE, IncomingNumber =
Bundle[{state=IDLE}]
running waiting messages (1)
broj poslan-1
broj poslan-2
broj poslan-3
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **

Basic idea is to have program used in my restaurant where we have delivery service, and would like to have database on PC where we store all orders, but also to keep record about customer (phone,name, address,etc.).

there is so many LOG items in program, just for me to follow up what is going on. There is also few times UDP packet send in each loop just to be sure that data are send to network.
Please help,
Thanks,
Damir
B4X:
#Region  Service Attributes
    #StartAtBoot: true
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals

   Dim PE As PhoneEvents
   Dim PhoneId As PhoneId
   Dim incomph As String
End Sub

Sub Service_Create
PE.InitializeWithPhoneState("PE",PhoneId)
Log("service created")
End Sub

Sub Service_Start (StartingIntent As Intent)
Dim noti As Notification 'shows in the notification window
noti.Initialize
noti.Icon = "icon"
'noti.Notify(1)
noti.Sound = True
noti.Vibrate = True
noti.Light = False
'this runs the next Activity (smstext)
noti.SetInfo("IncomingPh","Running",Main)
Service.StartForeground(1, noti)
Log("service started")
End Sub

Sub Service_Destroy
PE.StopListening()
Service.StopForeground(1)
End Sub
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
  Log("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber)
    Log(Intent.ExtrasToString)
   
Select State
Case "RINGING"
Log("ringing")   

CallSub2(Main, "toastphn", IncomingNumber)
Log("vracen iz maina-")
CallSubDelayed2(Main, "toastphn", IncomingNumber)
End Select
End Sub
B4X:
#Region  Project Attributes
    #ApplicationLabel: Incomingph
    #VersionCode: 1
    #VersionName: 1.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#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 UDPSocket1 As UDPSocket
    Dim Server As ServerSocket
    Public data() As Byte
    Public brojac As Byte
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.

    Private start_btn As Button
    Private stop_btn As Button
   
    Dim LabelIP As Label
    Private Button1 As Button
   
   
    Private Label1 As Label
   
   
   
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("Main")
   
    Dim IP As String
  
    If FirstTime Then
                          UDPSocket1.Initialize("UDP", 5000, 8000)
                          IP = Server.GetMyWifiIP
       
                          LabelIP.Text=IP    '        LabelIP.Text = IP
                        
                         Button1_Click
                         StartService("phdetect")
    End If
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub stop_btn_Click
    StopService("phdetect")
End Sub
Sub start_btn_Click
    StartService("phdetect")
End Sub

Sub toastphn(incomph As String)
Dim Packet As UDPPacket
 
    'ToastMessageShow ("Incoming Number is "&incomph, True)
     data =incomph.GetBytes("UTF8")                       'prepare message with incoming number
    Label1.Text=incomph
   
       UDPSocket1.Initialize("UDP", 5000, 8000)
       IP = Server.GetMyWifiIP
       
    'LabelIP.Text=IP    '        LabelIP.Text = IP
    'EditDest.Text="192.168.1.255"   
    Packet.Initialize(data, "192.168.1.255", 5000)
    UDPSocket1.Send(Packet)
    Log("broj poslan-1")
     UDPSocket1.Send(Packet)
    Log("broj poslan-2")
     UDPSocket1.Send(Packet)
    Log("broj poslan-3")
    UDPSocket1.close

End Sub


Sub Activity_KeyPress (KeyCode As Int) As Boolean
  Dim Answ As Int
  If KeyCode = KeyCodes.KEYCODE_BACK Then
    Answ = Msgbox2("Do you want to Quit?", _
      "A T T E N T I O N", "Yes", "", "No", Null)
    If Answ = DialogResponse.NEGATIVE Then
      Return True
    End If
  End If
  Return False
End Sub

Sub Button1_Click
    Dim Packet As UDPPacket
    Label1.Text=""
    Label1.Text="DATA TO SEND"
                     UDPSocket1.Initialize("UDP", 5000, 8000)
  
     
     
   
        IP = Server.GetMyWifiIP
       
      'LabelIP.Text=IP    '        LabelIP.Text = IP
  
  
  
    data =Label1.Text.GetBytes("UTF8")                       ' EditData.text.GetBytes("UTF8")    OVDE SALJEM BROJ KOJI JE ZVAO
                 Packet.Initialize(data, "192.168.1.255", 5000)
                 UDPSocket1.Send(Packet)
                 Log("poslano na UDP -1")
                 UDPSocket1.Send(Packet)
                 Log("poslano na UDP -2")
                 UDPSocket1.Send(Packet)
                 Log("poslano na UDP -3")
               
                 UDPSocket1.close
              
               
               
    'ToastMessageShow("Message send = ", False)
   
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    'Msgbox("Message received: " & msg, "")
End Sub
 
Upvote 0
Top