omaraflak / Bluetooth-Library

Bluetooth client library for Android.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

function Refactor: startScanning()

root-ansh opened this issue · comments

I made a special test condition in my app, where the user might accidentally press The button to "start Discovering Nearby Devices" multiple times even when bluetooth is actually off.Although the app or the library isn't generating errors,but is also not showing any perticular changes to repeated calls in the logs.

however I made the test case a little more tricky by doing this : updated the button for "start scanning" , which will now alter its text between "scanning started" and "stop scanning" on repeated presses(WITHOUT checking for bluetooth being enable or not).
thus , the library failed(not generating errors but also not showing changes) on repeated presses since the startScanning() function was of void return-type, so their was no way to check if the scanning has started or not . it was still showing "scanning started" on repeated presses since the scan has not started on the first place!and am pretty sure that bluetoothAdapter.startDiscovery() is being uselessly called again and again by the library

you see, its not a 2 case flag, its a 3-case flag:
if( discovery is not started)=>
Boolean isStarted= CALL startDiscovery() which will return weather discovery started
successfully or failed;
return isStarted; //so that user can implement changes/ handle errors for startDiscovery() call

else(if discovery is started)=>
stopDiscovery()// I will feel nice if this function also returned a boolean too

again, its not a very high profile error, since startScanning() is almost always a background function invisible to user , so there is almost a null chance for user to create errors in this but as i said in my other issue raise, i see a simple and nice stock library in this code, so you might or might not wanna change it

 public boolean startScanning(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

        context.registerReceiver(scanReceiver, filter);
        Boolean hasDiscoveryStarted =bluetoothAdapter.startDiscovery();
        return hasDiscoveryStarted;
    }

Yes I also face the problem related to search a device a pair. @omaraflak