jueves, 16 de junio de 2011

Program for sending SMS from Linux to Android (wifi and USB)

Explain how to send SMS from a Mobile Linux Android (USB or WiFi connection).


You need the Android SDK, you will use python SL4A within thecell.


Create a class  call AndroidSMS that will verify   if the connection is wifi or usb, then validates the mobile number and send the message to the phone.


If the connection is wifi you need to a port number  and the host to stablish connection with cell phone.
If the connection is usb you need to a port number only.


You must have the android module in the directory where you execute the script.


The Android SDK can be downloaded at this link.


Android module for remote execution on this link.


How to make Android remote control is explained in this link.


The code for sending text messages:





#!/usr/bin/env python
# -*- coding: utf-8 -*-


#Send Message, from Linux in Android mobile.
#Author: Ernesto Crespo
#Email:ecrespo@gmail.com
#License: GPLv3
#Version:0.4


#Import  android,sys,re and getstatusoutput
import android,sys,re


from commands import getstatusoutput


#Create a Class AndroidSMS
class AndroidSMS:
    def __init__(self,conn,port,host="192.168.0.100"):
        """conn: usb or wifi
        port: port number of SL4A server.
        host: IP number of cell phone
        #path to adb program
        self.__adb = "/home/ernesto/android-sdk-linux_x86/platform-tools/adb"
        self.__port = port
        self.__host = host
        self.__conn = conn 
    
    def __CheckNumber__(self,numCell):
        """check if numCell has  11 digits, and check valid provider
        """
        if len(numCell) == 11 and ((re.search("041[2|4|6]\d\d\d\d\d\d\d",numCell)) or (re.search("042[4|6]\d\d\d\d\d\d\d",numCell))) :
           # return 1 if a valid provider and valid number
            return 1
          
        else:
            return 0


    def __ConfigAndroid__(self):
        """Config conecction Linux to Android.
        """
        # kill SL4A server 
        getstatusoutput("%s kill-server" %self.__adb)
        #clean AP_PORT and AP_HOST variables
        getstatusoutput("export AP_PORT=\"\"")
        getstatusoutput("export AP_HOST=\"\"")
        #start SL4A server
        r = getstatusoutput("%s devices" %self.__adb)
        if r[0] <> 0:
            print "Problems with cell phone connection"
            sys.exit
        else:
            if self.__conn == "usb":
                #check id device 
                if r[1].split("\n")[1] == "":
                    print "No cell phone"
                    sys.exit
        #Cell phone work 
        # port forward  in SL4A server
        getstatusoutput("%s  forward tcp:9999 tcp:%s" %(self.__adb,self.__port))
        if self.__conn == "wifi":
            #Set AP_PORT and AP_HOST
            #if wifi
            getstatusoutput("export AP_PORT=%s" %self.__port)
            getstatusoutput("export AP_HOST=%s" %self.__host)
        elif self.__conn == "usb":
            #Set AP_PORT
            #if usb
            getstatusoutput("export AP_PORT=\"9999\"")
        print "Cell phone work"




    def SendMessage(self,number,message):
        """SendMessage: Send Message to cell phone
        """
        if self.__CheckNumber__(number) == 0:
            print "No valid Number"
            sys.exit
        #Set object Android if connection wifi or usb
        self.__ConfigAndroid__()
        #wifi: set host and port 
        #usb: set port only
        if self.__conn == "wifi":
            droid = android.Android((self.__host,self.__port))
        elif self.__conn == "usb":
            droid = android.Android(("127.0.0.1",9999))
        #Send Message
        droid.smsSend(number,message)
        
    
if __name__ == '__main__':
    """Sets values: number, message, conn, port and host
    """
    if len(sys.argv) == 6:
        number = sys.argv[1]
        message = sys.argv[2]
        conn = sys.argv[3]
        port = sys.argv[4]
        host = sys.argv[5]
    elif len(sys.argv) == 5:
        number = sys.argv[1]
        message = sys.argv[2]
        conn = sys.argv[3]
        port = sys.argv[4]
        host = ""
    else:
        print "Error"
        sys.exit
    #Set androidsms object 
    androidsms = AndroidSMS(conn,port,host)
    #Send Message
    androidsms.SendMessage(number,message)


Testing the program:


  • Wifi connection: python message.py 0xxyyyzzww "test1" wifi  47529 "192.168.0.100"
  • USB connection: python message.py 0xxyyyzzww "test2" usb  43421



Shown in Figure 
test messages:



jueves, 6 de enero de 2011

Application that displays the coordinates of cell phone in Desktop with google maps

In this post I use 2 previous articles about create web browser and howto capture cell phone location.


The idea is capture the information of GPS from cell phone and display location in application with gtk, webkit and python with Google Maps.


The glade interface shown below.


It's actually the same application web browser but, I remove the text entry and button "go ".


Below the code:




#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
"""
Nombre:Geolocalizacion
Descripción: Program that capture the location of cell phone
     shown in google maps on the Linux desktop.
