1. 快速接入

1.1 支持环境

环境

兼容范围

备注

软件系统

目标版本: Android API 30
最低版本: Android API 21

支持BLE功能

开发工具

Android Studio

建议使用最新版本

1.2 准备工作

1.2.1 依赖库导入

Important

XXX 为版本号,请以最新发布版本为准

  1. jl_bluetooth_connect_Vxxx.aar : 蓝牙连接相关

  2. jldecryption_vxxx.aar : 加密相关

1.2.2 导入库

//1.将1.2.1的aar文件放入工程目录中的对应moudle的lib文件夹下
//2.在moudlu的build.gradle中添加
implementation fileTree(include: ['*.aar'], dir: 'libs')

//库内使用数据库, 所以需要依赖Room持久性库
implementation "androidx.room:room-runtime:2.3.0"
annotationProcessor "androidx.room:room-compiler:2.3.0"

1.2.3 必要权限

 //使用蓝牙权限
    <uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
     //定位权限,官方要求使用蓝牙或网络开发,需要位置信息
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

1.3 SDK配置

1.3.1 配置SDK参数

 //构建默认配置参数
 BluetoothOption bluetoothOption = BluetoothOption.createDefaultOption();
 if (HealthConstant.DEFAULT_CONNECT_WAY == BluetoothConstant.PROTOCOL_TYPE_SPP) { //SPP方式
     bluetoothOption.setPriority(BluetoothConstant.PROTOCOL_TYPE_SPP);
     bluetoothOption.setScanFilterData("");  //过滤设备条件(设备名前缀)
 } else { //BLE方式
     bluetoothOption.setPriority(BluetoothConstant.PROTOCOL_TYPE_BLE);
     bluetoothOption.setNeedChangeBleMtu(true)  //需要调整MTU
             .setMtu(BluetoothConstant.BLE_MTU_MAX); //MTU预设值
     bluetoothOption.setBleScanStrategy(BluetoothConstant.HASH_FILTER); //扫描设备策略
 }
 bluetoothOption.setUseMultiDevice(false); //是否使用多设备管理
 //更多配置项,请参考BluetoothOption
 //初始化BluetoothManager对象
 mBluetoothOp = new BluetoothManager(context, bluetoothOption);

//获取当前配置
BluetoothOption oldOption = mBluetoothOp.getBluetoothOption();
//该方式可以修改BLE的通讯UUID
oldOption.setBleUUID(serviceUUID, writeCharacteristicUUID, notificationCharacteristicUUID);
//该方式可以修改SPP的通讯UUID
oldOption.setSppUUID(sppUUID);
//可以修改配置, 但是不推荐
mBluetoothOp.setBluetoothOption(oldOption);

蓝牙配置参数

public class BluetoothOption {

    /*=======================================================================*
     * 通用配置
     *=======================================================================*/
   /**
    * 指定通讯方式
    * <p>
    * 可选以下值:
    * {@link BluetoothConstant#PROTOCOL_TYPE_BLE} -- BLE协议
    * {@link BluetoothConstant#PROTOCOL_TYPE_SPP} -- SPP协议
    * </p>
    */
    private int priority = BluetoothConstant.PROTOCOL_TYPE_BLE;
   /**
    * 是否允许自动重连
    */
    private boolean reconnect;
   /**
    * 命令超时时间
    */
    private int timeoutMs = BluetoothConstant.DEFAULT_SEND_CMD_TIMEOUT;
   /**
    * 是否进入低功耗模式
    * <p>
    * 说明: 1. app端主动设置是否进入低功耗模式
    * 2. 如果为低功耗模式,则不连接经典蓝牙
    * </p>
    */
    private boolean enterLowPowerMode = false;
   /**
    * 是否使用多设备管理
    */
    private boolean isUseMultiDevice = true;

    /**
     * 是否使用设备认证
     */
    private boolean isUseDeviceAuth = true;

    /*=======================================================================*
     * BLE相关配置
     *=======================================================================*/
    /**
     * BLE过滤规则的过滤标识
     */
    private String scanFilterData;  // 搜索过滤数据

    /**
     * BLE扫描策略
     * <p>
     * 说明: 决定设备过滤规则
     * 可选参数:{@link BluetoothConstant#NONE_FILTER}  --- 不使用过滤规则
     * {@link BluetoothConstant#ALL_FILTER} --- 使用全部过滤规则
     * {@link BluetoothConstant#FLAG_FILTER} --- 仅使用标识过滤规则
     * {@link BluetoothConstant#HASH_FILTER} --- 仅使用hash加密过滤规则
     * </p>
     */
    private int bleScanStrategy = BluetoothConstant.ALL_FILTER; //BLE过滤规则

    /**
     * BLE扫描模式
     * <p>
     * 说明: 1.Android 5.1+才能使用
     * 可选参数: {@link android.bluetooth.le.ScanSettings#SCAN_MODE_LOW_POWER} --- 低功耗模式(默认)
     * {@link android.bluetooth.le.ScanSettings#SCAN_MODE_BALANCED} --- 平衡模式
     * {@link android.bluetooth.le.ScanSettings#SCAN_MODE_LOW_LATENCY} --- 低延迟模式(高功耗,仅前台有效)
     * </p>
     */
    private int bleScanMode = 0;

    /**
     * BLE传输MTU
     * <p>
     * 说明:Android为了兼容4.0的手机,默认的MTU是23;可用数据长度:20;
     * 随着手机系统的提升,现在的手机的MTU最大为512。建议提升MTU到512。
     * </p>
     */
    private int mtu = BluetoothConstant.BLE_MTU_MIN;  //调节蓝牙MTU

    /**
     * BLE 通讯UUID集合
     * <p>
     * {@link BluetoothConstant#KEY_BLE_SERVICE_UUID}  服务UUID
     * {@link BluetoothConstant#KEY_BLE_WRITE_CHARACTERISTIC_UUID} 写特征UUID
     * {@link BluetoothConstant#KEY_BLE_NOTIFICATION_CHARACTERISTIC_UUID} 通知特征UUID
     * </p>
     */
    private final Map<String, UUID> bleUUIDMap = new HashMap<>();

    /**
     * 是否使用BLE配对方式
     */
    private boolean isUseBleBondWay = false;
    /**
     * 是否自动连接BLE
     */
    private boolean isAutoConnectBle = false;
    /**
     * 是否需要改变BLE的MTU
     */
    private boolean isNeedChangeBleMtu = false;

    /*=======================================================================*
     * SPP相关配置
     *=======================================================================*/
    /**
     * SPP连接的UUID
     * <p>
     * 默认是标准的SPP UUID
     * </p>
     */
    private UUID sppUUID = BluetoothConstant.UUID_SPP;
    ...
}

1.3.2 接入流程

../_images/import_sdk_flow_zh.png