2

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();
        }
}
Samra
  • 1,815
  • 4
  • 35
  • 71

2 Answers2

1
  • 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);
            }
        });
    
    }
    
Nishant Thapliyal
  • 1,540
  • 17
  • 28
  • Thanks a bunch! seems like the right answer only that I am getting error: method lookup failed for selector "getBluetoothLeScanner" with signature "()Landroid/bluetooth/le/BluetoothLeScanner;" when i do this -> mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); – Samra Jul 07 '16 at 01:13
  • My java compiler version is 1.7 (if it matters) – Samra Jul 07 '16 at 01:19
  • You might be getting this error because **BluetoothLeScanner** was added in API 21 (Android 5.0). So, try to set `minSdkVersion 21`. – Nishant Thapliyal Jul 07 '16 at 04:42
  • No errors now but same issue, now also no device is detected. In my Android Manifest file i have permissions for bluetooth, bluetooth_admin, Coarse_location, Fine_Location. My GPS and bluetooth are both on in my device – Samra Jul 08 '16 at 03:39
0

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

Samra
  • 1,815
  • 4
  • 35
  • 71