Android的蓝牙低功耗code。与API&GT兼容= 21和API< 21 [英] Android Bluetooth Low Energy code compatible with API>=21 AND API<21

查看:926
本文介绍了Android的蓝牙低功耗code。与API&GT兼容= 21和API< 21的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发必须与一个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及以上)。

我该如何解决呢?

非常感谢你。
丹尼尔。


解决方案

  1. 创建 AbstractBluetoothLe 类和 IBleScanCallback 接口。 IBleScanCallback 接口是一个标记接口。在其他的说法,没有方法的接口。您还可以添加方法,如果你需要的接口。这些方法将为所有类型scanCallbacks即getListOfFoundBleDevices(),clearListOfFoundBleDevices的()等做相同的功能。

  2. 创建 BluetootLeLollipop BluetoothLeJellyBean 这扩展类 AbstractBluetoothLe 类。还创建 BluetootLeMarshmallow 延伸 BluetootLeLollipop 类类。 AbstractBluetoothLe 类保护了现场 mIBleScanCallback 这是一个 IBleScanCallback 对象。

  3. 创建 BleScanCallbackBase 类,它实现 IBleScanCallback

  4. 创建 LollipopScanCallback 延伸 ScanCallback 类和工具类 IBleScanCallback 接口。 。这一类有一个受保护字段 scanCallback 将被实例化为 BleScanCallbackBase 对象。还创建 MarshmallowScanCallback 延伸类 LollipopScanCallback 类。

  5. 创建 JellyBeanScanCallback 延伸类 BleScanCallbackBase 和农具 BluetoothAdapter.LeScanCallback

  6. BleScanCallbackBase 覆盖的方法: onScanCallback(...)

  7. LollipoScanCallback 覆盖 onScanResult(INT callbackType,ScanResult结果)这个方法中调用该方法 onScanCallback(...) scanCallback 对象>。

  8. JellyBeanScanCallback 覆盖 onLeScan(最终BluetoothDevice类设备,INT RSSI,字节[] scanRecord)这种方法中叫 onScanCallback(...)

  9. 最后,做任何需要在设备中 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.

解决方案

  1. Create 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.
  2. Create BluetootLeLollipop, and BluetoothLeJellyBean classes which extend AbstractBluetoothLe class. Create also BluetootLeMarshmallow class which extends BluetootLeLollipopclass. AbstractBluetoothLe class has protected field mIBleScanCallback which is an IBleScanCallback object.
  3. Create BleScanCallbackBase class which implements IBleScanCallback.
  4. Create 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.
  5. Create JellyBeanScanCallback class which extends BleScanCallbackBase and implements BluetoothAdapter.LeScanCallback
  6. In BleScanCallbackBase override the method: onScanCallback(...)
  7. In LollipoScanCallback override onScanResult(int callbackType, ScanResult result) and inside this method call the method onScanCallback(...) of the scanCallback object.
  8. In JellyBeanScanCallback override onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) and inside this method call onScanCallback(...)
  9. Finally, do whatever needs to be done when a device is found in 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:

这篇关于Android的蓝牙低功耗code。与API&GT兼容= 21和API&LT; 21的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