Receive sms on APDE

edited April 14 in Android Mode

Hello! I'm want to receive sms using APDE but the library smsP5 doesn't work on :(( . And I've also tried the code find here but he is too old ~X( . So, if you have some idea I'm will accept it with pleasure. Ps : Excuse me for grammar but i'm French...

Tagged:

Answers

  • @wladimir=== what do you mean "the code is too old" - It creates a receiver, gets the sms and parses it: that is the standard way for sms...

  • On the console there is an avalanche of "this method is deprecated".

  • @wladimir=== "this method is deprecated" is only some kind of warning; it runs and you can use it, though you can "update" the code using the new versions for these methods. As soon i can i ll have a look to that.

  • edited April 20

    @wladimir===

    modifying somewhat the code, it seems to work (i cannot test the incoming sms)

                    import android.os.Build;
    
    
    
                        import android.content.BroadcastReceiver;
                        import android.content.Context;
                        import android.content.Intent;
    
                        import android.content.IntentFilter;
                        import android.os.Bundle;
    
                        import android.telephony.SmsMessage; //new-API23
    
                         String message = ""; 
                         String numero = ""; 
                         SmsReceiver mySMSReceiver = new SmsReceiver();
                         boolean enregistre = false;
    
                         void setup(){
    
                           size(400,600);
                           orientation(PORTRAIT);
    
                           if(!enregistre){
                           IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");   
                              getActivity().registerReceiver(mySMSReceiver, filter); //fragment - launch class when SMS are RECEIVED
                           enregistre = true;
                           }
    
                         }
    
    
                         void draw(){
    
                           if(message!=""){ // waiting for new messages
                             fill(0,0,0);
                             rect(0,0,400,600);
                             fill(255,0,0);
                             text("Nouveau SMS !",10,40);
                            text(message,10,90);
                            text("From : "+numero,10,130);
                            message="";      
                           }
                         }
    
    
                        public class SmsReceiver extends BroadcastReceiver //Class to get SMS
                        {
                            @Override
                            public void onReceive(Context context, Intent intent) 
                            {
                                //---get the SMS message passed in---
                                Bundle bundle = intent.getExtras();        
                                SmsMessage[] msgs = null;
                                String appelle="";
                                String str="";
    
                                if (bundle != null)
                                {
                                    //---retrieve the SMS message received---
                                    Object[] pdus = (Object[]) bundle.get("pdus");
                                    msgs = new SmsMessage[pdus.length];  
    
    
                                    for (int i=0; i<msgs.length; i++){
    
                                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//for new API >=Mashmallow
                                    String format = bundle.getString("format");
                                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                                     appelle += msgs[i].getDisplayOriginatingAddress() ;
                                        str += msgs[i].getMessageBody().toString();
                                }
                                else {// for old API
                                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
                                        //msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i], String format); 
                                       appelle += msgs[i].getOriginatingAddress();
                                        str += msgs[i].getMessageBody().toString();
                                    }
    
                                }   
                                message=str;
                                numero=appelle;   
    
                            }
                        }
                        }// fin de la classe
    
    
    
    
                            //@Override
                             public void onResume() {
                               super.onResume();
                               if(!enregistre){
    
                              IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");   
                              getActivity().registerReceiver(mySMSReceiver, filter); // launch class for incoming SMS
                            }
                             }
    
  • edited April 21

    @akenaton There are two permissions. One is to read SMS and the second is to receive SMS. I tested that you need the latter. If I have only read SMS permission, it doesn't do anything. Do you know what is that permission for?

    Also, does AS offer any tool to simulate sending and receiving SMS?

    Finally, I notice that I can add sketch permissions, but I cannot remove them. I have to delete the manifest file first and re-select all those permissions that I need. Inconvenient. I might generate a bug report pending: https://github.com/processing/processing-android/issues/303

    Kf

  • dear @kfrajer===

    in this case you want to receive sms && READ them (see the code) so, you are right, twos are needed, error is mine

    yes,you can simulate sms using the emulator (&& ddms) but it is stupid; best is to send some fake sms to the phone (from another real one) but as i am travelling i cannot do that: that is the reason why i said "not tested"!!! - though i think that the code must run...

    As for the manifest, with P5 I edit it manually at each time and add what i think useful.

  • Answer ✓

    I confirm your code works. Only one permission is needed: Receive_SMS . Mind one needs to set the text size to 32. Thxs for sharing the solution @akenaton.

    Good to know you edit your own manifest manually....

    Kf

  • Thanks a lot @akenaton and @kfrajer!

  • Answer ✓

    @wladimir ===

    ok; put answered for others...And good luck!

Sign In or Register to comment.