androidbluetooth-lowenergyandroid-notificationsiotibeacon-android

Ibeacons sending notification when Android device comes in specific range


In my project I want to show the notification on my android device having my android app installed. What I actually want to do is that suppose that the person enters the shop I want to show him the welcome notification or any other text from the MySQL database. Is it possible to do so ? I have searched alot but I am not very well impressed with any solution. Can anyone please give me the right tips and code.


Solution

  • Yes, it is possible to do this and it is a common thing to do. There are two key things you need to know how to do to solve this problem:

    1. You need to set up your app to detect beacons and send a notification when the beacon is detected. You can see an example of how to do this with the Android Beacon Library here:

      public class BeaconNotificationApplication extends Application implements BootstrapNotifier {
        private static final String TAG = "BeaconReferenceApp";
        private RegionBootstrap regionBootstrap;
        private BackgroundPowerSaver backgroundPowerSaver;
      
        public void onCreate() {
          super.onCreate();
          BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
      
          // By default the AndroidBeaconLibrary will only find AltBeacons.  If you wish to make it
          // find a different type of beacon, you must specify the byte layout for that beacon's
          // advertisement with a line like below.  The example shows how to find a beacon with the
          // same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb.  To find the proper
          // layout expression for other beacon types, do a web search for "setBeaconLayout"
          // including the quotes.
          //
          //beaconManager.getBeaconParsers().clear();
          //beaconManager.getBeaconParsers().add(new BeaconParser().
          //        setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
      
          // wake up the app when a beacon is seen
          Region region = new Region("backgroundRegion",
                  null, null, null);
          regionBootstrap = new RegionBootstrap(this, region);
          backgroundPowerSaver = new BackgroundPowerSaver(this);
        }
      
        @Override
        public void didEnterRegion(Region arg0) {
            sendNotification();
        }
      
        @Override
        public void didExitRegion(Region region) {
        }
      
        @Override
        public void didDetermineStateForRegion(int state, Region region) {
        }
      
        private void sendNotification() {
            NotificationCompat.Builder builder =
                  new NotificationCompat.Builder(this)
                          .setContentTitle("Beacon Reference Application")
                          .setContentText("An beacon is nearby.")
                          .setSmallIcon(R.drawable.ic_launcher);
      
          NotificationManager notificationManager =
                  (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
          notificationManager.notify(1, builder.build());
        }    
      }
      

    You can read more about details of detecting beacons with the Android Beacon Library on the project website.

    1. You need to set up a SQL database that stores your beacon messages, then modify the sendNotification method above to query that database for the beacon identifiers and show the proper text. The Android developer website has a good tutorial of how to store date in SQL and retrieve it here:

    https://developer.android.com/training/basics/data-storage/databases.html