很长时间后AltBeacon停止扫描

时间:2018-07-03 12:13:51

标签: android bluetooth-lowenergy android-bluetooth altbeacon

我正面临BLE问题。该代码可以正常工作(我正在使用AltBeacon库,但我在Eddystone中也遇到了这个问题),直到没有。经过长时间的扫描,设备仅停止扫描,即使我重新编译,它也完全无法工作,我必须重新启动平板电脑。代码中是否有解决此问题的解决方法?

平板电脑的型号是Multilaser M10A。 库:“ org.altbeacon:android-beacon-library:2 +”

这是Logcat:

07-03 09:01:26.117 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:26.123 932-1760/com.arkmeds.picasso D/BluetoothAdapter: stopLeScan()
07-03 09:01:26.124 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:26.124 932-1760/com.arkmeds.picasso D/BluetoothLeScanner: stopScan
07-03 09:01:26.125 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:26.125 932-1760/com.arkmeds.picasso D/BluetoothAdapter: STATE_ON
07-03 09:01:26.126 932-1760/com.arkmeds.picasso I/BluetoothLeScanner: startRegisteration: mLeScanClients={android.bluetooth.BluetoothAdapter$2@d7aaa9c=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@dde5ca5} ,callback=android.bluetooth.BluetoothAdapter$2@d7aaa9c
07-03 09:01:26.284 932-932/com.arkmeds.picasso D/ActivityThread: SVC-Calling onStartCommand: org.altbeacon.beacon.BeaconIntentProcessor@7a1246, flags=0, startId=1
07-03 09:01:26.290 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:26.293 932-1760/com.arkmeds.picasso D/BluetoothAdapter: startLeScan(): null
07-03 09:01:26.294 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:26.294 932-943/com.arkmeds.picasso I/art: Enter while loop.
07-03 09:01:26.295 932-1760/com.arkmeds.picasso D/BluetoothLeScanner: startScan
07-03 09:01:26.297 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:26.297 932-1760/com.arkmeds.picasso D/BluetoothAdapter: STATE_ON
07-03 09:01:26.299 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:26.303 932-945/com.arkmeds.picasso D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=8
07-03 09:01:26.303 932-945/com.arkmeds.picasso D/BluetoothLeScanner: mClientIf=0
07-03 09:01:26.308 932-1760/com.arkmeds.picasso I/BluetoothLeScanner: startRegisteration: mLeScanClients={android.bluetooth.BluetoothAdapter$2@daf4c07=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@137b134}
07-03 09:01:27.755 932-950/com.arkmeds.picasso D/LIST: Atualizei a lista. :)
07-03 09:01:31.302 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:31.312 932-932/com.arkmeds.picasso D/ActivityThread: SVC-Calling onStartCommand: org.altbeacon.beacon.BeaconIntentProcessor@f350cd2, flags=0, startId=1
07-03 09:01:32.756 932-950/com.arkmeds.picasso D/LIST: Atualizei a lista. :)
07-03 09:01:36.499 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:36.503 932-1760/com.arkmeds.picasso D/BluetoothAdapter: stopLeScan()
07-03 09:01:36.505 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:36.505 932-1760/com.arkmeds.picasso D/BluetoothLeScanner: stopScan
07-03 09:01:36.508 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:36.508 932-1760/com.arkmeds.picasso D/BluetoothAdapter: STATE_ON
07-03 09:01:36.508 932-1760/com.arkmeds.picasso I/BluetoothLeScanner: startRegisteration: mLeScanClients={android.bluetooth.BluetoothAdapter$2@daf4c07=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@137b134} ,callback=android.bluetooth.BluetoothAdapter$2@daf4c07
07-03 09:01:36.674 932-932/com.arkmeds.picasso D/ActivityThread: SVC-Calling onStartCommand: org.altbeacon.beacon.BeaconIntentProcessor@b38aca0, flags=0, startId=1
07-03 09:01:36.690 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:36.692 932-1760/com.arkmeds.picasso D/BluetoothAdapter: startLeScan(): null
07-03 09:01:36.693 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:36.693 932-1760/com.arkmeds.picasso D/BluetoothLeScanner: startScan
07-03 09:01:36.694 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:36.694 932-1760/com.arkmeds.picasso D/BluetoothAdapter: STATE_ON
07-03 09:01:36.696 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:36.699 932-960/com.arkmeds.picasso D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=8
07-03 09:01:36.699 932-960/com.arkmeds.picasso D/BluetoothLeScanner: mClientIf=0
07-03 09:01:36.703 932-1760/com.arkmeds.picasso I/BluetoothLeScanner: startRegisteration: mLeScanClients={android.bluetooth.BluetoothAdapter$2@f7a7059=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@333241e}
07-03 09:01:37.756 932-950/com.arkmeds.picasso D/LIST: Atualizei a lista. :)
07-03 09:01:41.710 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:41.731 932-932/com.arkmeds.picasso D/ActivityThread: SVC-Calling onStartCommand: org.altbeacon.beacon.BeaconIntentProcessor@e8bf2cc, flags=0, startId=1
07-03 09:01:42.757 932-950/com.arkmeds.picasso D/LIST: Atualizei a lista. :)
07-03 09:01:46.897 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:46.899 932-1760/com.arkmeds.picasso D/BluetoothAdapter: stopLeScan()
07-03 09:01:46.900 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:46.900 932-1760/com.arkmeds.picasso D/BluetoothLeScanner: stopScan
07-03 09:01:46.902 932-932/com.arkmeds.picasso D/ActivityThread: SVC-Calling onStartCommand: org.altbeacon.beacon.BeaconIntentProcessor@869642a, flags=0, startId=1
07-03 09:01:46.903 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:46.904 932-1760/com.arkmeds.picasso D/BluetoothAdapter: STATE_ON
07-03 09:01:46.904 932-1760/com.arkmeds.picasso I/BluetoothLeScanner: startRegisteration: mLeScanClients={android.bluetooth.BluetoothAdapter$2@f7a7059=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@333241e} ,callback=android.bluetooth.BluetoothAdapter$2@f7a7059
07-03 09:01:47.090 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:47.093 932-1760/com.arkmeds.picasso D/BluetoothAdapter: startLeScan(): null
07-03 09:01:47.095 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:47.096 932-1760/com.arkmeds.picasso D/BluetoothLeScanner: startScan
07-03 09:01:47.097 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:47.097 932-1760/com.arkmeds.picasso D/BluetoothAdapter: STATE_ON
07-03 09:01:47.099 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:47.105 932-960/com.arkmeds.picasso D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=8
07-03 09:01:47.106 932-960/com.arkmeds.picasso D/BluetoothLeScanner: mClientIf=0
07-03 09:01:47.112 932-1760/com.arkmeds.picasso I/BluetoothLeScanner: startRegisteration: mLeScanClients={android.bluetooth.BluetoothAdapter$2@3e0a21b=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@3eb6fb8}
07-03 09:01:47.759 932-950/com.arkmeds.picasso D/LIST: Atualizei a lista. :)
07-03 09:01:52.102 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:52.109 932-932/com.arkmeds.picasso D/ActivityThread: SVC-Calling onStartCommand: org.altbeacon.beacon.BeaconIntentProcessor@31198f6, flags=0, startId=1
07-03 09:01:52.759 932-950/com.arkmeds.picasso D/LIST: Atualizei a lista. :)
07-03 09:01:57.298 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:57.301 932-1760/com.arkmeds.picasso D/BluetoothAdapter: stopLeScan()
07-03 09:01:57.303 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:57.303 932-1760/com.arkmeds.picasso D/BluetoothLeScanner: stopScan
07-03 09:01:57.305 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:57.305 932-1760/com.arkmeds.picasso D/BluetoothAdapter: STATE_ON
07-03 09:01:57.305 932-1760/com.arkmeds.picasso I/BluetoothLeScanner: startRegisteration: mLeScanClients={android.bluetooth.BluetoothAdapter$2@3e0a21b=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@3eb6fb8} ,callback=android.bluetooth.BluetoothAdapter$2@3e0a21b
07-03 09:01:57.482 932-932/com.arkmeds.picasso D/ActivityThread: SVC-Calling onStartCommand: org.altbeacon.beacon.BeaconIntentProcessor@3c3cf64, flags=0, startId=1
07-03 09:01:57.490 932-932/com.arkmeds.picasso D/BluetoothAdapter: isEnabled
07-03 09:01:57.492 932-1760/com.arkmeds.picasso D/BluetoothAdapter: startLeScan(): null
07-03 09:01:57.493 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:57.493 932-1760/com.arkmeds.picasso D/BluetoothLeScanner: startScan
07-03 09:01:57.494 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:57.494 932-1760/com.arkmeds.picasso D/BluetoothAdapter: STATE_ON
07-03 09:01:57.495 932-1760/com.arkmeds.picasso D/BluetoothAdapter: getLeState() returning 12
07-03 09:01:57.499 932-944/com.arkmeds.picasso D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=8
07-03 09:01:57.499 932-944/com.arkmeds.picasso D/BluetoothLeScanner: mClientIf=0
07-03 09:01:57.503 932-1760/com.arkmeds.picasso I/BluetoothLeScanner: startRegisteration: mLeScanClients={android.bluetooth.BluetoothAdapter$2@ee7f5cd=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@f364e82}
07-03 09:01:57.760 932-950/com.arkmeds.picasso D/LIST: Atualizei a lista. :)

