0

I would like the simplest program to add in my app. The program should only take beacon information after a dialog box option is clicked, I tried many times to write the code but didn't get what are the minimum steps required to start ranging.

public class SecondClass extends Activity implements BeaconConsumer {
private BeaconManager beaconManager;
protected static final String TAG = "RangingActivity";
RegionBootstrap regionBootstrap;

Region region;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout);

    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.bind(this);

    region = new Region("backgroundRegion", null, null, null);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(101); //closes notification

    //opens the alert dialog
    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(this);
    }
    builder.setTitle("Send data")
            .setMessage("Are you sure you want to send your data to the server?")
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // if yes was pressed, send the data

                    try {
                        beaconManager.startRangingBeaconsInRegion(region);
                    } catch (RemoteException e) {
                        Log.d(TAG, "Can't start ranging");
                    }

                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // if cancel was pressed, do nothing

                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}


@Override
public void onBeaconServiceConnect() {

    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {
                Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
            }
        }
    });
}
}

Can anyone explain how the onBeaconServiceConnect runs? And do I need before to start ranging to do the monitoring process, check if there is a beacon and if there is a beacon to start ranging or is okay just to have a program only ranging?

I tried to do as described in Android Beacon Library sample code section, but didn't work

Best regards

EDIT

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Added these permissions to the code, but still not work

Another thing I did was to check if ACCESS_COARSE_LOCATION was enabled by using the function described in here (2nd top most voted answer)

pb77
  • 47
  • 9

2 Answers2

2

Ranging by itself is fine.

The onBeaconServiceConnect is a callback that is made when the beacon scanning service is ready to go, after you call beaconManager.bind(this);. The callback indicates you are ready to start ranging or monitoring. Make sure you get this callback.

  • Make sure you can detect the same beacon with an off the shelf beacon detecting like BeaconScope: https://play.google.com/store/apps/details?id=com.davidgyoungtech.beaconscanner&hl=en_US

  • If using iBeacon, Eddystone or a custom beacon format. Make sure you have registered the proper BeaconParser to work with the library. Here is the code for iBeacon: beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); Eddystone has a different layout.

  • Make sure you have programatically requested and obtained location permission from the user if you are testing on Android 6+, otherwise detections will be blocked. http://altbeacon.github.io/android-beacon-library/requesting_permission.html

  • Make sure Bluetooth is on, Location is enabled for the phone.

  • If on Android 10+, make sure you have obtained FINE_LOCATION permission as COARSE_LOCATION is no longer sufficient.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I added the permission, checked both (location and Bluetooth functionality) and if it works with a another app and everything seems to be right... still app doesn't work – pb77 Feb 05 '18 at 00:20
  • To check if app is working, I open the Logcat and check if I get "The first beacon I see is about " but nothing shows up... – pb77 Feb 05 '18 at 00:26
  • Can you add a log line to the start of onBeaconServiceConnected to see it tomorrow code gets there? – davidgyoung Feb 05 '18 at 03:55
  • Yes code gets there, the problem is in inside the `beacons.size() > 0` – pb77 Feb 05 '18 at 04:56
  • The problem might be I just gave permission in the manifest but not requested to the user – pb77 Feb 05 '18 at 09:09
  • Yes, see my first link above. This is required for it to work. – davidgyoung Feb 05 '18 at 13:00
  • Already added that part of the code and granted coarse location to user but still didn't work... problem still in the `beacons.size() > 0` if – pb77 Feb 06 '18 at 01:39
0

The problem was: in the sample code I thought it was already specified for iBeacons, so I left as it was... changing to

beaconManager.getBeaconParsers().add(new BeaconParser(). setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));

worked for the beacon I am using

pb77
  • 47
  • 9