1

I have the below code used for transmitting iBeacon from an Android app.

Beacon beacon = new Beacon.Builder()
                        .setId1("6fb0e0e9-2ae6-49d3-bba3-3cb7698c77e2")
                        .setId2(Integer.toString(minor1))
                        .setId3(Integer.toString(minor2))
                        .setManufacturer(0x0000)
                        .setTxPower(-59)
                        .setDataFields(Arrays.asList(new Long[] {0l}))
                        .build();
                BeaconParser beaconParser = new BeaconParser()
                        .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
                BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
                beaconTransmitter.startAdvertising(beacon);
            }
        });

Would it be possible for me to get the transmitted data in packets?How do i get those data? Thanks!

Helmi
  • 33
  • 1
  • 10

2 Answers2

0

Assuming that you are going to receive the transmitted data on some Android Device, you could use the same library to do that.

This is how the flow would be,

@Override
public void onCreate(Bundle bundle){
    ...
    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);
    ...
}


@Override
public void onBeaconServiceConnect() {

    /**
     * Start Monitoring the relevant region.
     * */

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

    beaconManager.addMonitorNotifier(new MonitorNotifier() {

        /**
         * Start ranging for Beacons when a region is entered.
         * */

        @Override
        public void didEnterRegion(Region region) {
            try {
                beaconManager.startRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        /**
         * Stop ranging for Beacons on region exit.
         * */
        @Override
        public void didExitRegion(Region region) {
            try {
                beaconManager.stopRangingBeaconsInRegion(region);
            }
            catch (RemoteException e) {
                e.printStackTrace();
            }
             Log.i(TAG, "SERVICE : I no longer see a beacon");
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "SERVICE : I have just switched from seeing/not seeing beacons: " + state);
        }
    });


    beaconManager.addRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {




          }
    });

}
Aneeb Khawar
  • 330
  • 1
  • 8
  • will it show me the data in packet binary format?because that is what i need right now – Helmi Aug 25 '16 at 11:54
  • As you can see The RangeNotifier method is essentially providing a Collection of Beacons in the monitored Region that are currently in range. Every Beacon in this collection indeed has some data associated with it which you can access. [Here](https://altbeacon.github.io/android-beacon-library/javadoc/org/altbeacon/beacon/Beacon.html) is more info on Beacon and the kind of Data you can get. – Aneeb Khawar Aug 25 '16 at 12:28
0

If you simply want to get the bytes of the packet to be transmitted, you can do so like this:

Beacon beacon = new Beacon.Builder()
                    .setId1("6fb0e0e9-2ae6-49d3-bba3-3cb7698c77e2")
                    .setId2(Integer.toString(minor1))
                    .setId3(Integer.toString(minor2))
                    .setManufacturer(0x0000)
                    .setTxPower(-59)
                    .setDataFields(Arrays.asList(new Long[] {0l}))
                    .build();
BeaconParser beaconParser = new BeaconParser()
                    .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
byte[] bytes = p.getBeaconAdvertisementData(beacon);

Using the code above, the array of bytes will look like this:

02 15 6f b0 e0 e9 2a e6 49 d3 bb a3 3c b7 69 8c 77 e2 ...

Note that this byte array isn't the full packet that is transmitted over the air, just the manufacturer data part of it. The packet transmitted over the air is prefixed with a PDU type byte and a length byte, and is also prefixed with a flags PDU. These prefixes, however, are the same for every packet transmitted, and are added automatically by Androids BLE transmission APIs, so they are not included in the bytes returned by getBeaconAdvertisementData().

davidgyoung
  • 63,876
  • 14
  • 121
  • 204