当前位置: 首页 > 知识库问答 >
问题:

Android Beacon在应用程序关闭后停止扫描(在最近的应用程序列表中删除)

鄢飞鸾
2023-03-14
package com.example.budakgigibesi.ibeacon_scan;

import android.app.Activity;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.powersave.BackgroundPowerSaver;
import org.altbeacon.beacon.startup.BootstrapNotifier;
import org.altbeacon.beacon.startup.RegionBootstrap;

import java.util.Collection;

public class MainActivity extends AppCompatActivity implements BootstrapNotifier, BeaconConsumer, RangeNotifier {

    private BeaconManager beaconManager;
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;

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

        //enable beacon features///////////////////////////////////////////////////////////////////////

        beaconManager = BeaconManager.getInstanceForApplication(this);
        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"));
        beaconManager.setEnableScheduledScanJobs(false);    // disable JobScheduler-based scans (used on Android 8+)
        beaconManager.setBackgroundBetweenScanPeriod(0);    // set the time between each scan to be 1 hour (3600 seconds)
        beaconManager.setBackgroundScanPeriod(1100);    // set the duration of the scan to be 1.1 seconds

        Region region = new Region("backgroundRegion", null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);    // wake up the app when a beacon is seen

        backgroundPowerSaver = new BackgroundPowerSaver(this);  //This reduces bluetooth power usage by about 60%

