Android连接/断开扫描枪时屏幕刷新异常解决分析

    Android连接/断开扫描枪时屏幕刷新异常解决分析



前言

  当你吃着火锅,唱着歌在Android终端上面连接扫码枪(包括蓝牙 USB等等)准备进行扫码工作时,突然屏幕一闪刷新了,造成临时加载的数据丢失了。本篇章就是为了解决这个问题的。



一.现象和整套解决方案


1.1 现场复盘

  插入和拔出扫码枪时演示demo效果如下:
在这里插入图片描述


1.2 分析原因

  扫描枪连接时是将其作为外部物理输入设备,即物理键盘,而安卓在改变屏幕方向、接入外部键盘时都是弹出隐藏键盘时都是手机状态改变,会导致重新执行onCreate( )方法,而造成部分数据丢失。


1.3 解决方案

  解决方案比较简单分如下两步走:

  • 在对应的AndroidManifest.xml配置如下内容,如下所示:
        <activity
            android:name="com.xxx.api.test.ApiActivity"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
            android:theme="@style/AppTheme" >
  • 然后在activity中重写onConfigurationChanged()方法,不做任何处理,就是重写就行了,如下所示:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        //其中keyboard表示键盘类型发生了变化,比如用户使用了外接键盘,其中取值为1表示显示系统键盘,为2表示隐藏系统键盘
        //keyboardHidden表示键盘的访问属性发生了变化,比如用户调出了键盘
        Configuration configuration = getResources().getConfiguration();
        Log.e("config", "newConfig  keyboard = " + newConfig.keyboard);
        Log.e("config", "newConfig  keyboardHidden = " + newConfig.keyboardHidden);
        Log.e("config", "configuration  keyboard = " + configuration.keyboard);
        Log.e("config", "configuration  keyboardHidden = " + configuration.keyboardHidden);
    }
       


结语

  好了,今天的博客到这里就结束了,各位江湖见。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是 Android 测试手机与小米手环 BLE 蓝牙扫描、连接断开的代码: 1. 首先,在 AndroidManifest.xml 中添加以下权限: ```xml <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ``` 2. 在代码中声明蓝牙适配器和扫描回调: ```java private BluetoothAdapter mBluetoothAdapter; private BluetoothLeScanner mBluetoothLeScanner; private ScanCallback mScanCallback; ``` 3. 初始化蓝牙适配器和扫描回调: ```java // 初始化蓝牙适配器 final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); // 初始化扫描回调 mScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); BluetoothDevice device = result.getDevice(); // 连接设备 connect(device); } @Override public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); // 扫描失败 } }; ``` 4. 开始扫描: ```java if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) { mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); mBluetoothLeScanner.startScan(mScanCallback); } ``` 5. 连接设备: ```java private BluetoothGatt mBluetoothGatt; private void connect(BluetoothDevice device) { if (mBluetoothGatt != null) { mBluetoothGatt.close(); } mBluetoothGatt = device.connectGatt(this, false, mGattCallback); } private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { // 连接成功 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 断开连接 } } }; ``` 6. 断开连接: ```java if (mBluetoothGatt != null) { mBluetoothGatt.disconnect(); mBluetoothGatt.close(); mBluetoothGatt = null; } ``` 以上是 Android 测试手机与小米手环 BLE 蓝牙扫描、连接断开的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值