android BLE扫描无法正常运行

时间:2015-02-09 18:15:14

标签: android service bluetooth-lowenergy android-bluetooth

如何在Android中的后台服务中扫描信标设备。

我试过

 _bluetoothAdapter.startLeScan(mLeScanCallback);


private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi,
                byte[] scanRecord) {

            Log.i(Utils.LOG_TAG,"BLE packet received");

            IBeacon newBeacon = parseAdvertisementData(scanRecord);
            if(newBeacon == null)
                return;

            newBeacon.setMacAddress(device.getAddress());

            // If already discovered, then just refresh the RSSI of the existing instance and return
            if(_arrOrderedIBeacons.contains(newBeacon)){
                int newDistance = (int)calculateDistance(newBeacon.getPowerValue(), rssi);
                IBeacon previousIBeaconInfo = findIfExists(newBeacon);
                int oldDistance = previousIBeaconInfo.getProximity();
                if(newDistance < oldDistance){
                    Log.i(Utils.LOG_TAG,"Updating distance");
                    previousIBeaconInfo.setProximity(newDistance);
                    // Sort again
                    Collections.sort(_arrOrderedIBeacons, new IBeaconProximityComparator());
                }
                return;
            }

            newBeacon.setEasiBeacon(false);
            if(device.getName() != null){
                if(device.getName().startsWith(EASIBEACON_IDPREFIX)){
                    newBeacon.setEasiBeacon(true);
                    String version = device.getName().substring(EASIBEACON_IDPREFIX.length());
                    newBeacon.setVersionModel(version);
                    if(newBeacon.getVersion() == 1){
                        // Version 1 is always connectable
                        newBeacon.setConnectable(true);
                    }else if(newBeacon.getVersion() == 2){
                        newBeacon.setConnectable(getConnectable(scanRecord));
                        if(!newBeacon.isConnectable())
                            newBeacon.setEasiBeacon(false); //If not connectable we will report it as unknown 
                    }                   
                }
            }
            // Review this
            Log.i(Utils.LOG_TAG,device.getName() + " " + device.getAddress() + " " + newBeacon.getPowerValue() + " " + rssi + " Connectable: " + newBeacon.isConnectable());
            newBeacon.setProximity((int)calculateDistance(newBeacon.getPowerValue(), rssi));

            if(!_arrOrderedIBeacons.contains(newBeacon)){
                _arrOrderedIBeacons.add(newBeacon);
                Collections.sort(_arrOrderedIBeacons, new IBeaconProximityComparator());
                _listener.beaconFound(newBeacon);

                // Every time a new beacon is found, reset the timeout
                _timeoutHandler.removeCallbacks(timeoutTask);
                _timeoutHandler.postDelayed(timeoutTask, IBeaconProtocol.SCANNING_PERIOD);

            }   
       }

    };

上面的代码扫描前台/活动应用程序中的设备工作正常。

但是当我把这个逻辑投入使用时它就不起作用了。

请建议如何在android后台服务中扫描信标设备。

提前致谢。

0 个答案:

没有答案
相关问题