How to strip contact.phonenumber?

netchicken

Active Member
Licensed User
Longtime User
I am making a program that intercepts the sms message coming in, compares the phone number to the contacts list and speaks the name of the person if it matches. (Its going to be a lesson for my students)

It works to a point. My wifes phone has started texting from 2 numbers, the usual (0271234567) and one with the international code prefix (64271234567). However the contacts list only triggers if the first number is received even when both are added to her contact details.

I notice other incoming texts do similar. Can it be stopped on the phone?

So I thought to strip out the incoming number to the last 7 digits, as they are common. However I have crashed on one line. Contact.phonenumber doesn't want to be chopped up.

Can anyone help? BTW I tried unsuccessfully to use MAP instead of list, so I commented it out, anyone know where I went wrong with Map? Hashtables are so much faster.

All code attached.

The Activity code is for testing purposes, the Service will be the final code.

Fixed . Updated with working copy
 

Attachments

  • smsintecepter.zip
    6.6 KB · Views: 160
Last edited:

netchicken

Active Member
Licensed User
Longtime User
Apparently the phones all send in a set format E.164 E.164 - Wikipedia, the free encyclopedia.

So internally it must match up that number 64271234567 with the local db version 0271234567 without the country code.

Which makes more important to strip the incoming number to match it with the locally stored number.
 
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
I fixed it!!!

The Contact.phonenumber crashed because it was either null, no number, or less than 7 numbers. Removing that solved the problem, the zip has been updated.
 
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
Sigh ...
I thought I had it, but but it was a fluke, the phone I was using to send the text has the person as the first person in the contacts database so it was triggering her name. But when I tried another person, much later, it still only loads her name.

Here is the relevant code, the whole program is updated in the first link.

B4X:
Sub SI_MessageReceived (From As String, Body As String) As Boolean
'test messages
ToastMessageShow("Text from " & From, True)
Log ("From before cut" & From)


'strips out the first numbers of the incoming phone number
From = From.SubString2(From.Length-7, From.Length)
'ToastMessageShow("Chopped from " & From, True)

Dim listOfContacts As List
Dim Contacts2 As Contacts2
listOfContacts = Contacts2.GetAll(True,False)


For i = 0 To listOfContacts.Size - 1
    Dim Contact As Contact
    Contact = listOfContacts.Get(i)

'this strips out the first numbers of the phone list   
Dim phonenumber As String
Dim startphone, endphone As Int

If   Contact.phonenumber <>"" AND Contact.phonenumber.Length > 8 Then

phonenumber = Contact.phonenumber 
startphone = phonenumber.Length-7
endphone = phonenumber.Length
phonenumber = phonenumber.SubString2(startphone,endphone)
Log ("From after the cut" & From)

If From = phonenumber Then

'bunch of test messages
   ToastMessageShow("PH " & phonenumber & " From " & From, True)
   Log("PH " & phonenumber & " From after cut " & From)
   ToastMessageShow("Incoming text from " & Contact.Name, True)
   
TTS1.Speak("Text from " & Contact.Name, True)

'write the body text to a file where it gets picked up to read by the notify in another activity (works)

File.WriteString(File.DirInternal, "body.txt","From " & Contact.Name & " " & Body)   
'stop the rest of the loop running? 
   Return
   Else 
   
ToastMessageShow("No matching contact", True)   
Log("PH " & phonenumber & " From after cut " & From)
   Return
   End If
      End If
         
Next  
   
End Sub
 
Last edited:
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
I solved it again, Just delete out the returns.

It would be nice to use Map instead of the loop .....
 
Last edited:
Upvote 0
Top