获取从iBeacon android传输的数据包数据

时间:2016-08-25 11:03:53

标签: android ibeacon-android

我有以下代码用于从Android应用程序传输iBeacon。

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

我是否可以通过数据包获取传输的数据?如何获取这些数据? 谢谢!

2 个答案:

答案 0 :(得分:0)

假设您要在某些Android设备上接收传输的数据,您可以使用相同的库来执行此操作。

这是流程的方式,

@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) {




          }
    });

}

答案 1 :(得分:0)

如果您只想获取要传输的数据包的字节,可以这样做:

angular.module('myApp').controller('SampleController',
        function ($scope) {
            $scope.ButtonClickMethod = function () {
                    your logic goes here
            };
});

使用上面的代码,字节数组将如下所示:

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

请注意,此字节数组不是通过空中传输的完整数据包,而只是其制造商数据部分。通过空中传输的分组以PDU类型字节和长度字节为前缀,并且还以标志PDU为前缀。但是,这些前缀对于每个传输的数据包都是相同的,并且由Androids BLE传输API自动添加,因此它们不包含在02 15 6f b0 e0 e9 2a e6 49 d3 bb a3 3c b7 69 8c 77 e2 ... 返回的字节中。