监控信标的Android后台服务Cordova插件

时间:2016-11-10 20:42:05

标签: android-service cordova-plugins ibeacon altbeacon

更新:

我让它在另一部手机上工作。我不知道问题是与图书馆,手机还是Android版本有关。它失败的手机是运行Android 6.0的BLU ADVANCE 4.0 L2。我在运行Android 5.1的其他手机上进行了测试,它可以正常工作。有人可以解释为什么会发生这种情况吗?

我创建了一个基本的后台服务插件,可以在应用程序被杀死时监视Android上的信标。我遇到了一个奇怪的问题。当我的应用程序处于后台/前台时,信标监控工作正常,当我终止应用程序时,我的服务一直在运行,但信标监控停止工作!当我下次打开时,监控会再次开始工作。我正在使用Altbeacon库。我注意到,只要我杀了应用程序,beaconManager就会变为null,并且在你启动应用程序之前它不再绑定。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Log.d(TAG, "onStartCommand run");

    initialiseService();

    beaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
    // To detect proprietary beacons, you must add a line like below corresponding to your beacon
    // type.  Do a web search for "setBeaconLayout" to get the proper expression.
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout(_beaconLayout));
    beaconManager.bind(this);

    return START_STICKY;  
}

我开始在OnBeaconServiceConnect()

中进行监控
@Override
    public void onBeaconServiceConnect() {
        isInitialized = true;
        lastMessage = "Beacon Service Connected";
        send();
        beaconManager.setMonitorNotifier(new MonitorNotifier() {

            @Override
            public void didEnterRegion(Region region) {
                lastMessage = "I just saw an beacon for the first time!";
                //sendMessage();
                send();
                Log.i(TAG, lastMessage);
            }

            @Override
            public void didExitRegion(Region region) {
                lastMessage = "I no longer see an beacon";
                //sendMessage();
                send();
                Log.i(TAG, lastMessage);
            }

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

        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                    //send();
                    Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
                }
            }
        });

        try {
            beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
            beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        } catch (RemoteException e) {    }

    }

这是我尝试使用的另一个插件 - github.com/lulibrary/Background-Beacon-Monitoring我也看到了与此插件相同的行为。杀死应用程序后,服务重新启动,beaconManager不绑定。

以下是我的Android Manifest:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="com.example.sample" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="uk.ac.lancaster.library.backgroundbeacons.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>
        <service android:enabled="true" android:exported="true" android:isolatedProcess="false" android:label="myJourneysBackgroundBeaconService" android:name="uk.ac.lancaster.library.backgroundbeacons.BackgroundBeaconService" />
        <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>
        <service android:enabled="true" android:exported="true" android:isolatedProcess="false" android:label="iBeacon" android:name="org.altbeacon.beacon.service.BeaconService" />
        <service android:enabled="true" android:name="org.altbeacon.beacon.BeaconIntentProcessor">
            <meta-data android:name="background" android:value="true" />
            <intent-filter android:priority="1">
                <action android:name="com.unarin.cordova.beacon.IBeaconPlugin.DID_RANGING" />
                <action android:name="com.unarin.cordova.beacon.IBeaconPlugin.DID_MONITORING" />
            </intent-filter>
        </service>
    </application>
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="24" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>

0 个答案:

没有答案