Scanning an Android BLE device with a filter does not work

I work with android BLE (Bluetooth Low Energy). I am having problems scanning a BLE device using the method startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)while it is startLeScan(BluetoothAdapter.LeScanCallback callback)working fine.
When I use a filter to scan for specific serviceUUIDs, the callback fails. I am testing the samsung galaxy s6.
I want to know if this problem is device specific or if there is some error in the scan function.

+4
source share
4 answers

I am sure this is not a device. First of all, as mentioned in IshArt, you should use startScan from Android 5.0 on.

startScan(List<ScanFilter> filters, ScanSettings settings, ScanCallback callback)

In my experience, the Scanfilters implementation works fine if you go for MAC addresses, but other filter settings are problematic:

ScanFilter filter = new ScanFilter.Builder().setDeviceAddress(deviceMacAddress).build();
filters.add(filter);

If this is not an option for you, you can also implement your own filter. If you leave the filter list for startScan () empty, it ignores all filtering and gets everything. Then in the callback you can write your own method to check if the result matches your requirements or not.

+5
source

, Samsung Galaxy S6. API- Android 5.0+. , S4, ( S5), S7 S8 ; , S6 .

, , mac , .

Update

, Galaxy S6.

ScanFilter, (Kotlin):

ScanFilter.Builder()
    .setServiceUuid(BluetoothBlind.Service.ParcelUUID)
    .setDeviceAddress(macAddress)
    .build()

:

ScanFilter.Builder()
    .setDeviceAddress(macAddress)
    .build()

ScanFilter.Builder()
    .setServiceUuid(BluetoothBlind.Service.ParcelUUID)
    .build()
+1

2 :

//Device scan callback Lollipop and above
    private ScanCallback generateScanCallback(){

        if(apiVersion> Build.VERSION_CODES.KITKAT) {
            ScanCallback mScanCallback = new ScanCallback() {
                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                    super.onScanResult(callbackType, result);
                    final BluetoothDevice device = result.getDevice();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e(TAG, "running scan " + device.getAddress());
                            if (device.getAddress().equals(mDeviceAddress)) {
                                Log.e(TAG, "device founded, trying to connect");
                                scanLeDevice(false);
                                Intent gattServiceIntent = new Intent(mContext, BluetoothLeService.class);
                                mContext.bindService(gattServiceIntent, mServiceConnection, mContext.BIND_AUTO_CREATE);
                                mIndicationText.setText(mContext.getString(R.string.waiting));
                            }
                        }
                    });
                }
            };
            return mScanCallback;
        }
        return null;
    }


// Device scan callback KITKAT and below.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
    new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.e(TAG,"running scan "+device.getType());
                    if(device.getAddress().equals(mDeviceAddress)){
                        Log.e(TAG, "device founded, trying to connect");
                        scanLeDevice(false);
                        Intent gattServiceIntent = new Intent(mContext, BluetoothLeService.class);
                        mContext.bindService(gattServiceIntent, mServiceConnection, mContext.BIND_AUTO_CREATE);
                        mIndicationText.setText(mContext.getString(R.string.waiting));
                    }
                }
            });
        }
    };

:

if(apiVersion> Build.VERSION_CODES.KITKAT) {
    scanner = mBluetoothAdapter.getBluetoothLeScanner();
    // Device scan callback LOLLIPOP
    scanner.startScan(generateScanCallback());
} else {
    mBluetoothAdapter.startLeScan(mLeScanCallback);
}

, , 2 android 5 android OS

0

The problem with checking BLE filters is a known issue. See https://github.com/iDevicesInc/SweetBlue/wiki/Android-BLE-Issues for this and other BLE issues. The conclusion is simple: "You must scan all devices and filter yourself."

0
source

Source: https://habr.com/ru/post/1618422/


All Articles