-1

我是一名工程专业的法国学生,我正在自学Android语言。我和我的朋友们必须创建一个基于 iBeacon 技术的 Android 应用程序。几天前我发现了 AltBeacon 库,我发现它很棒,但我有一些问题要问。

首先,你必须明白我是一个编程新手,我的问题对你来说是白痴。但请我真的需要帮助;)

Android提供了一个Bluetooth.LE Api,我知道我可以使用startLeScan()方法来获取一个BluetoothDevice。但是,如果我想使用 AltBeacon 库,它是允许我们扫描 iBeacon 设备并获取 Beacon 对象的等效方法?

另一个问题,如果我使用 startLeScan() 并获得一个 BluetoothDevice,我怎样才能将其转换为 Beacon 以使用 AltBeacon 方法?

我很抱歉我的英语错误,我希望我的问题可以理解。再见

4

3 回答 3

3

这是我们用来检测 iBeacon 并使用 AltBeacon 库在 Android 服务中获取信标对象的方法。

设置信标管理器

BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.setForegroundScanPeriod(5100);
beaconManager.setForegroundBetweenScanPeriod(2000);
beaconManager.setBackgroundScanPeriod(5100);
beaconManager.setBackgroundBetweenScanPeriod(2000);

//Parse IBeacon structure
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);

开始测距信标

private void startBeaconRangeFinderService() {
        beaconManager.setRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
            try {
                if (beacons.size() > 0) {
                    for (Beacon b : beacons) {
                        processYourBeaconInThisMethod(b);
                    }
                }
            } catch (Exception ex) {
                Log.e(TAG_BEACON_ACTIVITY, "Error was thrown: " + ex.getMessage());
            }
        }
    });
    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {
        Log.e(TAG_BEACON_ACTIVITY, "Error was thrown: " + e.getMessage());
    }
}
于 2015-09-15T10:28:07.503 回答
1

您可以轻松地使用Android 信标库来扫描信标并使用此处所述的“测距 API”返回结果:

http://altbeacon.github.io/android-beacon-library/samples.html

如果要直接调用startLeScan()并使用库代码将结果转换为信标对象,可以在扫描回调中调用如下方法:

Beacon beacon = beaconParser.fromScanData(scanData, rssi, bluetoothDevice)

但是,如果使用专有的信标格​​式(如来自 Apple 的),您将需要BeaconParser使用正确的布局构建一个。这是专有信息,但您可以进行 Google 搜索以找出构建 BeaconParser专有布局的正确方法。

于 2015-02-28T00:05:20.673 回答
1

我认为nv-bluetooth是从广告包中提取 iBeacon 的最简单方法。下面是onLeScan方法的示例实现。

public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
{
    // Parse the payload of the advertising packet.
    List<ADStructure> structures =
        ADPayloadParser.getInstance().parse(scanRecord);

    // For each AD structure contained in the advertising packet.
    for (ADStructure structure : structures)
    {
        if (structure instanceof IBeacon)
        {
            // iBeacon was found.
            IBeacon iBeacon = (IBeacon)structure;

            // Proximity UUID, major number, minor number and power.
            UUID uuid = iBeacon.getUUID();
            int major = iBeacon.getMajor();
            int minor = iBeacon.getMinor();
            int power = iBeacon.getPower();

            ........

详见“ iBeacon 作为一种 AD 结构”。

于 2015-03-20T17:06:05.653 回答