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
#License: GPLv3

#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=""):
        """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
            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"
            if self.__conn == "usb":
                #check id device 
                if r[1].split("\n")[1] == "":
                    print "No cell phone"
        #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"
        #Set object Android if connection wifi or usb
        #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(("",9999))
        #Send 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 = ""
        print "Error"
    #Set androidsms object 
    androidsms = AndroidSMS(conn,port,host)
    #Send Message

Testing the program:

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

Shown in Figure 
test messages:

1 comentario: