Java Android:支持蓝牙应用程序的单个APK上的多个API级别

Java Android:支持蓝牙应用程序的单个APK上的多个API级别,java,android,bluetooth,Java,Android,Bluetooth,我已经开发了一个应用程序,在Jelly Bean(Android 4.3)上使用蓝牙功能。我注意到棒棒糖的蓝牙类有一套不同的方法。当我在棒棒糖上运行我的应用程序时,它失败了 我了解到,我可以在运行时添加代码以支持不同的API级别,如: private void startScanBluetooth(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Method supported i

我已经开发了一个应用程序,在Jelly Bean(Android 4.3)上使用蓝牙功能。我注意到棒棒糖的蓝牙类有一套不同的方法。当我在棒棒糖上运行我的应用程序时,它失败了

我了解到,我可以在运行时添加代码以支持不同的API级别,如:

private void startScanBluetooth(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Method supported in Lollipop
        mLeScanner.stopScan(mScanCallback);
    }
    else {
        // Method for older API level
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
}
我的代码有一个BluetoothAdapter.LeScanCallback,它在我的活动类中,但不在任何方法中

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
    setContentView(R.layout.device_list);

    final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    //....
}

private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //String targetDevice = MainActivity.targetDevice.getText().toString();
                                //if(targetDevice==null || targetDevice.length()==0 || targetDevice.equals(device.getName()))
                                addDevice(device,rssi);
                            }
                        });

                    }
                });
            }
        };
由于此回调已被弃用,我需要一个新的回调方法:ScanCallBack来支持棒棒糖,因此我添加了另一个类似这样的回调:

    mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Log.i("callbackType", String.valueOf(callbackType));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Log.i("result", result.toString());
                    BluetoothDevice btDevice = result.getDevice();
                    connectToDevice(btDevice);
                }
            }
        }
     };
它现在可以在棒棒糖中运行,但是当我在4.4.2上运行它时,一旦它运行类DeviceListActivity(以上所有内容都在该类中),应用程序就会意外存在。失败时的日志是:

