1

I have made my iphone 4s an iBeacon and able to detect this using Locate Beacon app from my Glaxy S4, 5.01, I have also detected this using beacon reference library by modifying its layout found from this question. Its showing the detect beacon in device logs from the library files as below,

onScanResult() - ScanResult{mDevice=6C:64:80:68:86:59, mScanRecord=ScanRecord [mAdvertiseFlags=26, mServiceUuids=null, mManufacturerSpecificData={76=[2, 21, -96, -54, 104, -88, 101, -76, 75, 30, -66, -91, 73, -91, -114, -5, -124, 29, 0, 0, 0, 0, -59]}, mServiceData={}, mTxPowerLevel=-2147483648, mDeviceName=null], mRssi=-46, mTimestampNanos=162979288294680}

i am getting device name always null, and also unable to get the uuid, and i can't get that in my application, can any one please help how can i get beacon info in my application ?

Also How can i uniquely identify a beacon?

Here's what i have done so far, downloaded a ALT beacon library, then ALT beacon library reference, added library dependency, and written the following code in Ranging Activity class,

modified the onCreate method as

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ranging);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().
              setBeaconLayout("m:0-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));  // iBeacons
    beaconManager.bind(this);

}

and implements this by BeaconConsumer, and added his method as

@Override
public void onBeaconServiceConnect() {
    // TODO Auto-generated method stub
    beaconManager.setRangeNotifier(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.");        
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {  e.printStackTrace();  }
}

if i run the code without,

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

then it works fine when add this statement get exception as mentioned before.

Community
  • 1
  • 1
arslan haktic
  • 4,348
  • 4
  • 22
  • 35

2 Answers2

1

Finally i am able to identify my problem, my beacon parser was wrong

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

i was using this while i correct results that detects an ibeacon for me are given by this parser

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

It sounds like you are using beacon Monitoring when you really want to be using beacon Ranging. Monitoring is used to tell you when any one of a group of beacons with shared identifier parts (or any beacon at all) is first detected. But it doesn't tell you the exact identifiers of the beacons in view.

In order to read the identifiers of beacons that are visible at a given time, you should use the Ranging APIs. These APIs give you a callback to your code once per second with a list of all visible beacons matching the Region you define.

@Override 
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
     for (Beacon beacon: beacons) {
        Log.i(TAG, "I see a beacon with identifiers: "+beacon.getId1()+" "+beacon.getId2()+" "+beacon.getId3());        
     }
}

See the Ranging Example Code on this page for more details on how to set this up.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • 2
    Thanks for your response, i really do admire your great work for beacons especially, I have followed Ranging Example Code as well in the beacon reference library code, but that throughs an exception as "06-19 14:28:58.611: E/AndroidRuntime(23645): Caused by: java.lang.UnsupportedOperationException 06-19 14:28:58.611: E/AndroidRuntime(23645): at java.util.Collections$UnmodifiableCollection.add(Collections.java:928) " and this caused, when i add beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); line. – arslan haktic Jun 19 '15 at 10:10
  • 2
    You need to add the beaconParser before you do anything else with the library. Put it in the first line after you get an instance of the beaconManager. For your other exception, please show two code excerpts of what causes the exception (1) the initial setup where you set the beacon parser and (2) your didRangeBeaconsInRegion method. – davidgyoung Jun 19 '15 at 14:46
  • 3
    i have edit the question, please check. Thanks for your valuable tim. – arslan haktic Jun 20 '15 at 18:00
  • 1
    The issue is that the beacon parser needs to be added when the beaconManager is first constructed. Since you are using the reference application, this must happen in the BeaconReferenceApplication.onCreate method **NOT** the RangingActivity.onCreate method – davidgyoung Jun 20 '15 at 18:11
  • 2
    I have written the same code in BeaconReferenceApplication.onCreate but didRangeBeaconsInRegion never calls :( i am detecting the iBeacon using locate app in ipad, beacon detection is shown in logcat but no response in code. – arslan haktic Jun 20 '15 at 23:34
  • I am only talking about the beacon parser. Move that to the BraconReferenceApplication class. You can leave the ranging code where it is. – davidgyoung Jun 20 '15 at 23:40
  • 1
    also tried this, i have added this mBeaconManager = BeaconManager.getInstanceForApplication(this); mBeaconManager.getBeaconParsers().add(new BeaconParser(). setBeaconLayout("m:0-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); while in ranging activity beaconManager = BeaconManager.getInstanceForApplication(this); beaconManager.bind(this); but still no detections, i will be grateful if you can refer some working code, i just need to detect an ibeacon from my android s4 5.0.1 device. Thanks – arslan haktic Jun 21 '15 at 00:19
  • Sorry you are having trouble -- it should be very simple! Just start over with a clean copy of the reference app, then replace [this line](https://github.com/AltBeacon/android-beacon-library-reference/blob/master/app/src/main/java/org/altbeacon/beaconreference/BeaconReferenceApplication.java#L41) with your parser expression shown above. – davidgyoung Jun 21 '15 at 02:34
  • 1
    m really stuck at this, i have tried multiple times with new project, is there any possibility of problem with my devices ? I make an Ibeacon using locate app from iPad, that while i detect by locate app in android s4, it detects this as ibeacon. But not working while i write my own code. – arslan haktic Jun 21 '15 at 05:45
  • Perhaps it is a device or beacon-specific problem with the latest version of the library, yes. Please try running this test version of the reference app, follow the instructions, and let me know that you see. https://github.com/AltBeacon/android-beacon-library-reference/releases/tag/adhoc1 – davidgyoung Jun 21 '15 at 13:34
  • 1
    Thanks a lot for your timely response and support, finally i am able to identify my problem, my beacon parser was wrong beaconManager.getBeaconParsers().add(new BeaconParser(). setBeaconLayout("m:0-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); i was using this while i correct results are given by this beaconManager.getBeaconParsers().add(new BeaconParser(). setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24")); Thanks again – arslan haktic Jun 21 '15 at 18:28
  • Glad you figured it out! Would you mind taking the results of your last comment and putting it into an answer of your own and accepting it? That way other folks who find this question will easily see what you did to fix the problem. – davidgyoung Jun 21 '15 at 18:30