        beaconManager.bind(this);   //for beacon RangeNotifier
        //////////////////////////////////////////////////////////////////////////////////////////////
    }

    //altbeacon bootstrapnotifier///////////////////////////////////////////////////////////////////////
    @Override
    public void didEnterRegion(Region region) {
        TextView mTextView = (TextView) findViewById(R.id.id_tv);
        mTextView.setText( "found a beacon");

        // 
        try {
            beaconManager.startRangingBeaconsInRegion(region);  //Tells the BeaconService to start looking for beacons that match the passed Region object, and providing updates on the estimated mDistance every seconds while beacons in the Region are visible.
        }
        catch (RemoteException e) {
            if (BuildConfig.DEBUG) Log.d("RangeNotifier", "Can't start ranging");
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////

    //altbeacon BeaconConsumer///////////////////////////////////////////////////////////////////////
    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        if (beacons.size() > 0) {
            Log.i("beaconconsumer", "didRangeBeaconsInRegion called with beacon count:  "+beacons.size());
            for (Beacon b : beacons) {
                Log.i("beaconconsumer", "The first beacon " + b.toString() + " is about " + b.getDistance() + " meters away.");
                TextView mTextView = (TextView) findViewById(R.id.id_tv);
                mTextView.setText( "The first beacon " + b.toString() + " is about " + b.getDistance() + " meters away.");
            }
        }catch (RemoteException e) {   }
    }

    @Override
    public void onBeaconServiceConnect() {
        beaconManager.addRangeNotifier(this);
    }

    @Override 
    protected void onPause() {
        super.onPause();
        if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(true);
    }

    @Override 
    protected void onResume() {
        super.onResume();
        if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false);
    }




}
10-30 13:24:40.263 14712-30801/? D/BtGatt.btif: btif_gattc_upstreams_evt: Event 4096
10-30 13:24:40.263 14712-30801/? D/BtGatt.GattService: onScanResult() - address=7C:64:56:83:5E:71, rssi=-86
10-30 13:24:40.323 7928-7945/com.example.budakgigibesi.ibeacon_scan D/BluetoothAdapter: stopLeScan()
10-30 13:24:40.323 14712-14847/? D/BtGatt.GattService: stopScan() - queue=1
10-30 13:24:40.333 14712-14847/? D/BtGatt.GattService: stopScan() - queue empty; stopping scan
10-30 13:24:40.333 14712-14847/? D/BtGatt.btif: btif_gattc_scan
10-30 13:24:40.333 14712-30801/? D/BtGatt.btif: btgattc_handle_event: Event 1003
10-30 13:24:40.343 14712-14723/? D/BtGatt.GattService: unregisterClient() - clientIf=5
10-30 13:24:40.343 14712-14723/? D/BtGatt.btif: btif_gattc_unregister_app
10-30 13:24:40.353 14712-30801/? D/BtGatt.btif: btgattc_handle_event: Event 1001
10-30 13:24:40.353 14712-30801/? D/BtGatt.btif: btif_gattc_upstreams_evt: Event 1
10-30 13:24:40.363 7928-7945/com.example.budakgigibesi.ibeacon_scan D/BluetoothAdapter: startLeScan(): null
10-30 13:24:40.363 14712-14724/? D/BtGatt.GattService: registerClient() - UUID=6f27a4f0-4631-405a-8651-07c58b62f50d
10-30 13:24:40.373 14712-14724/? D/BtGatt.btif: btif_gattc_register_app
10-30 13:24:40.373 14712-30801/? D/BtGatt.btif: btgattc_handle_event: Event 1000
10-30 13:24:40.373 14712-30801/? D/BtGatt.btif: btif_gattc_upstreams_evt: Event 0
10-30 13:24:40.373 14712-30801/? D/BtGatt.GattService: onClientRegistered() - UUID=6f27a4f0-4631-405a-8651-07c58b62f50d, clientIf=5
10-30 13:24:40.373 7928-7998/com.example.budakgigibesi.ibeacon_scan D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
10-30 13:24:40.383 14712-30801/? D/BtGatt.GattService: startScan() - queue=0
10-30 13:24:40.383 14712-30801/? D/BtGatt.GattService: startScan() - adding client=5
10-30 13:24:40.383 14712-30801/? D/BtGatt.btif: btif_gattc_scan
10-30 13:24:40.383 14712-30801/? D/BtGatt.btif: btgattc_handle_event: Event 1002
10-30 13:24:40.463 932-14195/? I/ActivityManager: Killing 7928:com.example.budakgigibesi.ibeacon_scan/u0a315 (adj 16): remove task
10-30 13:24:40.673 14712-16859/? D/BtGatt.GattService: Binder is dead - unregistering client (5)!
10-30 13:24:40.673 14712-16859/? D/BtGatt.GattService: stopScan() - queue=1
10-30 13:24:40.673 14712-16859/? D/BtGatt.GattService: stopScan() - queue empty; stopping scan
10-30 13:24:40.673 14712-16859/? D/BtGatt.btif: btif_gattc_scan
10-30 13:24:40.673 14712-30801/? D/BtGatt.btif: btgattc_handle_event: Event 1003
10-30 13:24:40.673 932-1593/? W/ActivityManager: Scheduling restart of crashed service com.example.budakgigibesi.ibeacon_scan/org.altbeacon.beacon.service.BeaconService in 1000ms
10-30 13:24:40.683 14712-16859/? D/BtGatt.GattService: unregisterClient() - clientIf=5
10-30 13:24:40.683 14712-16859/? D/BtGatt.btif: btif_gattc_unregister_app
10-30 13:24:40.683 14712-30801/? D/BtGatt.btif: btgattc_handle_event: Event 1001
10-30 13:24:40.683 14712-30801/? D/BtGatt.btif: btif_gattc_upstreams_evt: Event 1
10-30 13:24:41.204 932-932/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
10-30 13:24:41.204 1189-1189/? D/StatusBar.NetworkController: refreshViews: Data not connected!! Set no data type icon / Roaming
10-30 13:24:41.204 932-932/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
10-30 13:24:41.294 406-1066/? D/QC-QMI: Thread state: conn_id=18, state=RX_THREAD_WAKELOCK_ACQUIRE
10-30 13:24:41.294 406-1066/? D/QC-QMI: Thread state: conn_id=18, state=RX_THREAD_WAIT_READ
10-30 13:24:41.294 406-1066/? D/QC-QMI: Thread state: conn_id=18, state=RX_THREAD_CLIENT_TX
10-30 13:24:41.294 406-1066/? D/QC-QMI: qmi_qmux: TX/RX - RX 95 bytes on conn_id=18
10-30 13:24:41.294 406-1066/? D/QC-QMI: qmuxd: TX message on fd=24, to qmux_client_id=0x3, len=129
10-30 13:24:41.294 406-1066/? D/QC-QMI: Thread state: conn_id=18, state=RX_THREAD_WAKELOCK_RELEASE
10-30 13:24:41.294 406-1066/? D/QC-QMI: Thread state: conn_id=18, state=RX_THREAD_WAIT_POLL

共有1个答案

柯苗宣
2023-03-14

我设法解决了这个问题。下面是更正的代码。我把它分成了两个班。一个是应用类的,一个是蓝牙扫描类的。当应用程序关闭时(在最近的应用程序列表中划掉它),应用程序类(MyApplication)将再次启动,并启动蓝牙扫描类(ScanBT)。

myapplication.java

