php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 552|回复: 0

Android BLE 蓝牙开发——扫码枪基于BLESSED

[复制链接]

3

主题

16

帖子

279

积分

VIP会员

Rank: 7Rank: 7Rank: 7

UID
2
威望
0
积分
263
贡献
0
注册时间
2021-4-15
最后登录
2022-3-28
在线时间
4 小时
发表于 2022-3-28 18:18:43 | 显示全部楼层 |阅读模式
一、蓝牙模式HID与BLE

  当扫码枪与手机连接时,通常采用的是蓝牙HID(Human Interface Device)模式。本质上是一个把扫码枪作为一个硬件键盘,按照键盘协议把扫码后的结果逐个输入到对应的控件上。

  优点:无需开发集成,配对就可以立即作为键盘输入使用。可以使用输入框等组件直接接收扫码结果。

  缺点:对非数字支持不佳,与输入法相关,在某些时候会触发英文联想-_-||,与虚拟键盘会发生冲突,连接扫码枪时需要切换键盘输入法输入。

  而我们要扫描的标签,不仅有英文,特殊符号,还有中文,因此以HID模式接入的蓝牙扫码枪,最终是不能满足要求的。于是重新选型了支持BLE模式的扫码枪。

  BLE模式扫码枪

  优点:兼容性好,遵循蓝牙协议,与键盘输入法无关。更底层,直接返回原始二进制数据流,方便判定编码以及进行字符编码转换。

  缺点:需要进行原生开发, 处理设备扫描,连接,数据读写等操作。

  二、BLE协议白话

  好在有github,其中的 BLESSED for Android - BLE made easy 项目,就是一个致力于简化android上BLE开发工作的开源库。但在撸代码前还是要简单理解下BLE协议的主要概念。

  较为重要的两个东西是Service(服务) 与Characteristic(特性,译为功能可能更好理解),简而言之,一个设备可以提供多个服务,每个服务可以提供多个特性功能,每个服务及特性对应一个UUID。

  与设备的通信通过功能进行,每个功能通过Properties(属性)表明该特性支持读,写或者通知。

  为了便于理解BLE协议,推荐下载一个叫做“BLE调试助手”的APP。下面是APP的截图。
image.png
  截图演示了如何从一个支持BLE协议的设备中读取电量,不需要提前配对,打开APP扫描到对应设备后,点击Connect, 随后列出的就是一堆上面说的Service(服务),能够显示服务名称的如“Battery Service”,是根据UUID的约定取得的。

  如电池服务为0x180F。点开服务后是Characteristic,其中的Battery Level(也是UUID约定 0x2A19)的Properties为 READ NOTIFY,表明该特性支持读取和通知。

  点击那个下箭头,点击读取,显示出返回数据为0x5D(十进制估计九十多:-0)。

  电池服务是一个在协议中约定的标准服务,但扫码枪的似乎不是,我们还需要找到扫码后,是通过哪个Service的哪个Characteristic进行通知的,通过这个工具APP也不难找,注意,要打开那个接受通知数据。

  扫码后会有数据显示,找到之后就可以开始编码了。

  三、第三方库  BLESSED for Android 的使用

  https://github.com/weliem/blessed-android

  下面进入具体的撸代码环节

  安装 gradle file加入
[mw_shl_code=applescript,true]allprojects {
        repositories {
                ...
                maven { url 'https://jitpack.io' }
        }
}

dependencies {
        implementation 'com.github.weliem:blessed-android:2.0.6'
}[/mw_shl_code]
扫描设备
BluetoothCentralManager central = new BluetoothCentralManager(AppContext.baseAppContext,
                                                              bluetoothCentralManagerCallback,
                                                              new Handler(Looper.getMainLooper()));
central.scanForPeripherals();
bluetoothCentralManagerCallback是扫描回调方法,重要的有下面三个
[mw_shl_code=applescript,true]//发现了一个设备@Override
public void onDiscoveredPeripheral(BluetoothPeripheral peripheral, ScanResult scanResult)
//连接设备@Override
public void onConnectedPeripheral(BluetoothPeripheral peripheral)
//设备断开@Overridepublic void onDisconnectedPeripheral(BluetoothPeripheral peripheral, HciStatus status)[/mw_shl_code]
  发现设备后onDiscoveredPeripheral,连接设备,停止扫描 。bluetoothPeripheralCallback 为设备回调,用于接受通知
