Bluetooth scanning process

For the scanning process, following objects should be prepared in order:

BluetoothManager mBluetoothManager =         (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();

getSystemService() method brings back the Bluetooth Service Manager, if called with Context.BLUETOOTH_SERVICE. This manager can then be used to get the adapter which is the core of the Bluetooth operation. As mentioned in the code, mBluetoothAdapter can be acquired by calling getAdapter() on the BluetoothManager object. This bluetooth adapter will now be used to get the BluetoothLeScanner object:

mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

It is a good practice to start the Bluetooth scanning process for 10 to 15 seconds so that the phone don't consume continous power from the battery. This purpose will be achieved by Android Handler. Handler class allows sending runnable objects associated with the thread's MessageQueue. We wrote a method which will run for 10 seconds and will provide Bluetooth scanning process:

private void scanBluetoothDevices(boolean enable) {     if (mBluetoothLeScanner != null) {         if (enable) {             // Stops scanning after a pre-defined scan period.             mHandler.postDelayed(new Runnable() {                 @Override                 public void run() {                     mScanning = false;                     mBluetoothLeScanner.stopScan(mBluetoothScanCallBack);                 }             }, 10000);             mScanning = true;             mBluetoothLeScanner.startScan(mBluetoothScanCallBack);         } else {             mScanning = false;             mBluetoothLeScanner.stopScan(mBluetoothScanCallBack);         }     } 
}

scanBluetoothDevice() takes boolean as a decision to start or stop the scan. If enable is false, the scanner will stop scanning by calling mBluetoothLeScanner.stopScan() method. On the other hand, if enable is true, it will start the scanning by calling mBluetoothLeScanner.startScan() with a callback object. Let's forget the callback for a bit time and concentrate on the mHandler.postDelayed() method.

mHandler.postDelayed(Runnable, x) is a method executes the provided runnable after x amount of time. In our case, we are providing 10000 millisec as the processing time. The runnable will be executed after 10 seconds which will interupt the scanning process and stop the scan by calling mBluetoothLeScanner.stopScan().

mBluetoothScanCallback is an object of ScanCallback which provides developers methods for overriding. The most important methods of this class are: onScanResult and onScanFailed. When the scan yields any result the onScanResult is triggered and the developer can update the user interface. onScanFailed, on the other hand, is called if for some reason the scan fails. You can update the view in this case as well.

In our application, we are updating the list view adapter by giving the scanned Bluetooth device to the adapter and calling notifyDataSetChanged().

private ScanCallback mBluetoothScanCallBack = new ScanCallback() {     @Override     public void onScanResult(int callbackType, final ScanResult result) {         super.onScanResult(callbackType, result);         runOnUiThread(new Runnable() {             @Override             public void run() {                 mLeDeviceListAdapter.addDevice(result.getDevice());                 mLeDeviceListAdapter.notifyDataSetChanged();             }         });     }     @Override     public void onScanFailed(int errorCode) {         super.onScanFailed(errorCode);         Toast.makeText(MainActivity.this, "Scan Failed: Please try again...", Toast.LENGTH_LONG).show();     } 
};

result.getDevice() will return a BluetoothDevice object which can then be passed to the custom list adapter for UI update. In previously mentioned code, we are adding the device to the list adapter and calling notifyDataSetChanged(). This method is used to notify the observer that the underlying data is being changed and the view should update itself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.162