代码之家  ›  专栏  ›  技术社区  ›  user3337849

如何让AltBeacon库的BootstrapRegion识别iBeacon布局?

  •  2
  • user3337849  · 技术社区  · 11 年前

    我在使用参考, http://altbeacon.github.io/android-beacon-library/samples.html 。我还使用了 How to detect Region Enter/Exit for multiple beacons using AltBeacon android-beacon-library?

    我正在尝试使用AltBeacon的Android Beacon Library在后台检测iBeacons。我包含了项目中的以下代码片段。到目前为止,我没有在背景中检测到ibeacons。。。谢谢你的帮助

    我正在使用Beacon Manager

    setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    

    然而,我没有得到错误和信标检测。在三星Galaxy 4设备上调试运行应用程序时,应用程序会启动,但不会检测到活动信标。我的信标是雷达信标,配置为iBeacons。Rad Beacon应用程序检测到它们,我在前台运行的其他AltBeacon库应用程序也检测到我的iBeacons。这些应用程序正在三星Galaxy 4上运行。

    我为背景信标检测设置的应用程序。。。不检测iBeacons。

    这是我的代码。对Constants.java的引用只是我的应用程序的一个常量文件。

    package com.myApp.BTleDemo;
    
    import android.app.Application;
    import android.content.Intent;
    import android.util.Log;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    import java.util.Locale;
    
    
    
    import org.altbeacon.beacon.startup.BootstrapNotifier;
    import org.altbeacon.beacon.startup.RegionBootstrap;
    import org.altbeacon.beacon.Region;
    import org.altbeacon.beacon.BeaconConsumer;
    import org.altbeacon.beacon.BeaconManager;
    import org.altbeacon.beacon.BeaconParser;
    import org.altbeacon.beacon.MonitorNotifier;
    import org.altbeacon.beacon.RangeNotifier;
    import org.altbeacon.beacon.Region;
    import org.altbeacon.beacon.Identifier;
    
    
    
    
    public class BackgroundMode extends Application implements BootstrapNotifier{
        private static final String TAG = ".BackgroundMode";
        private RegionBootstrap regionBootstrap;
    
        private BeaconManager beaconManager;
    SharedPreferences prefs;
    List<Region> regions;
        public void onCreate() {
            super.onCreate();
        Log.d(TAG, "App started up");
    
    
      beaconManager = BeaconManager.getInstanceForApplication(this);
      // Add AltBeacons Parser for iBeacon 
      beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    
    
        // wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
    
      Region region = new Region("com.myApp.BTleDemo.boostrapRegion", Identifier.parse(Constants.BT_UUID), 
      Identifier.fromInt(Constants.BT_MAJOR), Identifier.fromInt(Constants.BT_MINOR));
    
            regionBootstrap = new RegionBootstrap(this, region);
    
    
    }
    
    
    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        // Don't care
    }
    
    @Override
    public void didEnterRegion(Region arg0) {
        Log.d(TAG, "Got a didEnterRegion call");
    
        // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
        // if you want the Activity to launch every single time beacons come into view, remove this call.  
        regionBootstrap.disable();
        Intent intent = new Intent(this, MainActivity.class);
        // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
        // created when a user launches the activity manually and it gets launched from here.
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }
    
    @Override
    public void didExitRegion(Region arg0) {
        // Don't care
    }  
    
    
    /*
    @Override
    public void onBeaconServiceConnect() {
        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) {   }
    }
    }
    */
    }
    

    我没有得到任何ibeacon检测。没有发布预期的LogCat列表。我是不是漏了一步?

    2 回复  |  直到 8 年前
        1
  •  5
  •   davidgyoung    11 年前

    编辑project.properties文件并添加以下行:

    manifestmerger.enabled=true
    

    请参阅此处的完整说明:

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

    问题与您试图识别的信标类型无关。如果没有启用清单合并,AndroidManifest.xml文件就没有从库继承的服务定义来启动Beacon service,因此不会检测到任何类型的信标。

    使用Eclipse,您可以通过执行构建,然后查看bin/AndroidManifest.xml中生成的清单来判断清单合并是否正常工作。

    如果所有其他操作都失败,您可以手动将这些条目复制到项目清单中:

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    
    <!-- nest the entries below underneath your application tag -->
    
        <service android:enabled="true" android:exported="true" android:isolatedProcess="false" android:label="beacon" android:name="org.altbeacon.beacon.service.BeaconService">
        </service>
        <service android:enabled="true" android:name="org.altbeacon.beacon.BeaconIntentProcessor">
        </service>
        <receiver android:name="org.altbeacon.beacon.startup.StartupBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
            </intent-filter>
        </receiver>
    
        2
  •  5
  •   davidgyoung    11 年前

    如果你根本没有检测到信标,请确保图书馆的信标服务正在运行,并注意到蓝牙设备。为此,请在 onCreate 应用程序或活动的方法:

    mBeaconManager.setDebug(true);
    

    在捕获LogCat结果的同时运行应用程序。你应该寻找下面这样的行来告诉你信标服务是否成功启动:

    I/BeaconService( 7140): beaconService version 2.0-beta4 is starting up
    I/BeaconService( 7140): binding
    I/BeaconService( 7140): start ranging received
    D/BeaconService( 7140): Currently ranging 1 regions.
    

    如果您看到类似这样的行,那么这意味着服务正在成功启动。然后打开你的信标,寻找下面这样的线路,它会告诉你你是否检测到蓝牙设备,以及它们是否被识别为信标:

    D/BtGatt.GattService( 2006): onScanResult() - address=00:07:80:02:60:20, rssi=-65
    D/BeaconService( 1772): got record
    D/BeaconParser( 1772): This is a recognized beacon advertisement -- beac seen
    D/BeaconService( 1772): beacon detected multiple times in scan cycle :id1: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6 id2: 1 id3: 3
    
    
    D/BtGatt.GattService( 2006): onScanResult() - address=D4:CB:08:6F:DA:AD, rssi=-82
    D/BeaconService( 7140): got record
    D/BeaconParser( 7140): This is not a matching Beacon advertisement.  (Was expecting be ac.  The bytes I see are: 0201061aff0000001012f234454cf6d4a0fadf2f4911ba9ffa600010001b90000000000000000000000000000000000000000000000000000000000000000
    

    第一个片段显示了被识别为信标的蓝牙设备的成功解码。第二个片段显示检测到未被识别为信标的蓝牙设备。如果你没有看到任何类似的片段,那么这表明服务没有运行,或者根本没有检测到蓝牙设备。

    一旦您获得了这样的LogCat捕获,请将其摘录添加到您的问题中,这有助于进一步排除故障。