09-02 17:42:46.469 15329-15329/com.conerstonee2.blecaller D/OLED:立即启动DeviceListActivity
09-02 17:42:46.499 15329-15329/com.conerstonee2.blecaller D/OLED:onPause
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:无法解析Lcom/conerstonee2/blecaller/DeviceListActivity$3的超类;(63)
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:Lcom/conerstonee2/blecaller/DeviceListActivity$3类链接失败
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller E/dalvikvm:找不到从方法com.conerstonee2.blecaller.DeviceListActivity引用的类“com.conerstonee2.blecaller.DeviceListActivity$3”。
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:VFY:无法解析Lcom/conerstonee2/blecaller/DeviceListActivity中的新实例1960(Lcom/conerstonee2/blecaller/DeviceListActivity$3;);
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller D/dalvikvm:VFY:在0x0006处替换操作码0x22
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:VFY:找不到签名中引用的类(Landroid/bluetooth/le/ScanCallback;)
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller I/dalvikvm:找不到方法android.bluetooth.le.BluetoothLeScanner.startScan,从方法com.conerstonee2.blecaller.DeviceListActivity.Scanlevice引用
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:VFY:无法解析虚拟方法311:Landroid/bluetooth/le/BluetoothLeScanner;。startScan(Landroid/bluetooth/le/ScanCallback;)V
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller D/dalvikvm:VFY:在0x0024处替换操作码0x6e
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller I/dalvikvm:找不到方法android.bluetooth.le.BluetoothLeScanner.stopScan,从方法com.conerstonee2.blecaller.DeviceListActivity.Scanlevice引用
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:VFY:无法解析虚拟方法312:Landroid/bluetooth/le/BluetoothLeScanner;。stopScan(Landroid/bluetooth/le/ScanCallback;)V
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller D/dalvikvm:VFY:在0x0041处替换操作码0x6e
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller I/dalvikvm:找不到方法android.bluetooth.BluetoothAdapter.getBluetoothLeScanner,从方法com.conerstonee2.blecaller.DeviceListActivity.onCreate引用
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:VFY:无法解析虚拟方法283:Landroid/bluetooth/BluetoothAdapter;。getBluetoothLeScanner()Landroid/bluetooth/le/BluetoothLeScanner;
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller D/dalvikvm:VFY:在0x0071处替换操作码0x6e
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller I/dalvikvm:找不到方法android.bluetooth.le.BluetoothLeScanner.stopScan,从方法com.conerstonee2.blecaller.DeviceListActivity.onDestroy引用
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:VFY:无法解析虚拟方法312:Landroid/bluetooth/le/BluetoothLeScanner;。stopScan(Landroid/bluetooth/le/ScanCallback;)V
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller D/dalvikvm:VFY:在0x0014处替换操作码0x6e
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller I/dalvikvm:找不到方法android.bluetooth.le.BluetoothLeScanner.stopScan,从方法com.conerstonee2.blecaller.DeviceListActivity.onStop引用
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:VFY:无法解析虚拟方法312:Landroid/bluetooth/le/BluetoothLeScanner;。stopScan(Landroid/bluetooth/le/ScanCallback;)V
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller D/dalvikvm:VFY:在0x0014处替换操作码0x6e
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:无法解析Lcom/conerstonee2/blecaller/DeviceListActivity$3的超类;(63)
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:Lcom/conerstonee2/blecaller/DeviceListActivity$3类链接失败
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller D/dalvikvm:DexOpt:无法在Lcom/conerstonee2/blecaller/DeviceListActivity;中的0x08选择直接呼叫0x3ea0;。
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller D/AndroidRuntime:关闭虚拟机
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller W/dalvikvm:threadid=1:线程以未捕获异常退出(组=0x41f8bda0)
09-02 17:42:46.519 15329-15329/com.conerstonee2.blecaller E/AndroidRuntime:致命异常:main
进程:com.conerstonee2.blecaller,PID:15329
java.lang.NoClassDefFoundError:com.conerstonee2.blecaller.DeviceListActivity$3
在com.conerstonee2.blecaller.DeviceListActivity.(DeviceListActivity.java:181)
09-02 17:42:46.469 15329-15329/com.conerstoneee2.blecaller D/OLED: Start DeviceListActivity now
09-02 17:42:46.499 15329-15329/com.conerstoneee2.blecaller D/OLED: onPause
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: Unable to resolve superclass of Lcom/conerstoneee2/blecaller/DeviceListActivity$3; (63)
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: Link of class 'Lcom/conerstoneee2/blecaller/DeviceListActivity$3;' failed
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller E/dalvikvm: Could not find class 'com.conerstoneee2.blecaller.DeviceListActivity$3', referenced from method com.conerstoneee2.blecaller.DeviceListActivity.<init>
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: VFY: unable to resolve new-instance 1960 (Lcom/conerstoneee2/blecaller/DeviceListActivity$3;) in Lcom/conerstoneee2/blecaller/DeviceListActivity;
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller D/dalvikvm: VFY: replacing opcode 0x22 at 0x0006
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/bluetooth/le/ScanCallback;)
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller I/dalvikvm: Could not find method android.bluetooth.le.BluetoothLeScanner.startScan, referenced from method com.conerstoneee2.blecaller.DeviceListActivity.scanLeDevice
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: VFY: unable to resolve virtual method 311: Landroid/bluetooth/le/BluetoothLeScanner;.startScan (Landroid/bluetooth/le/ScanCallback;)V
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller D/dalvikvm: VFY: replacing opcode 0x6e at 0x0024
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller I/dalvikvm: Could not find method android.bluetooth.le.BluetoothLeScanner.stopScan, referenced from method com.conerstoneee2.blecaller.DeviceListActivity.scanLeDevice
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: VFY: unable to resolve virtual method 312: Landroid/bluetooth/le/BluetoothLeScanner;.stopScan (Landroid/bluetooth/le/ScanCallback;)V
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller D/dalvikvm: VFY: replacing opcode 0x6e at 0x0041
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller I/dalvikvm: Could not find method android.bluetooth.BluetoothAdapter.getBluetoothLeScanner, referenced from method com.conerstoneee2.blecaller.DeviceListActivity.onCreate
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: VFY: unable to resolve virtual method 283: Landroid/bluetooth/BluetoothAdapter;.getBluetoothLeScanner ()Landroid/bluetooth/le/BluetoothLeScanner;
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller D/dalvikvm: VFY: replacing opcode 0x6e at 0x0071
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller I/dalvikvm: Could not find method android.bluetooth.le.BluetoothLeScanner.stopScan, referenced from method com.conerstoneee2.blecaller.DeviceListActivity.onDestroy
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: VFY: unable to resolve virtual method 312: Landroid/bluetooth/le/BluetoothLeScanner;.stopScan (Landroid/bluetooth/le/ScanCallback;)V
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller D/dalvikvm: VFY: replacing opcode 0x6e at 0x0014
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller I/dalvikvm: Could not find method android.bluetooth.le.BluetoothLeScanner.stopScan, referenced from method com.conerstoneee2.blecaller.DeviceListActivity.onStop
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: VFY: unable to resolve virtual method 312: Landroid/bluetooth/le/BluetoothLeScanner;.stopScan (Landroid/bluetooth/le/ScanCallback;)V
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller D/dalvikvm: VFY: replacing opcode 0x6e at 0x0014
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: Unable to resolve superclass of Lcom/conerstoneee2/blecaller/DeviceListActivity$3; (63)
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: Link of class 'Lcom/conerstoneee2/blecaller/DeviceListActivity$3;' failed
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller D/dalvikvm: DexOpt: unable to opt direct call 0x3ea0 at 0x08 in Lcom/conerstoneee2/blecaller/DeviceListActivity;.<init>
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller D/AndroidRuntime: Shutting down VM
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41f8bda0)
09-02 17:42:46.519 15329-15329/com.conerstoneee2.blecaller E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.conerstoneee2.blecaller, PID: 15329
                                                                             java.lang.NoClassDefFoundError: com.conerstoneee2.blecaller.DeviceListActivity$3
                                                                                 at com.conerstoneee2.blecaller.DeviceListActivity.<init>(DeviceListActivity.java:181)
                                                                                 at java.lang.Class.newInstanceImpl(Native Method)
                                                                                 at java.lang.Class.newInstance(Class.java:1208)
                                                                                 at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2289)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:173)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:136)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5579)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
09-02 17:42:49.959 15329-15329/com.conerstoneee2.blecaller I/Process: Sending signal. PID: 15329 SIG: 9
private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //String targetDevice = MainActivity.targetDevice.getText().toString();
                                //if(targetDevice==null || targetDevice.length()==0 || targetDevice.equals(device.getName()))
                                addDevice(device,rssi);
                            }
                        });

                    }
                });
            }
        };
mLeScanner.stopScan(mScanCallback);
 private **typeNameGoesHere** mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.i("callbackType", String.valueOf(callbackType));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Log.i("result", result.toString());
                BluetoothDevice btDevice = result.getDevice();
                connectToDevice(btDevice);
            }
        }
    };
private void startScanBluetooth(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Method supported in Lollipop
        mLeScanner.stopScan(mScanCallback);
    }
    else {
        // Method for older API level
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
}