问题描述
我开发必须与一个BLE设备连接,在我的code我想用新的扫描和ScanCallback从API 21(Android版5)实现BLE的应用程序,但我要保持兼容性搭载Android 4.3及以上。
所以我写了code,例如,以这种方式:
如果(Build.VERSION.SDK_INT> = 21){
mLEScanner.startScan(过滤器,设置,mScanCallback);
}其他{
btAdapter.startLeScan(leScanCallback);
}
和我已经定义了2回调,一个是API 21及以上的,一个是API 18〜20:
// 21 API
私人ScanCallback mScanCallback =新ScanCallback(){
@覆盖
公共无效onScanResult(INT callbackType,ScanResult结果){
BluetoothDevice类btDevice = result.getDevice();
connectToDevice(btDevice);
}
公共无效connectToDevice(BluetoothDevice类设备){
如果(mGatt == NULL){
mGatt = device.connectGatt(背景下,假的,btleGattCallback);
如果(Build.VERSION.SDK_INT< 21){
btAdapter.stopLeScan(leScanCallback);
}其他{
mLEScanner.stopScan(mScanCallback);
}
}
}
};
// API 18至20
私人BluetoothAdapter.LeScanCallback leScanCallback =新BluetoothAdapter.LeScanCallback(){
@覆盖
公共无效onLeScan(最终BluetoothDevice类设备,最终诠释RSSI,最后一个字节[] scanRecord){ btAdapter.stopLeScan(leScanCallback);
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
mBluetoothGatt = device.connectGatt(背景下,假的,btleGattCallback);
}
});
}
};
我还添加注释
@TargetApi(21)
但是当我启动应用程序在Android 4.x的崩溃立即报告,类ScanCallback不能发现错误(一个打算用来仅与Android 5及以上)。
我该如何解决呢?
非常感谢你。
丹尼尔。
- 创建
AbstractBluetoothLe
类和IBleScanCallback
接口。IBleScanCallback
接口是一个标记接口。在其他的说法,没有方法的接口。您还可以添加方法,如果你需要的接口。这些方法将为所有类型scanCallbacks即getListOfFoundBleDevices(),clearListOfFoundBleDevices的()等做相同的功能。 - 创建
BluetootLeLollipop
和BluetoothLeJellyBean
这扩展类AbstractBluetoothLe
类。还创建BluetootLeMarshmallow
延伸BluetootLeLollipop
类类。AbstractBluetoothLe
类保护了现场mIBleScanCallback
这是一个IBleScanCallback
对象。 - 创建
BleScanCallbackBase
类,它实现IBleScanCallback
。 - 创建
LollipopScanCallback
延伸ScanCallback
类和工具类IBleScanCallback
接口。 。这一类有一个受保护字段scanCallback
将被实例化为BleScanCallbackBase
对象。还创建MarshmallowScanCallback
延伸类LollipopScanCallback
类。 - 创建
JellyBeanScanCallback
延伸类BleScanCallbackBase
和农具BluetoothAdapter.LeScanCallback
- 在
BleScanCallbackBase
覆盖的方法:onScanCallback(...)
- 在
LollipoScanCallback
覆盖onScanResult(INT callbackType,ScanResult结果)
这个方法中调用该方法onScanCallback(...) scanCallback
对象>。 - 在
JellyBeanScanCallback
覆盖onLeScan(最终BluetoothDevice类设备,INT RSSI,字节[] scanRecord)
这种方法中叫onScanCallback(...)
- 最后,做任何需要在设备中
onScanCallback(...)
的方法BleScanCallbackBase 类。
总之,了解了组成继承 - 我知道这是不是一个回答你的问题,但是这是你到底想达到什么样的一种巧妙的方法。下面是类图:
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 to maintain the compatibility with Android 4.3 and above.
So I wrote the code, for example, in this way:
if (Build.VERSION.SDK_INT >= 21) {
mLEScanner.startScan(filters, settings, mScanCallback);
} else {
btAdapter.startLeScan(leScanCallback);
}
And I have defined the 2 callbacks, one for API 21 and above and one for API 18 to 20:
//API 21
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice btDevice = result.getDevice();
connectToDevice(btDevice);
}
public void connectToDevice(BluetoothDevice device) {
if (mGatt == null) {
mGatt = device.connectGatt(context, false, btleGattCallback);
if (Build.VERSION.SDK_INT < 21) {
btAdapter.stopLeScan(leScanCallback);
} else {
mLEScanner.stopScan(mScanCallback);
}
}
}
};
//API 18 to 20
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
btAdapter.stopLeScan(leScanCallback);
runOnUiThread(new Runnable() {
@Override
public void run() {
mBluetoothGatt = device.connectGatt(context, false, btleGattCallback);
}
});
}
};
I also added the annotation
@TargetApi(21)
but when I launch the App on Android 4.x it crashes immediately reporting the error that the class ScanCallback cannot be found (the one intended to be used only with Android 5 and above).
How can I solve this?
Thank you very much.Daniele.
解决方案
Create
AbstractBluetoothLe
class andIBleScanCallback
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.Create
BluetootLeLollipop
, andBluetoothLeJellyBean
classes which extendAbstractBluetoothLe
class. Create alsoBluetootLeMarshmallow
class which extendsBluetootLeLollipop
class.AbstractBluetoothLe
class has protected fieldmIBleScanCallback
which is anIBleScanCallback
object.Create
BleScanCallbackBase
class which implementsIBleScanCallback
.Create
LollipopScanCallback
class which extendsScanCallback
class and implementsIBleScanCallback
interface. .This class has a protected fieldscanCallback
which will be instantiated asBleScanCallbackBase
object. Create alsoMarshmallowScanCallback
class which extendsLollipopScanCallback
class.Create
JellyBeanScanCallback
class which extendsBleScanCallbackBase
and implementsBluetoothAdapter.LeScanCallback
In
BleScanCallbackBase
override the method:onScanCallback(...)
In
LollipoScanCallback
overrideonScanResult(int callbackType, ScanResult result)
and inside this method call the methodonScanCallback(...)
of thescanCallback
object.In
JellyBeanScanCallback
overrideonLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord)
and inside this method callonScanCallback(...)
Finally, do whatever needs to be done when a device is found in
onScanCallback(...)
method ofBleScanCallbackBase
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:
这篇关于Android的蓝牙低功耗code。与API&GT兼容= 21和API&LT; 21的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!