Versión:0.1
Licence:GPLv3
Author: Ernesto Crespo
Email: ecrespo@gmail.com
"""
#Import gtk and webkit


import gtk
import webkit
#Import android and time
import android
from time import sleep


    
#class App
class App:
    def __init__(self):
        #init function
        #Binding glade file with Builder
        self.glade_file = "geolocalizcion.glade"
        self.glade = gtk.Builder()
        self.glade.add_from_file(self.glade_file)
        #Bind window geo
        self.window = self.glade.get_object('geo')
        #Bind button exit
        self.exit = self.glade.get_object('salir')
        #Bind scrolledwindow
        self.scrolledwindow1 = self.glade.get_object('scrolledwindow1')
        #
        #Linking destroy event with function
        self.window.connect("destroy",self.on_geo_destroy)
        #Linking clicked event with function
        self.exit.connect('clicked', self.on_salir_clicked)
        #Open google maps site
        #create Object webview
        self.webview = webkit.WebView()
        #Add webview in scrolledwindow
        self.scrolledwindow1.add(self.webview)
        #Capture location
        tupla = localizacion()
        #Open  google maps URL win  longitude and latitude
        self.__go("http://maps.google.com/maps?q=%s,%s" %tupla)
        
        #Show window
        self.window.show_all()
    
    
    def __go(self,url):
        #Open URL
        self.webview.open(url)
        
        
    def on_geo_destroy(self,*args):
        #close application
        gtk.main_quit()
    
    def on_exit_clicked(self,*args):
     #close application
        gtk.main_quit()
    
    
    def main(self):
        #Start application
        gtk.main()
        
    
def localizacion():
    
        #create android object
        droid = android.Android()
        #start location
        droid.startLocating()
        #wait 15 seconds
        sleep(15)
        #capture latitude and longitude
        latitude = droid.readLocation().result["network"]["latitude"]
        longitude = droid.readLocation().result["network"]["longitude"]
        #Stop location
        droid.stopLocating()
        #return latitude and longitude
        return (latitude,longitude)


if __name__ == '__main__':
    app = App()
    app.main()



The figure showing the app running with google maps site.

Create a basic web browser with python+webkit+gtk

Returning to the programming with python, glade and gtknow explain how to make a simple browser using python webkit.

This post is based on Marcelo's blog article, I create interface with glade.



The figure shows the GUI application.


The application simply has a EntryText, the URL where you can enter to load the page when push button go, the page is loaded into the frame with sliders in the center of the interface and has a button  exit. The default is to load a page to start the application.

The application code is shown below:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Import gtk and webkit



import gtk
import webkit



    
#class App
class App:
    def __init__(self):
     #function init 
        #bind glade file with Builder object
        self.glade_file = "browser.glade"
        self.glade = gtk.Builder()
        self.glade.add_from_file(self.glade_file)
        #Binding  window with browser
        self.window = self.glade.get_object('browser')
        #Button exit
        self.exit = self.glade.get_object('bexit')
        #Button browser
        self.go = self.glade.get_object('browse')
        #EntryText URL
        self.link = self.glade.get_object('url')
        #Scrolledwindows where you can see a site
        self.scrolledwindow1 = self.glade.get_object('scrolledwindow1')
        #
        #Linking destroy browser with function
        self.window.connect("destroy",self.on_browser_destroy)
        #Linking clicked exit button
        self.exit.connect('clicked', self.on_bexit_clicked)
        #Linking clicked browse button
        self.go.connect('clicked',self.on_browse_clicked)
        #Linking activate with URL entrytext
        self.link.connect('activate',self.on_url_activate)
        #Open  default site
        #Instant class webview 
        self.webview = webkit.WebView()
        #Add webview in scrolledwindow
        self.scrolledwindow1.add(self.webview)
        #Open default page
        self.__go("http://www.debian.org")
        
        #Show window 
        self.window.show_all()
    
    
    def on_url_activate(self,*args):
        #catch url to open page
        url = "http://" + self.enlace.get_text()
        self.__go(url)



    
    def __go(self,url):
        self.webview.open(url)
        
        
    def on_browser_destroy(self,*args):
        #close application
        gtk.main_quit()
    
    def on_bexit_clicked(self,*args):
     #close application
        gtk.main_quit()
    
    def on_browse_clicked(self,*args):
        #Open site
        url = "http://" + self.link.get_text()
        self.__go(url)
    
    def main(self):
     #star application
        gtk.main()


















if __name__ == '__main__':
    app = App()
    app.main()




The following figure shows the browser.