简体   繁体   中英

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();
        }
}
  • 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); } }); } 

Anroid 4.4不兼容,我用的是Android 5.1设备,它就像一个魅力!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM