Android Question Http server on Android device having only 3G/4G data connection

rzv25

Member
Licensed User
Longtime User
Hello all,

I have in mind a personal project which makes sense and can only be implemented if the answer to my question below is yes.
The project is about a car alarm & surveillance system which will use an Android phone as central unit.
As probably already figured out from subject, the question is:

Is it possible to run with the help of B4A a HTTP server on a android device connected to internet only over it's 3G/4G data connection ?

Searching the forum, I have found Davide's post which gave me some hope.
I guess it's not an easy task and probably there are some challenges to overcome (found something not quite cool here) but I am willing to try.

Thank you very much for any idea, suggestion, comment you have.
 

KMatle

Expert
Licensed User
Longtime User
The example is about having an android device connected via router at home. Incoming requests are routed to this device. For security reasons devices inside the mobile network can't be reached.

Of course there is a solution. Like WhatsApp or other apps you can have a cheap hosted server for 5$ which runs php & MySql. Via OkHttpUtils you can send data to that server and do some stuff (like alarm other devices, retrieve data, etc.). Quite simple and there are tons of examples here. I use that configuration with FCM (Firebase Cloud Messaging).

Workflow:

Your app -> OkHttpUtils -> Network (3G/4G WiFi) -> Your Sever -> PHP -> MySQL and back

Problem here: You need sensors for the alarm (door opened, engine started, car moved, etc.)

I have another working solution with a tracker (via Ebay for 40$) which can do a lot of stuff with. It uses SMS to alarm and offers functions like GPS location, fencing, door check, etc. Even there's a relay to cut the engine's function. The user can set phone numbers where the sms goes to. Inside the app just read the sms and react.
 
Upvote 0

DavideV

Active Member
Licensed User
Longtime User
As KMatle wrote you can't access your android server via 3G/4G for security reason.
My android server works at home because it is accessible via router, and the android server is connected to the router via wifi.
Of course the router's settings has been changed to make it accessible from the network.

Bye
 
Upvote 0

rzv25

Member
Licensed User
Longtime User
Thank you for the input, KMatle

For security reasons devices inside the mobile network can't be reached.

I have searched the net a bit and found this thread. Seems like indeed it is not possible to access directly the phone from internet .

... you can have a cheap hosted server for 5$ which runs php & MySql.
Let's say I worked in the past with MySql but I really have no idea about PHP.

Via OkHttpUtils you can send data to that server and do some stuff (like alarm other devices, retrieve data, etc.).
I intend to connect the phone to a GoPro camera and I want to be able to access for example the pictures and videos from it from a browser. Not to mention that it would be awesome to be able to see the stream the GoPro is currently filming. But the part with the stream is just a dream, I guess. So, I don't want to upload on the 5$ server every picture and movie that the GoPro is taking, cause this will consume immediately the data traffic. I could update on the 5$ server only the names of the pictures and files for example, so these would be seen from a browser, but then, when the user clicks on a picture's name, how can the 5$ server retrieve it from the Android phone ? Maybe here is where PHP comes in... Can you please detail a bit ?

Can you please point me to a link of this tracker ? Is it something that can be customized by programming or is a closed solution ?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I have this one:

http://www.ebay.co.uk/itm/TK103-Car...785746?hash=item1c4224b352:g:d-YAAOSwrklU5b70

Manual (just a quick grab from the www. See others which have more detailed info's)

http://www.localizadorgpstracker.com.mx/TK103.pdf

Insert a SIM card (deactivate the PIN protection before!). It just send's a simple text message. You can modify the settings by sending a text message in a defined format to it (see the manual)

To read the SMS'es I use this code (it intecepts any incoming sms and checks it for useful content). Use it inside a service module.

B4X:
Sub Service_Start(startingIntent As Intent)
    If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
        SMSList.Initialize
        ParseSmsIntent(startingIntent)
        For i = 0 To SMSList.size - 1
            Dim SMSMap As Map
            SMSMap=SMSList.Get(i)
            Log(SMSMap.Get("address"))
            Log(SMSMap.Get("body"))
            Log(SMSMap.Get("time"))
            ParseSingleSMS(SMSMap.Get("address"),SMSMap.Get("body"))
        Next
              
    End If

Sub ParseSmsIntent (in As Intent)
   
    If in.HasExtra("pdus") = False Then Return 
    Dim pdus() As Object
    Dim r As Reflector
    pdus = in.GetExtra("pdus")
    If pdus.Length > 0 Then
       
        For i = 0 To pdus.Length - 1
            r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
            Array As Object(pdus(i)), Array As String("[B"))
           
            Dim SMSMap As Map
            SMSMap.Initialize
            SMSMap.put("address", r.RunMethod("getOriginatingAddress"))
            SMSMap.put("body", r.RunMethod("getMessageBody"))
            SMSMap.put("time", r.RunMethod("getTimestampMillis"))
           
            SMSList.Add(SMSMap)
   
        Next
    End If
   
End Sub  
   
   
End Sub

Just check the contents of "body" (this contains the sms'es text with the info from the tracker). SMSList is just a list.

In the manifest (Project -> Manifest Editor) add the folowing at the end:

B4X:
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(s1,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

This will add a "listener" and the permission for your app to read/receive sms. Target SDK: 22!

When your app starts, the listener will be added. If a sms comes in, the service module "s1" will be called (Service_Start).

B4X:
If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then ...

askes why the service has been started (here: only if an sms was received).

I do a loop here because more than one SMS can be received at a time.
 
Upvote 0

rzv25

Member
Licensed User
Longtime User
Thank you KMatle for the details of the device and the way to use it. It's very nice but I want to accomplish a bit more than this and want to use an Android phone.
Do you have any more details on the topic of how could I access for example pics from the GoPro from a web browser, giving the fact that the Android is connected to internet through the 4G ? How would the Android device know when the browser requested a picture, in order to send it to the server ?
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Actually, with a bit of experimenting, you CAN with IPv6 only as long as you have IPv6 service on your phone. the IPv6 address is unique to the device like IPv4 was in the old days. I have tried it with UDP from a PHP script across the internet, and it did work. at least on IPv6. IPv4 still, was a nogo.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…