0

I tried to calculate my ibeacon distance using android studio , i followed a video tutorial in http://www.software7.com/blog/creating-a-beacon-app-for-android-in-less-than-10-minutes-from-scratch/ , i did exactly the same as he did , however he didn't use android studio instead IntelliJ IDEA . The result should be appear at android monitor - logcat , but mine didn't

import android.os.RemoteException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import java.util.Collection;

public class MainActivity extends AppCompatActivity implements 
BeaconConsumer {
public static final String TAG = "BeaconsEverywhere";
private BeaconManager beaconManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    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"));
    beaconManager.bind(this);

}

@Override
public void onBeaconServiceConnect() {
    final Region region = new Region("myBeaons", Identifier.parse("2173E519-
    9155-4862-AB64-7953AB146156"),null,null);
    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            try {
                Log.d(TAG,"didEnterRegion");
                beaconManager.startRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void didExitRegion(Region region) {
            try {
                Log.d(TAG,"didExitRegion");
                beaconManager.stopRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void didDetermineStateForRegion(int i, Region region) {

        }
    });
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, 
        Region region) {
            for(Beacon oneBeacon : beacons){

                Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" + 
                oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + 
                oneBeacon.getId3());
            }
        }
    });
    try {
        beaconManager.startMonitoringBeaconsInRegion(region);
    }catch (RemoteException e){
        e.printStackTrace();
    }

}

@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}
}

The first Image is my expectation , second is my result

would you guys help me to figure out what the problem is ?

  • This might help https://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing – Barns Aug 05 '17 at 17:50

1 Answers1

0

I would add new debug lines like Log.d(TAG,"onCreate"); and Log.d(TAG,"onBeaconServiceConnect"); in each of those methods so you can tell what is going on, then look for those lines when you run your app. This will tell you how far your program is getting in the process.

I also suspect that you will need to use a different beacon parser expression than is listed here:

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"));

If you are using an off-the-shelf iBeacon, you will need to replace that string with the iBeacon layout as can be found here:

https://beaconlayout.wordpress.com/

Don't worry about Android Studio vs. IntelliJ IDEA. The code shown should work fine using either IDE.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • i tried to change the parser but still didn't work , and i did as you say to add new debug line , but i still couldn't find the solution . However i tried to show the distance result(double) in TextView , i convert the double to string first but seems didn't work . Do you think it's posible to show the result in TextView ? If you know how , please teach me . Thx @davidgyoung – ZeratoZ Aug 06 '17 at 08:07