蓝牙BLE ScanCallback停止工作

时间:2019-03-25 19:04:20

标签: android bluetooth-lowenergy android-bluetooth

我正在研究BLE设备的android应用程序。我正在尝试使用scanCallBack获得可用的蓝牙设备。几天前工作正常,但突然停了下来。 onScanResults或onScanFailed没有执行任何操作。我在清单中给出了ACCESS_COARSE_LOCATION,BLUETOOTH_ADMIN,BLUETOOTH和ACCESS_FINE_LOCATION并在运行时请求了权限。蓝牙处于打开状态,位置也处于打开状态。请帮忙。

Measurement.java

public class MeasurementScreen extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
static int REQUEST_ENABLE_BT = 1001;
private Boolean spinnerStatus= false;
private Handler handler = new Handler(Looper.getMainLooper());
final BluetoothAdapter mBluetoothAdapter = 
BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.device_reading_screen);
    Objects.requireNonNull(getSupportActionBar()).hide();
    if (Build.VERSION.SDK_INT >= 23) {
        int permissionCheck = ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION);
        if (permissionCheck == -1 ) {
            ActivityCompat.requestPermissions(this,
                    new String[]{
                            Manifest.permission.ACCESS_COARSE_LOCATION
                    }, 0);
        }
    }
    final Button tryAgain = findViewById(R.id.tryagain);
    next = findViewById(R.id.next);
    back = findViewById(R.id.back);
    progressDialog = new ProgressDialog(this);
    bluetoothscan();

  }

  void bluetoothscan(){
    spinner();
    scanBLEDevices(true);
  }

    private void scanBLEDevices(final boolean enable){
    final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

    final BLEScanCallback scanCallback = new BLEScanCallback();
    if (enable){
        // Stops scanning after a pre-defined scan period.
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                bluetoothLeScanner.stopScan(scanCallback);
            }
        }, 10000);

        mScanning = true;
        bluetoothLeScanner.startScan(scanCallback);
    }else{
        mScanning = false;
        bluetoothLeScanner.stopScan(scanCallback);
    }
  }


    public class BLEScanCallback extends ScanCallback{
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);
        Log.e("Scan Success", "Scan Success");
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
        Log.e("Scan Success", "Scan Success Batch");
    }

    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
        Log.e("Scan Failed", "Error Code: " + errorCode);
    }
  }
}

这是日志输出:-

 D/BluetoothAdapter: STATE_ON
 D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=6 
 mClientIf=0

0 个答案:

没有答案