Arun Ravichandran
Arun Ravichandran

Reputation: 208

Could not find method android.bluetooth.le.BluetoothLeScanner.startScan

Error logs from acer tablet, Acer B1-810 android 4.4.4.I was testing small BLE application , i got this log "Could not find method android.bluetooth.le.BluetoothLeScanner.startScan ", I am confused since BLE is introduced in api level 18 (4.3) here am using Android 4.4.4 , but "Could not find method android.bluetooth.le.BluetoothLeScanner.startScan ". it shows in log.

Upvotes: 0

Views: 1682

Answers (1)

Shashank
Shashank

Reputation: 1117

BluetoothLeScanner was added in api 21 for api below 21 use BluetoothAdapter.startLeScan()

private void startBluetoothLeScan() {
    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    if (Build.VERSION.SDK_INT < 21) {
        bluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

            }
        });
    } else {
        BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
        ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build();
        List<ScanFilter> scanFilters = new ArrayList<>();
        bluetoothLeScanner.startScan(scanFilters, scanSettings, new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
            }

            @Override
            public void onBatchScanResults(List<ScanResult> results) {
            }

            @Override
            public void onScanFailed(int errorCode) {
                super.onScanFailed(errorCode);
            }
        });
    }
}

refer to Bluetooth Low Energy documentation for more information

Upvotes: 3

Related Questions