无法使用 "altbeacon" 库 android 监控信标区域

Failing to monitor beacon regions with "altbeacon" libary android

我正在 运行 回调 altbeacon libary. Sample code from here ("Starting an App in the Background" section) 我所在地区的状态变化。但是在尝试了我能想到的一切之后,仍然没有运气。我错过了什么?

我进入我的应用程序的全部内容是:

详情:

编辑:使用测距示例代码我也看不到信标。但是使用 Kontakt 应用程序我可以。根据 BeaconScope 的信标之一:

f7826da6-4fa2-4e98-8024-bc5b71e0893e
id2: 29737
id3: 24354
power: -77 dBm
distance: 0.9 meters
rssi -65dBm
average rssi: -76.5 dBm
packets: 78
packes/sec: 1.4
detection rate: 100%
stabilized: true (sometimes false)
sample period: 53.9 secs

已尝试

代码:

App.java

public class App extends Application implements BootstrapNotifier {
    private static final String TAG = "RegionApp";
    private RegionBootstrap regionBootstrap;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        BeaconManager 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"));
        // Also tried this: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25;

        Region region = new Region("com.example.regionmonitor.boostrapRegion", null, null, null);
        // Also tried creating region with defining UUID, and changing uniqueId

        regionBootstrap = new RegionBootstrap(this, region);
    }

    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        Boolean isInRegion = arg0 == 1;
        Log.d(TAG, "Got a didDetermineStateForRegion call, isInRegion: " + isInRegion.toString());
    }

    @Override
    public void didEnterRegion(Region arg0) {
        Log.d(TAG, "Got a didEnterRegion call");
    }

    @Override
    public void didExitRegion(Region arg0) {
        Log.d(TAG, "Got a didExitRegion call");
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.regionmonitor"
    >

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:name="com.example.regionmonitor.App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >

        <!-- Note:  the singleInstance below is important to keep two copies of your activity from getting launched on automatic startup -->
        <activity
            android:launchMode="singleInstance"
            android:name="com.example.regionmonitor.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

MainActivity.java 应该无关紧要,但为了共享所有代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

在 Android 6+ 上,将位置权限放在清单中已经不够了。您还必须动态地请求用户的许可。在用户授予权限之前,将阻止信标检测(以及所有蓝牙 LE 扫描)。

有关说明,请参阅此处:https://altbeacon.github.io/android-beacon-library/requesting_permission.html

发布的代码适用于 Kontakt 信标(和 IBeacon),但您需要耐心等待。如果您等待的时间足够长,回调将按预期调用,but it might take a while

如果您正在诺基亚(像我一样)进行测试,您可能需要阅读 this