ALTBEACON库-后台模式?

时间:2018-07-03 14:55:19

标签: android beacon altbeacon

我们正在尝试构建一个可以连续进行Beacon测距并每秒向我们发送一次数据的应用程序。

我们发现的问题是,显示打开时ALTBEACON库运行良好且精确。 一旦我们将显示切换为“关闭”,应用程序就会停止在预定义的时间间隔内发送数据。实际上,Android Studio logcat向我们显示了它甚至没有识别信标,因此它没有发送数据。

我们在其他信标库中没有这个问题,因此我们认为这是程序性问题。 有谁知道为什么会发生这种情况,以及如何防止应用在显示器关闭的情况下停止工作?

清单片段

<?xml version="1.0" encoding="UTF-8"?>

-<manifest android:installLocation="internalOnly" package="package_name" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

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

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
// other permissions 

-<application android:theme="@style/AppTheme" android:supportsRtl="true" android:label="@string/app_name" android:icon="@mipmap/app_icon" android:allowBackup="false">

<activity android:name=".MyView" android:theme="@style/Theme.MyFancyThemeWhiteNoActionBar" android:screenOrientation="portrait"/>
// other activity and services 

-<service android:name="org.altbeacon.beacon.service.BeaconService" tools:node="replace">

<meta-data android:name="longScanForcingEnabled" android:value="true"/>

</service>

</application>

</manifest>

JAVA片段

public class MyView extends AppCompatActivity implements BeaconConsumer {
    Region region;

    @Override
    public void onBeaconServiceConnect() {
        beaconManager.addRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                for (Beacon oneBeacon : beacons) {
                    // doing my task.
                }
            }
        });

        try {
            beaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.landing_view);
        .
        .
        .
        .
        Identifier id1 = Identifier.parse(UUID);
        Identifier id2 = Identifier.parse(MAJOR);
        region = new Region("id", id1, id2, null);
        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.setBackgroundBetweenScanPeriod(0l);
        beaconManager.setForegroundBetweenScanPeriod(0l);

        
        beaconManager.setBackgroundScanPeriod(1000L);
        beaconManager.setForegroundScanPeriod(1000L);
        
        //new BackgroundPowerSaver(this);
        beaconManager.setBackgroundMode(true);
        BluetoothMedic medic = BluetoothMedic.getInstance();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            medic.enablePowerCycleOnFailures(this);
            medic.enablePeriodicTests(this, BluetoothMedic.SCAN_TEST | BluetoothMedic.TRANSMIT_TEST);
        }

        beaconManager.getBeaconParsers().clear();
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
        beaconManager.bind(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (beaconManager != null) {
            try {
                beaconManager.stopRangingBeaconsInRegion(region);
                beaconManager.unbind(this);
            } catch (NullPointerException | RemoteException e) {
                e.printStackTrace();
            }
        }
    }
}

注意我不为后台信标检测创建任何应用程序类。这个任务在前台进行得很顺利,我也需要它作为背景。因此,如果我需要更改任何内容,请发表评论。 我也想在前景和背景中每隔1秒扫描一次信标(当屏幕关闭时)。背景不起作用...

0 个答案:

没有答案