我们正在尝试构建一个不断进行Beacon测距的应用程序,并每秒向我们发送一次数据 .

我们发现的问题是,当显示为ON时,ALTBEACON库工作正常且精确 . 只要我们关闭显示屏,app就会停止在预定义的时间间隔内发送数据 . 事实上,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();
            }
        }
    }
}

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