central.autoConnectPeripheral(peripheral, bluetoothPeripheralCallback);
central.stopScan();
onConnectedPeripheral连接后查询提供的服务及特性
[mw_shl_code=applescript,true]Log.i("BLE","onConnectedPeripheral");
List<BluetoothGattService> serviceList =  peripheral.getServices();
for (int i = 0; i < serviceList.size(); i++) {
    Log.i("BLE", "Service:  " + serviceList.get(i).getUuid());
    if(serviceList.get(i).getUuid().toString().equals("6e400001-b5a3-f393-e0a9-e50e24dcca9e")){
        List<BluetoothGattCharacteristic> list=  serviceList.get(i).getCharacteristics();
        for (int j = 0; j < list.size(); j++) {
            Log.i("BLE", "Characteristic:  " + list.get(j).getUuid());
        }
    }
}[/mw_shl_code]
 onConnectedPeripheral后,对特性开启通知,接受扫码结果,服务的和特性的UUID,需要对应填写,扫码结果是以通知信息返回的。
[mw_shl_code=applescript,true]BluetoothGattCharacteristic currentTimeCharacteristic = peripheral.getCharacteristic(SERVICE_UUID, CURRENT_TIME_CHARACTERISTIC_UUID);
if (currentTimeCharacteristic != null) {   //开启通知
    peripheral.setNotify(currentTimeCharacteristic, true);
}
//与设备通信需要创建绑定
boolean bret=  peripheral.createBond();[/mw_shl_code]
 断开重连可以在onDisconnectedPeripheral中处理

  接收扫码结果 在设备回调类 bluetoothPeripheralCallback中的下列方法处理。其中value为扫到的二维码值
@Override
public void onCharacteristicUpdate(@NonNull BluetoothPeripheral peripheral, @NonNull byte[] value, @NonNull BluetoothGattCharacteristic characteristic, @NonNull GattStatus status)
另:

  中文编码通常为GBK或者UTF8,需要猜测判断,最后附了个工具函数
  因为BLE默认数据包较短,对大量数据,会拆分多次发送,onCharacteristicUpdate会被调用多次,需要将value拼接后,进行字符编码判断处理。  
[mw_shl_code=applescript,true]public static Boolean isUtf8(byte[] buffer) {
        boolean isUtf8 = true;
        int end = buffer.length;
        for (int i = 0; i < end; i++) {
            byte temp = buffer;
            if ((temp & 0x80) == 0) {// 0xxxxxxx
                continue;
            } else if ((temp & 0xC0) == 0xC0 && (temp & 0x20) == 0) {// 110xxxxx 10xxxxxx
                if (i + 1 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0) {
                    i = i + 1;
                    continue;
                }
            } else if ((temp & 0xE0) == 0xE0 && (temp & 0x10) == 0) {// 1110xxxx 10xxxxxx 10xxxxxx
                if (i + 2 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0
                        && (buffer[i + 2] & 0x80) == 0x80 && (buffer[i + 2] & 0x40) == 0) {
                    i = i + 2;
                    continue;
                }
            } else if ((temp & 0xF0) == 0xF0 && (temp & 0x08) == 0) {// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
                if (i + 3 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0
                        && (buffer[i + 2] & 0x80) == 0x80 && (buffer[i + 2] & 0x40) == 0
                        && (buffer[i + 3] & 0x80) == 0x80 && (buffer[i + 3] & 0x40) == 0) {
                    i = i + 3;
                    continue;
                }
            }
            isUtf8 = false;
            break;
        }
        return isUtf8;
    }

isUtf8[/mw_shl_code]





上一篇:怎样用读写锁快速实现一个缓存?
下一篇:【Spring AOP】暴力打通两个切面之间的通信
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-4-29 19:07 , Processed in 0.190277 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表