Reputation: 2015
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
Reputation: 2015
Anroid 4.4 is not compatible, i used android 5.1 device and it worked like a charm!
Upvotes: 0
Reputation: 1670
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