Showing posts with label Unity. Show all posts
Showing posts with label Unity. Show all posts

Saturday 20 February 2016

Unity | Unity Read SMS Plugin | Unity Pluging : Read all incoming sms

Hello Friends,
              I read a lot that people facing problem in register and un-register the incoming
sms broadcast from Unity. Also reading incoming sms in Unity app .


              So, today I am going to share my first blog on Unity. This post helps you
to read all android incoming sms in your unity app.  Also you can easily register
and un-register the broadcast base on need.

In this tutorial I am going provide you a Unity Plugin(i.e:smsreader.jar) which
communicate b/w your unity app and android native callbacks. Below are the
steps to use it in your unity app :

1. Note: The package name which you have to use to access the plugin method is

    Permission:  Add below permission inside your unity app manifest.xml file.
         <uses-permission android:name="android.permission.RECEIVE_SMS"/>
         <uses-permission android:name="android.permission.READ_SMS" />

2.  The plugin contains to static method which will be call from unity app :
          A. registerBroadcastReceiver() :  When your unity app launch we first need to
              call this method for registering the broadcast receiver. Below is code snapshot.
              These two methods and class are there in plugin we just need to add that
               plugin as a jar and call it.
  * This method register the Broadcast receiver
  * @param view
 public static void registerBroadcastReceiver() {
    if (broadCastReceiver == null) {
  broadCastReceiver = new IncomingSms();
  new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

          B. unregisterBroadcastReceiver() : Whenever you want to stop reading incoming
               sms call this method or when your unity app goes in background.
  * This method un-register the Broadcast receiver
  * @param view
 public static void unregisterBroadcastReceiver() {

  if (broadCastReceiver != null) {
   broadCastReceiver = null;

package com.affle.smsreader;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

import com.unity3d.player.UnityPlayer;

public class IncomingSms extends BroadcastReceiver {

 final SmsManager sms = SmsManager.getDefault();

 public void onReceive(Context context, Intent intent) {

  // Retrieves a map of extended data from the intent.
  final Bundle bundle = intent.getExtras();

  try {

   if (bundle != null) {

    final Object[] pdusObj = (Object[]) bundle.get("pdus");

    for (int i = 0; i < pdusObj.length; i++) {

     SmsMessage currentMessage = SmsMessage
       .createFromPdu((byte[]) pdusObj[i]);
     String phoneNumber = currentMessage

     String message = currentMessage.getDisplayMessageBody();

     if (UnityPlayer.currentActivity != null) {
        "OnMessageReceived", "" + message);
    } // end for loop
   } // bundle is null

  } catch (Exception e) {
   Log.e("SmsReceiver", "Exception smsReceiver" + e);



Download the plugin from here : SmsReader.jar
Hope this will help some one.
Happy Coding ... :)


Copyright @ 2013 Android Developers Blog.