public class MainApplication extends Application {      

    public void onCreate() {
        super.onCreate();
        Intent intent = new Intent(this, ScanBT.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent); 
    }


}

java

public class ScanBT extends AppCompatActivity implements BootstrapNotifier, BeaconConsumer, RangeNotifier {

    private BeaconManager beaconManager;
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     
        Log.i("foxy", "ScanBT - i am in onCreateeeeeeeee");

        //enable beacon features///////////////////////////////////////////////////////////////////////     
        beaconManager = BeaconManager.getInstanceForApplication(this);
        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"));
        beaconManager.setEnableScheduledScanJobs(false);    
        beaconManager.setBackgroundBetweenScanPeriod(0);    
        beaconManager.setBackgroundScanPeriod(1100);    

        Region region = new Region("backgroundRegion", null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);    

        backgroundPowerSaver = new BackgroundPowerSaver(this);  

        beaconManager.bind(this);   
        //////////////////////////////////////////////////////////////////////////////////////////////
    }

    //altbeacon bootstrapnotifier///////////////////////////////////////////////////////////////////////
    @Override
    public void didEnterRegion(Region region) {
        Log.i("foxy", "ScanBT - a beacon entered the region");


        try {
            beaconManager.startRangingBeaconsInRegion(region);  
        }
        catch (RemoteException e) {
            if (BuildConfig.DEBUG) Log.d("RangeNotifier", "Can't start ranging");
        }
    }

    @Override
    public void didDetermineStateForRegion(int state, Region region) {

    }   

    @Override
    public void didExitRegion(Region region) {

    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////

    //altbeacon BeaconConsumer///////////////////////////////////////////////////////////////////////
    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        if (beacons.size() > 0) {
            Log.i("foxy", "ScanBT - didRangeBeaconsInRegion called with beacon count:  "+beacons.size());
            for (Beacon b : beacons) {
                Log.i("foxy", "ScanBT - The first beacon " + b.toString() + " is about " + b.getDistance() + " meters away.");
            }
        }
    }

    @Override
    public void onBeaconServiceConnect() {
        beaconManager.addRangeNotifier(this);
    }

    @Override 
    protected void onPause() {
        super.onPause();
    }

    @Override 
    protected void onResume() {
        super.onResume();
    }

    @Override 
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }
}
 类似资料:
  • 我试图重新创建Connect四,我成功了。但我想通过频繁地切换颜色,给玩家一个获胜的四张光盘在哪里的指示。我对线程和编程中的时间概念是新的。 我也成功地给了用户这个指示,但是在关闭应用程序之后,控制台仍然会给出输出,也是在使用SetonCloserEquest时。 代码如下:

  • 后台服务是停止的,从最近在oppo&vivo手机中删除我的应用程序时,&广播累西弗在那种情况下也不工作。

  • 我正在用场景构建器创建一个JavaFX应用程序。我在开头加了一段视频。所以我想播放视频之前,我的应用程序启动在全屏模式。问题是当它停止时,我只看到黑色尖叫,什么也没发生,我想这是因为视频是全屏的,它没有自动关闭。 我也有一个bug,在视频开始之前,我的主窗口的一些闪烁。我想这是因为视频放在控制器中,在我的应用程序启动后开始。 公共类主扩展应用程序{ 控制器类:

  • 编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答问题。 我的主要活动: 我的清单: 我的下一个代码: 下一个: 对于堆栈跟踪排序: 我的xml文件显示第一页: 要显示第二个选项卡的我的xml文件: 最后我想告诉你,我已经坚持了三天了。请更正此代码。中的“TabListener”接口不可用。但忽略它,在几毫秒后运行我的应用程序,然后“很遗憾,你的应用程序停止工作

  • 我正在开发一款针对ICS手机的应用程序。 在应用程序中,我有一个飞溅屏幕和几个其他屏幕,可以从飞溅屏幕或通过NFC触摸启动。我的一个活动包含以下意图过滤器: 由于某些原因,我无法理解,每当启动包含上述意图过滤器的活动时,当用户按下“主页”按钮时,该活动不会出现在“最近的应用”列表中。通过调试器,我验证了它没有被销毁,只是停止了。 如果启动屏幕在ThingActivity之前打开,则启动将显示在最近

  • 我的iOS应用程序在出现运行时错误后停止运行。我将错误捕获为异常。我希望应用程序在错误处理后继续运行到下一步。有人建议怎么做吗?