简体   繁体   中英

How to check null Bluetooth Low Energy device in android

I have project relate to Bluetooth Low Energy. I scan all BLE device near and display into screen by a lot different icon. I'm ok that. But I want to check if scan not BLE device, I will show a message "not BLE device is found". I don't have idea about check NULL BLE device. Perhaps you know!?

Here my some code:

    private BluetoothAdapter mBluetoothAdapter;
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    mBluetoothAdapter.startLeScan(mLeScanCallback);

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice arg0, int arg1, byte[] arg2) {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //TODO add device to screen

            });
        }
    };

https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#find

Per the Android documentation, the onus is on the developers to stop the Le scan after a certain time interval.

In that time interval when scan is active, you can keep track of how many times the onLeScan callback was triggered. If it wasn't triggered even once, you can then display any error message that you want to, after the Le Scan is stopped.

Plain Simple, follow below steps.

1) Run BLE Scan for some interval. You can use postDelayed handler to call stop LE scan after certain time suppose 10 seconds.

2) You must be adding found device in a list or somewhere to show it on UI. you said you are showing different icons for different devices.

3) To check if your process dint find any device, you can check on the icon set or the device list size. if it zero then you dint find anything.

Or you can use some boolean flag which you can enable(true) in onLeScan method, so you will know this has been called for at-least one time or not.

Happy Coding.

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