0

I want to scan an iBeacon UUID = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6" from Android phone Nexus5

I have followed example from AltBeacon and SO query. But don't see anything scanning. Where am I wrong?

Here is the code

private static final String TAG = "ALTBEACON";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
private String UUID = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6";
private static final int REQUEST_ENABLE_BT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(beaconManager.checkAvailability()){
        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);
    }
    else{
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // User chose not to enable Bluetooth.
    if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


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


@Override
public void onBeaconServiceConnect() {
    beaconManager.setMonitorNotifier(new MonitorNotifier() {

        @Override
        public void didExitRegion(Region region) {
            Log.i(TAG, "Exit from Region");
            Toast.makeText(getApplicationContext(), "Exit from Region", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void didEnterRegion(Region region) {
            Log.i(TAG, "Entered in Region");
            Toast.makeText(getApplicationContext(), "Entered in Region", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "Not Sure... State : "+state+" ... Region : "+region.describeContents());
            Toast.makeText(getApplicationContext(), "Not Sure... State : "+state+" ... Region : "+region.describeContents(), Toast.LENGTH_SHORT).show();
        }
    });

    try {
        beaconManager.startMonitoringBeaconsInRegion(new Region(UUID, null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
Community
  • 1
  • 1
Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60
  • Please double check the parameters of the `Region` constructor. The first parameter is not the UUID, so you should change this to something like: `new Region("com.myapp.regionmonitor", UUID, null, null)` – davidgyoung Nov 18 '14 at 12:20

1 Answers1

1

The most likely problem is that your app does not have the proper AndroidManifest.xml entries to declare the beacon scanning service. This is generally done automatically using manifest merging from the library's manifest. If you are using Eclipse, you need to turn on manifest merging as described in the library's Quick Start guide:

Edit your project.properties file and add the line: manifestmerger.enabled=true

An easy way to tell if this is the problem is to add a log line to the top of your onBeaconServiceConnect() method. If it is not getting called, that means the service cannot start up.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I put the debug points inside onBeaconServiceConnect() and they were not getting called – Abhinav Tyagi Nov 18 '14 at 12:43
  • Manifest merger line is missing... Do I need to put bluetoith permissions or not? At present I have put both permissions. – Abhinav Tyagi Nov 18 '14 at 12:44
  • I am able to scan the beacon now. But how do I scan only one beacon with UUID? new Region("com.myapp.regionmonitor", UUID, null, null) does not look correct as UUID is string and region requires String,Identifier,Identifier,Identifier as parameters. I am using UUID as in code but not sure if I am correct or not. I need to scan one beacon only and get its major and minor. – Abhinav Tyagi Nov 18 '14 at 15:30
  • 1
    Use `new Region("myUniqueRegionString", Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"), null, null);` – davidgyoung Nov 18 '14 at 16:26
  • Thanks.. I am able to get required details now :) – Abhinav Tyagi Nov 19 '14 at 09:07