这是扫描的主要代码:

public class BeaconManagment {

    private static BeaconManagment instance;
    private Context context;
    private BeaconConsumer consumer;
    private BeaconManager beaconManager;
    private LeDeviceAdapter adapter;

    private BeaconManagment(BeaconConsumer context, LeDeviceAdapter adapter) {
        beaconManager = BeaconManager.getInstanceForApplication(context.getApplicationContext());
        this.adapter = adapter;
    }

    public static final BeaconManagment getInstance(Context context, LeDeviceAdapter adapter) {
        if (instance == null) {
            instance = new BeaconManagment((BeaconConsumer) context, adapter);
        }
        instance.setContext(context);
        instance.setConsumer((BeaconConsumer) context);

        return instance;
    }

    public void init() {

        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
        beaconManager.bind(consumer);
    }

    public void startConnection() {
        Log.w("Startei conexão", "hello");
        try {
            beaconManager.setForegroundScanPeriod(5000);
            beaconManager.setForegroundBetweenScanPeriod(200);
            beaconManager.setBackgroundScanPeriod(5000);
            beaconManager.setBackgroundBetweenScanPeriod(200);
            BeaconManager.setAndroidLScanningDisabled(true);
            beaconManager.updateScanPeriods();
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        beaconManager.addRangeNotifier((beacons, region) -> {
            List<EddystoneExtensor> extensors = new ArrayList<>();
            for (Beacon beacon: beacons) {
                if (beacon.getServiceUuid() == 0xfeaa && beacon.getBeaconTypeCode() == 0x00) {
                    // Do we have telemetry data?
                    if (beacon.getExtraDataFields().size() > 0) {
                        Long batteryMilliVolts = beacon.getExtraDataFields().get(1);
                        long unsignedTemp = (beacon.getExtraDataFields().get(2) >> 8);
                        double temperature = unsignedTemp > 128 ?
                                unsignedTemp - 256 : unsignedTemp + (beacon.getExtraDataFields().get(2) & 0xff) / 256.0;

                        EddystoneExtensor extensor = new EddystoneExtensor(
                                beacon.getBluetoothAddress(), temperature, batteryMilliVolts.intValue(),
                                beacon.getRssi());

                        extensors.add(extensor);
                    }

                    try {
                        adapter.addDevices(extensors);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        new Thread() {
        @Override
            public void run() {
                try {
                    beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
                } catch (RemoteException e) {
                    Log.w("BEACON_HEARING_FAIL", "deu ruinzão.");
                }
            }
        }.start();
    }

    public void terminate() {
        if (beaconManager != null) {
            beaconManager.unbind(consumer);
        }
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setConsumer(BeaconConsumer consumer) {
        this.consumer = consumer;
    }
}

这是活动:

public class ScanActivity extends AppCompatActivity implements BeaconConsumer {

    private static final int REQUEST_ENABLE_STORAGE = 2;
    private LeDeviceAdapter mLeDeviceAdapter;
    private static final int REQUEST_ENABLE_BT = 1;
    private boolean serviceStarted;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.list_view)
    RecyclerView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        serviceStarted = false;

        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        mLeDeviceAdapter = new LeDeviceAdapter(this);

        int mNoOfColumns = LayoutUtil.calculateNoOfColumns(this);
        gridView.setLayoutManager(new GridLayoutManager(this, mNoOfColumns));
        gridView.setAdapter(mLeDeviceAdapter);

        startService(new Intent(this, TimeService.class));
        startService(new Intent(this, UploadTemperatureService.class));
    }

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

        int PERMISSION_ALL = 1;
        String[] PERMISSIONS = {
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_COARSE_LOCATION
        };

        if(!hasPermissions(this, PERMISSIONS)){
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        } else {
            if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
                BluetoothAdapter.getDefaultAdapter().enable();
            }
            BeaconManagment.getInstance(this, mLeDeviceAdapter).init();
        }
    }

    public boolean hasPermissions(Context context, String... permissions) {
        if (context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if (grantResults.length < 4 ||
                grantResults[0] != 0 ||
                grantResults[1] != 0 ||
                grantResults[2] != 0 ||
                grantResults[3] != 0) {
            finish();
        }
    }

    @Override
    public void onBeaconServiceConnect() {
        new Thread(() -> {
            if (!serviceStarted) {
                BeaconManagment.getInstance(
                        ScanActivity.this, mLeDeviceAdapter).startConnection();
                serviceStarted = true;
            }
        }).start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        BeaconManagment.getInstance(this, mLeDeviceAdapter).terminate();
        serviceStarted = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Intent intent = new Intent(this, UploadTemperatureService.class);
        stopService(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_scan, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_update:
                EddystoneExtensorDAO.clear();
                break;
        }
        return false;
    }
}

0 个答案:

没有答案