I\'m developing an app that have to connect with a BLE device, in my code I want to use the new Scan and ScanCallback for BLE implemented from API 21 (Android 5) but I have
AbstractBluetoothLe
class and IBleScanCallback
interface. IBleScanCallback
interface is a Marker Interface. In other saying, an interface with no methods. You can also add methods to interface if you need. These methods will do the same functionality for all type of scanCallbacks i.e. getListOfFoundBleDevices(), clearListOfFoundBleDevices() etc.BluetootLeLollipop
, and BluetoothLeJellyBean
classes which extend AbstractBluetoothLe
class. Create also BluetootLeMarshmallow
class which extends BluetootLeLollipop
class. AbstractBluetoothLe
class has protected field mIBleScanCallback
which is an IBleScanCallback
object. BleScanCallbackBase
class which implements IBleScanCallback
.LollipopScanCallback
class which extends ScanCallback
class and implements IBleScanCallback
interface. .This class has a protected field scanCallback
which will be instantiated as BleScanCallbackBase
object. Create also MarshmallowScanCallback
class which extends LollipopScanCallback
class.JellyBeanScanCallback
class which extends BleScanCallbackBase
and implements BluetoothAdapter.LeScanCallback
BleScanCallbackBase
override the method: onScanCallback(...)
LollipoScanCallback
override onScanResult(int callbackType, ScanResult result)
and inside this method call the method onScanCallback(...)
of the scanCallback
object.JellyBeanScanCallback
override onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord)
and inside this method call onScanCallback(...)
onScanCallback(...)
method of BleScanCallbackBase
class.In short, read about composition over inheritance- I know this is not an answer to your question but this is a neat way of what you want to achieve in the end. Here is the class diagram: