Samra
Samra

Reputation: 2015

Scanning BLE device not working

I am trying to scan BLE devices with

mBluetoothAdapter.startLeScan(this);

(I know its obsolete for newer versions but just to see it works with my phone [4.4], I am using it). So it starts scanning and then moves on without giving errors but no device is detected. OnLEScan event is also fired but the device parameter in it is null. My LE device is right there and connected.

On googling, I found this happens if the BluetoothAdapter doesnot have a UUID. How do I set UUID? When is OnLeScan called/fired? Is there any other solution?

My callback code goes here

//BluetoothAdapte.LEScanCallBack on MainActivity
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord){
    Log.i(TAG, "New LE Device: " + device.getName() + "@" + rssi);
    if(Device_Name.equals(device.getName())){
            mDevices.put(device.hashCode(), device);
            invalidateOptionsMenu();
        }
}

Upvotes: 2

Views: 1987

Answers (2)

Samra
Samra

Reputation: 2015

Anroid 4.4 is not compatible, i used android 5.1 device and it worked like a charm!

Upvotes: 0

Nishant Thapliyal
Nishant Thapliyal

Reputation: 1670

  • Use this code as it will provide you insight of all the data available in your BLE device (will display the data in logs).
  • UUIDs are basically provided by the manufacturer of the device and you can't set it on your own.
  • For Instance : I was using BLE Beacons and its manufacturer provided me the API which helped me to fetch its UUID.
  • Sometimes, a special ID is available on the device itself(factory id in my case) which can help you to retrieve your UUID and that too from the manufacturer's website or API.

    BluetoothAdapter bluetoothAdapter;
    BluetoothLeScanner bluetoothLeScanner;
    
    
    
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
    
    
    
        bluetoothLeScanner.startScan(new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                super.onScanResult(callbackType, result);
    
                String s = "\nRssi : "+result.getRssi()+"" +
                        "\nName (Get Device) : "+result.getDevice().getName()+"" +
                        "\nBytes"+result.getScanRecord().getBytes()+"" +
                        "\nGet Device : " + result.getDevice()+"" +
                        "\nAddress : "+result.getDevice().getAddress()+"" +
                        "\nService UUIds : "+result.getScanRecord().getServiceUuids().get(0)+"" +       //Unique
                        "\nName (Scan Record) : "+result.getScanRecord().getDeviceName()+"" +
                        "\nUuids device : "+result.getDevice().getUuids()+"" +
                        "\nDescribe contents : "+result.describeContents();
    
                //This will show you all the data in logs.
                Log.e("All Data",s);
    
    
    
            }
    
            @Override
            public void onBatchScanResults(List<ScanResult> results) {
                super.onBatchScanResults(results);
            }
    
            @Override
            public void onScanFailed(int errorCode) {
                super.onScanFailed(errorCode);
            }
        });
    
    }
    

Upvotes: 1

Related Questions