Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android信标库-BLE信标检测不工作_Android_Ibeacon_Altbeacon_Beacon_Bluetooth Lowenergy - Fatal编程技术网

Android信标库-BLE信标检测不工作

Android信标库-BLE信标检测不工作,android,ibeacon,altbeacon,beacon,bluetooth-lowenergy,Android,Ibeacon,Altbeacon,Beacon,Bluetooth Lowenergy,我正在尝试用Android检测信标。我创建了一个在后台运行并检测信标的服务 问题是,当蓝牙关闭时,应用程序无法检测到信标。但是如果我打开蓝牙,它就正常工作了。还有一件很奇怪的事。若我在应用程序运行时再次关闭蓝牙,它仍会继续检测。这意味着BLE检测正在工作,但前提是我打开蓝牙并再次关闭它 我如何启用BLE检测?下面是我的实现。我错过什么了吗 信标服务类别 public class BeaconDiscoverer extends Service implements BeaconConsumer

我正在尝试用Android检测信标。我创建了一个在后台运行并检测信标的服务

问题是,当蓝牙关闭时,应用程序无法检测到信标。但是如果我打开蓝牙,它就正常工作了。还有一件很奇怪的事。若我在应用程序运行时再次关闭蓝牙,它仍会继续检测。这意味着BLE检测正在工作,但前提是我打开蓝牙并再次关闭它

我如何启用BLE检测?下面是我的实现。我错过什么了吗

信标服务类别

public class BeaconDiscoverer extends Service implements BeaconConsumer {
    private static final String TAG = BeaconDiscoverer.class.getSimpleName();
    private static BeaconManager beaconManager;
    private static Region region;
    private BackgroundPowerSaver backgroundPowerSaver;

    public BeaconDiscoverer() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        region = new Region("myRangingUniqueId", null, null, null);
        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,d:25-25"));

        configureBatterySaverMode();

        beaconManager.bind(this);
    }

    @Override
    public void onDestroy() {
        beaconManager.unbind(this);
        super.onDestroy();
    }

    private void configureBatterySaverMode() {
        BeaconManager.setAndroidLScanningDisabled(true);
        backgroundPowerSaver = new BackgroundPowerSaver(getApplicationContext());

        // set the duration of the scan to be 5 seconds
        beaconManager.setBackgroundScanPeriod(Utility.convertToMilliseconds(2));
        // set the time between each scan to be 1 min (60 seconds)
        beaconManager.setBackgroundBetweenScanPeriod(Utility.convertToMilliseconds(25));
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "BeaconDiscoverer started up");

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onBeaconServiceConnect() {
        Log.d(TAG, "onBeaconServiceConnect");
        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                    Beacon firstBeacon = beacons.iterator().next();
                    Log.i(TAG, "Beacon detected: " + firstBeacon.getDistance() + " m. - " + firstBeacon.getBluetoothAddress());

                }
            }
        });

        startRanging();
    }

    public void stopRanging() {
        try {
            beaconManager.stopRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void startRanging() {
        if (User.currentUser() == null)
            return;

        try {
            beaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
public class App extends Application {
    private static final String TAG = App.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, BeaconDiscoverer.class));
    }
}

简单回答:您必须启用蓝牙才能使用库检测信标。信标使用蓝牙LE来宣传其存在,蓝牙收音机必须打开才能听到信号

我无法解释为什么您在关闭蓝牙后继续检测信标。一种可能性是,在它们消失之前,您只需在库的短暂扫描周期内从内存缓存中看到它们。另一种可能是,当你关掉Android设备时,它会说蓝牙已经关闭,但实际上它并没有关闭


典型的方法是在应用程序启动时检测蓝牙是否关闭,并提示用户将其打开。参考应用程序中有执行此操作的示例代码。

您想要实现的目标不清楚。我想在蓝牙未关闭时检测信标。我的意思是,如果用户至少一次没有打开蓝牙,ble检测就不起作用。你能提供一些解释吗?接收蓝牙广播。哦,谢谢,我认为在棒棒糖设备检测信标之后,我们不需要启用蓝牙。我只是感到困惑,因为我的手机在我关闭蓝牙后检测到信标。无论如何,我还警告用户打开蓝牙。非常感谢。
 private void registerBoradcastReceiver() {
    IntentFilter stateChangeFilter = new IntentFilter(
            BluetoothAdapter.ACTION_STATE_CHANGED);
    IntentFilter connectedFilter = new IntentFilter(
            BluetoothDevice.ACTION_ACL_CONNECTED);
//IntentFilter connectedFilter = new   IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
    IntentFilter disConnectedFilter = new IntentFilter(
            BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(stateChangeReceiver, stateChangeFilter);
    registerReceiver(stateChangeReceiver, connectedFilter);
    registerReceiver(stateChangeReceiver, disConnectedFilter);
}

    private BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_ACL_CONNECTED == action) {
            startService;
        }
        if (BluetoothDevice.ACTION_ACL_DISCONNECTED == action) {
            stopService;
        }
        if (BluetoothAdapter.ACTION_STATE_CHANGED == action) {

        }
    }
};