1

I tried to get two device name and address inside onScanResult function in Android but I couldn't get these information. As I understand this function call peridicly itself until stop searching and the function give some result about the founded device but I couldn't get how many device found and device information at the same time.

Here is my code :

private ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {

        if("MyBLEdevice".equals(result.getDevice().getName())){
            bleDevice = result.getDevice();
            if(bleScanner !=null){
                bleScanner.stopScan(scanCallback);
            }

        }

        super.onScanResult(callbackType, result);
    }
};

I change my code but I couldn't stop scanResult

boolean isDuplicate = false;
private ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {

        if("MyBLEdevice".equals(result.getDevice().getName())){

            for(BluetoothDevice device : mLeDevices)
            {
                if(device.getAddress().equals(result.getDevice().getAddress())){
                    isDuplicate = true;
                }
                if(!isDuplicate){
                    mLeDevices.add(result.getDevice());
                }
                if(mLeDevices.size()==2)
                {
                    bleScanner.stopScan(scanCallback);
                    bleScanner.flushPendingScanResults(scanCallback);
                }

            }

        }

        super.onScanResult(callbackType, result);
    }
};

1 Answers1

2

You continuously get results, each result is for one device only, from scan even for the same device as soon as new advertisement is send.

To get list with unique devices you should remove duplicates and better choice is to update the device that is already on the list when you receive update for that device.

You can get the 2 specific devices by using device.getName(), make sure that you are advertising names, long names are not send via advertisement, or Service UUIDs by

List<ParcelUuid> parcelUuids = result.getScanRecord().getServiceUuids();

and compare this UUID you created to advertise.

Thracian
  • 43,021
  • 16
  • 133
  • 222
  • Device name and services are the same. Because of this I tried to filter device with address. Is it possible to filter address with List? – Nurullah Burcin Karabay Aug 28 '19 at 17:03
  • 1
    MAC Addresses for devices other than beacons are virtual. MAC address change when you restart the adversing app so trying to filter devices via [MAC address](https://stackoverflow.com/questions/36180407/why-the-address-of-my-bluetoothdevice-changes-every-time-i-relaunch-the-app) won't work. You should either filter with device name or advertising UUID, check with an app from Play Store if you are advertising correctly, i use nRF Connect app it's quite good. – Thracian Aug 28 '19 at 17:10
  • But the device name and services are the same. – Nurullah Burcin Karabay Aug 28 '19 at 17:44