3. 接口说明
3.1 蓝牙操作接口
public interface IBluetoothOperation {
/**
* 注册蓝牙回调监听器
*
* @param bluetoothCallback 蓝牙回调监听器(蓝牙数据回调)
* @return 操作结果
*/
boolean registerBluetoothCallback(BluetoothEventCallback bluetoothCallback);
/**
* 注销蓝牙回调监听器
*
* @param bluetoothCallback 蓝牙回调监听器(蓝牙数据回调)
* @return 操作结果
*/
boolean unregisterBluetoothCallback(BluetoothEventCallback bluetoothCallback);
/**
* 已连接蓝牙设备对象
*
* <p>在BLE方式时,返回BLE设备对象。在SPP方式时,需要注意,会通过BLE连接转换成Spp连接,
* 所以会先返回BLE的对象,再返回Spp的对象(经典蓝牙的对象)</p>
*
* @return 返回通讯通道的对象, 若通讯通道为建立, 返回null。
*/
BluetoothDevice getConnectedDevice();
/**
* 设置已连接且正在使用的蓝牙设备
*
* @param device 蓝牙设备
*/
void setConnectedDevice(BluetoothDevice device);
/**
* 获取已连接的BluetoothGatt对象
*
* @return 已连接的BluetoothGatt对象
*/
BluetoothGatt getConnectedBluetoothGatt();
/**
* 通过指定BLE设备对应的BluetoothGatt对象
*
* @param device BLE设备
* @return 已连接的BluetoothGatt对象
*/
BluetoothGatt getDeviceGatt(BluetoothDevice device);
/**
* 获取已连接设备列表
*
* @return 已连接设备列表
*/
List<BluetoothDevice> getConnectedDeviceList();
/**
* 判断设备是否已连接
*
* @param device 蓝牙设备
* @return 结果
*/
boolean isConnectedDevice(BluetoothDevice device);
/**
* 设置蓝牙配置信息
*
* @param bluetoothOption 蓝牙配置信息 <p>若为null时,默认创建Spp通讯方式的</p>
*/
void setBluetoothOption(BluetoothOption bluetoothOption);
/**
* 获取蓝牙配置信息
*/
BluetoothOption getBluetoothOption();
/**
* 获取发现蓝牙设备列表
*
* <p>列表,只会返回要扫描的类型; scan的操作会清空缓存列表;</p>
*
* @return 发现蓝牙设备列表
*/
ArrayList<BluetoothDevice> getDiscoveredBluetoothDevices();
/**
* 是否正在扫描设备
*/
boolean isScanning();
/**
* 获取扫描设备的类型
*
* @return 扫描设备的类型
*/
int getScanType();
/*-----------------------------------br/edr op--------------------------*/
/**
* 开始蓝牙设备扫描
*
* <p>该方法会扫描所有蓝牙设备,SDK只回调经典蓝牙设备</p>
*
* @param timeout 超时值
* @return 操作结果
*/
boolean startDeviceScan(long timeout);
/**
* 开始蓝牙设备扫描
*
* <p>该方法会扫描所有蓝牙设备,SDK回调所有蓝牙设备</p>
*
* @param type 扫描设备类型
* <p>参考值:{@link com.jieli.bluetooth_connect.constant.BluetoothConstant#SCAN_TYPE_BLE} -- BLE蓝牙设备(包含双模设备)
* or {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#SCAN_TYPE_CLASSIC} -- 经典蓝牙设备
* or {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#SCAN_TYPE_ALL} -- 所有类型蓝牙设备</p>
* @param timeout 超时值
* @return 操作结果
*/
boolean startDeviceScan(int type, long timeout);
/**
* 停止蓝牙设备扫描
*
* @return 操作结果
*/
boolean stopDeviceScan();
/**
* 判断蓝牙设备是否已配对
*
* @param device 蓝牙设备
* @return 是否已配对
*/
boolean isPaired(BluetoothDevice device);
/**
* 获取已配对的蓝牙设备列表
*
* @return 已配对的蓝牙设备列表 (需要注意,若蓝牙模块未打开,也是返回空列表)
*/
List<BluetoothDevice> getPairedDevices();
/**
* 请求蓝牙设备进行配对过程
*
* <p>直接调用系统反射接口</p>
*
* @param device 蓝牙设备
* @return 操作结果
*/
boolean pair(BluetoothDevice device);
/**
* 尝试进行配对过程
*
* <p>队列式执行配对过程,避免过度操作导致蓝牙系统异常。推荐使用该方法进行配对</p>
*
* @param device 蓝牙设备
* @return 添加队列结果
*/
boolean tryToPair(BluetoothDevice device);
/**
* 请求蓝牙设备进行取消配对
*
* <p>直接调用系统反射接口</p>
*
* @param device 蓝牙设备
* @return 操作结果
*/
boolean unPair(BluetoothDevice device);
/**
* 尝试进行取消配对
*
* <p>队列式执行取消配对过程,避免过度操作导致蓝牙系统异常。推荐使用该方法进行配对</p>
*
* @param device 蓝牙设备
* @return 添加队列结果
*/
boolean tryToUnPair(BluetoothDevice device);
/**
* 蓝牙设备是否已建立hfp服务
*
* @param device 经典蓝牙设备
* @return 结果码 <p>有以下可能值: {@link BluetoothProfile#STATE_DISCONNECTED} 未连接<br>
* or {@link BluetoothProfile#STATE_CONNECTING} 连接中<br>
* or {@link BluetoothProfile#STATE_CONNECTED} 已连接</p>
*/
int isConnectedByHfp(BluetoothDevice device);
/**
* 蓝牙设备是否已建立a2dp服务
*
* @param device 经典蓝牙设备
* @return 结果码 <p>有以下可能值: {@link BluetoothProfile#STATE_DISCONNECTED} 未连接<br>
* or {@link BluetoothProfile#STATE_CONNECTING} 连接中<br>
* or {@link BluetoothProfile#STATE_CONNECTED} 已连接</p>
*/
int isConnectedByA2dp(BluetoothDevice device);
/**
* 蓝牙设备是否已建立经典蓝牙服务(a2dp、hfp)
*
* @param device 经典蓝牙设备
* @return 结果码 <p>有以下可能值: {@link BluetoothProfile#STATE_DISCONNECTED} 未连接<br>
* or {@link BluetoothProfile#STATE_CONNECTING} 连接中<br>
* or {@link BluetoothProfile#STATE_CONNECTED} 已连接</p>
*/
int isConnectedByProfile(BluetoothDevice device);
/**
* 判断经典蓝牙是否连接中
*
* @return 结果
*/
boolean isBrEdrConnecting();
/**
* 获取连接中的经典蓝牙设备
*
* @return 经典蓝牙设备
*/
BluetoothDevice getConnectingBrEdrDevice();
/**
* 连接经典蓝牙
*
* @param device 经典蓝牙设备
* @return 结果值 false为操作失败,true为操作成功,等待回调{@link BluetoothEventCallback}
*/
boolean connectByProfiles(BluetoothDevice device);
/**
* 断开经典蓝牙
*
* @param device 经典蓝牙设备
* @return 结果
*/
boolean disconnectByProfiles(BluetoothDevice device);
/**
* 设置指定的设备为音频输出设备
*
* @param device 指定的已连接设备
* @return 结果
*/
boolean setActivityBluetoothDevice(BluetoothDevice device);
/**
* 获取当前音频输出设备对象
*
* @return 前音频输出设备对象
*/
BluetoothDevice getActivityBluetoothDevice();
/**
* 获取已连接SPP列表
*
* @return 已连接SPP列表
*/
List<BluetoothDevice> getConnectedSppList();
/**
* 判断设备是否已连接SPP
*
* @param device 蓝牙设备
* @return 结果
*/
boolean isConnectedSppDevice(BluetoothDevice device);
/**
* 连接Spp通道
*
* <p>连接结果通过{@link BluetoothEventCallback} 回调。需要通过 {@link #registerBluetoothCallback(BluetoothEventCallback)} 注册回调。</p>
*
* @param device 经典蓝牙设备
* @return 结果码
*/
boolean connectSPPDevice(BluetoothDevice device);
/**
* 断开Spp通道
*
* <p>连接结果通过{@link BluetoothEventCallback} 回调。需要通过 {@link #registerBluetoothCallback(BluetoothEventCallback)} 注册回调。</p>
*
* @param device 经典蓝牙设备
* @return 结果
*/
boolean disconnectSPPDevice(BluetoothDevice device);
/**
* 通过Spp通道发送数据包
*
* @param device 经典蓝牙设备
* @param data 数据包
* @return 发送结果
*/
boolean writeDataToSppDevice(BluetoothDevice device, byte[] data);
/*----------------------------------ble op----------------------------*/
/**
* 开始BLE设备扫描
*
* @param timeout 超时值
* @return 操作结果
*/
boolean startBLEScan(long timeout);
/**
* 停止BLE设备扫描
*
* @return 操作结果
*/
boolean stopBLEScan();
/**
* 获取已连接BLE设备的MTU
*
* @param device 已连接BLE设备
* @return MTU
*/
int getBleMtu(BluetoothDevice device);
/**
* 请求调整BLE的MTU
*
* @param device 已连接BLE设备
* @param mtu MTU
* @return 结果
*/
boolean requestBleMtu(BluetoothDevice device, int mtu);
/**
* 判断是否为已连接的BLE设备
*
* @param device BLE设备
* @return 结果
*/
boolean isConnectedBLEDevice(BluetoothDevice device);
/**
* 连接BLE设备
*
* <p>连接结果通过{@link BluetoothEventCallback} 回调。需要通过 {@link #registerBluetoothCallback(BluetoothEventCallback)} 注册回调。</p>
*
* @param device BLE设备
*/
boolean connectBLEDevice(BluetoothDevice device);
/**
* 断开BLE设备
*
* <p>连接结果通过{@link BluetoothEventCallback} 回调。需要通过 {@link #registerBluetoothCallback(BluetoothEventCallback)} 注册回调。</p>
*
* @param device BLE设备
* @return 操作结果
*/
boolean disconnectBLEDevice(BluetoothDevice device);
/**
* 通过BLE写服务发送数据包
*
* @param device BLE设备
* @param serviceUUID 服务UUID
* @param characteristicUUID 属性UUID
* @param writeData 数据包
* @return 操作结果
*/
boolean writeDataToBLEDevice(BluetoothDevice device, UUID serviceUUID, UUID characteristicUUID, byte[] writeData);
/*---------------------------------- special op ----------------------------*/
/**
* 获取当前连接中设备对象
*
* @return 连接中设备对象
*/
BluetoothDevice getConnectingDevice();
/**
* 连接蓝牙设备
*
* <p>根据蓝牙设备类型进行连接,BLE设备进行BLE连接,经典蓝牙设备进行经典蓝牙连接</p>
* <p>连接结果通过{@link BluetoothEventCallback} 回调。需要通过 {@link #registerBluetoothCallback(BluetoothEventCallback)} 注册回调。</p>
*
* @param device 蓝牙设备
* @param connectWay 连接方式
* <p>参考值:
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#PROTOCOL_TYPE_BLE}
* or @{@link com.jieli.bluetooth_connect.constant.BluetoothConstant#PROTOCOL_TYPE_SPP}</p>
* @return 结果结果
*/
boolean connectBtDevice(BluetoothDevice device, int connectWay);
/**
* 断开蓝牙设备
*
* <p>连接结果通过{@link BluetoothEventCallback} 回调。需要通过 {@link #registerBluetoothCallback(BluetoothEventCallback)} 注册回调。</p>
*
* @param device 蓝牙设备
*/
void disconnectBtDevice(BluetoothDevice device);
/**
* 连接经典蓝牙设备
*
* <p>连接结果通过{@link BluetoothEventCallback} 回调。需要通过 {@link #registerBluetoothCallback(BluetoothEventCallback)} 注册回调。</p>
*
* @param breDevice 蓝牙设备
* @return 操作结果
*/
boolean startConnectByBreProfiles(BluetoothDevice breDevice);
/**
* 是否正在连接设备
*
* @return 结果
*/
boolean isConnecting();
/**
* 发送数据到设备
*
* @param device 蓝牙设备
* @param data 数据
* @return 操作结果
*/
boolean sendDataToDevice(BluetoothDevice device, byte[] data);
/**
* 获取历史记录列表
*
* @return 历史记录列表
*/
List<HistoryRecord> getHistoryRecordList();
/**
* 获取指定的历史记录
*
* @param devAddress 蓝牙设备地址
* @return 历史记录
*/
HistoryRecord getHistoryRecord(String devAddress);
/**
* 获取映射地址
*
* @param deviceAddress 设备地址
* @return 映射地址
*/
String getMappedDeviceAddress(String deviceAddress); //获取映射地址
/**
* 删除历史记录
*
* @param address 设备地址
* @param callback 操作回调
*/
void removeHistoryRecord(String address, OnHistoryRecordCallback callback);
/**
* 清除连接历史记录
*/
void clearHistoryRecords();
/**
* 快速连接
*/
void fastConnect();
/**
* 回连历史记录
*
* @param record 历史记录
* @param callback 操作回调
*/
void connectHistoryRecord(HistoryRecord record, OnHistoryRecordCallback callback);
/**
* 释放资源
*/
void destroy();
}
3.2 蓝牙事件回调接口
public interface IBluetoothEventCallback {
/**
* 蓝牙适配器状态回调
*
* @param bEnabled 蓝牙是否打开
* @param bHasBle 是否支持BLE
*/
void onAdapterStatus(boolean bEnabled, boolean bHasBle);
/**
* 发现设备状态回调
*
* @param bBle 是否BLE设备发现
* @param bStart 是否搜索开始
*/
void onDiscoveryStatus(boolean bBle, boolean bStart);
/**
* 发现设备回调
*
* @param device 蓝牙设备
* @param bleScanMessage 广播信息
*/
void onDiscovery(BluetoothDevice device, BleScanMessage bleScanMessage);
/**
* 产品弹窗回调
*
* @param device 蓝牙设备
* @param bleScanMessage 广播信息
*/
void onShowDialog(BluetoothDevice device, BleScanMessage bleScanMessage);
/**
* 蓝牙设备配对状态回调
*
* @param device 蓝牙设备
* @param status 配对状态
* <p>参考值如下<br>
* {@link BluetoothDevice#BOND_NONE} or
* {@link BluetoothDevice#BOND_BONDING} or
* {@link BluetoothDevice#BOND_BONDED}
* </p>
*/
void onBondStatus(BluetoothDevice device, int status);
/**
* 设备UUID获取回调
*
* @param device: 配对的设备
* @param uuids; 设备uuid数组
*/
void onDeviceUuidsDiscovery(BluetoothDevice device, ParcelUuid[] uuids);
/**
* A2DP服务状态回调
*
* @param device 蓝牙设备
* @param status 连接状态
* <p>参考值如下<br>
* {@link android.bluetooth.BluetoothProfile#STATE_DISCONNECTED} or
* {@link android.bluetooth.BluetoothProfile#STATE_CONNECTING} or
* {@link android.bluetooth.BluetoothProfile#STATE_CONNECTED} or
* {@link android.bluetooth.BluetoothProfile#STATE_DISCONNECTING}
* </p>
*/
void onA2dpStatus(BluetoothDevice device, int status);
/**
* HFP服务状态回调
*
* @param device 蓝牙设备
* @param status 连接状态
* <p>参考值如下<br>
* {@link android.bluetooth.BluetoothProfile#STATE_DISCONNECTED} or
* {@link android.bluetooth.BluetoothProfile#STATE_CONNECTING} or
* {@link android.bluetooth.BluetoothProfile#STATE_CONNECTED} or
* {@link android.bluetooth.BluetoothProfile#STATE_DISCONNECTING}
* </p>
*/
void onHfpStatus(BluetoothDevice device, int status);
/**
* SPP连接状态回调
*
* @param device 蓝牙设备
* @param status 连接状态
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_DISCONNECT}
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_CONNECTING}
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_CONNECTED}
*/
void onSppStatus(BluetoothDevice device, int status);
/**
* 接收SPP通道数据回调
*
* @param device: 蓝牙设备
* @param sppUUID SPP通道的UUID
* @param data: 设备发送的数据
*/
void onSppDataNotification(BluetoothDevice device, UUID sppUUID, byte[] data);
/**
* BLE查找设备服务回调
* 参数:
*
* @param device: BLE设备
* @param status: 查找结果,成功为0({@link BluetoothGatt#GATT_SUCCESS}),否则为其他值
* @param services: 查找到的服务
*/
void onBleServiceDiscovery(BluetoothDevice device, int status, List<BluetoothGattService> services);
/**
* BLE设备连接状态回调
*
* @param device: BLE设备
* @param status: 连接状态
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_DISCONNECT}
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_CONNECTING}
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_CONNECTED}
*/
void onBleConnection(BluetoothDevice device, int status);
/**
* BLE数据缓冲区送数据设置回调
* 参数:
* device: BLE设备
* block 缓冲区大小
* state: 0 - 成功, 其他失败
*/
void onBleDataBlockChanged(BluetoothDevice device, int block, int status);
/**
* BLE特征Notification状态回调
*
* @param device: BLE设备
* @param serviceUuid:
* @param bEnabled: true - Notification已开启,false - Notification开启失败
*/
void onBleNotificationStatus(BluetoothDevice device, UUID serviceUuid, UUID characteristicUuid, boolean bEnabled);
/**
* 接收BLE设备发送数据回调,不遵循RCSP协议
*
* @param device: BLE设备
* @param data: BLE设备发送的数据(手机端接收到的数据)
*/
void onBleDataNotification(BluetoothDevice device, UUID serviceUuid, UUID characteristicsUuid, byte[] data);
/**
* 发送数据到BLE设备结果回调
*
* @param device: BLE设备
* @param data: 发送的数据
* @param status: 发送结果 0 - 成功, 其他失败
*/
void onBleWriteStatus(BluetoothDevice device, UUID serviceUuid, UUID characteristicsUuid, byte[] data, int status);
/**
* 系统回调蓝牙连接配置
*
* @param gatt 蓝牙Gatt对象
* @param interval 发送间隔
* @param latency 延时
* @param timeout 超时时间(单位为毫秒)
* @param status 状态码
* <p>0 为成功,其他为异常</p>
*/
void onConnectionUpdated(BluetoothGatt gatt, int interval, int latency, int timeout, int status);
/**
* 经典蓝牙蓝牙设备连接状态
*
* @param device 蓝牙设备
* @param status 连接状态
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_DISCONNECT}
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_CONNECTING}
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_CONNECTED}
*/
void onBtDeviceConnectStatus(BluetoothDevice device, int status);
/**
* 蓝牙连接状态回调
*
* @param device 蓝牙设备
* @param status 连接状态
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_DISCONNECT}
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_CONNECTING}
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#CONNECT_STATE_CONNECTED}
*/
void onConnection(BluetoothDevice device, int status);
/**
* 切换已连接且正在使用的设备的回调
*
* @param device 已连接设备
*/
void onSwitchConnectedDevice(BluetoothDevice device);
/**
* 历史连接记录发送改变
*/
void onHistoryRecordChange(int op, HistoryRecord record);
/**
* 错误事件回调
*
* @param error 错误事件
*/
void onError(ErrorInfo error);
}
Important
BluetoothEventCallback 是
IBluetoothEventCallback
接口的抽象类推荐使用 BluetoothEventCallback 回调
BleScanMessage
public class BleScanMessage implements Serializable {
/**
* 原始数据
*/
private byte[] rawData;
/**
* 过滤标识
*/
private String flagContent; //过滤规则标志
/* ------------------------------------------------------------------- *
* 固定头字段
* ------------------------------------------------------------------- */
/**
* 厂商ID
*/
private int pid;
/**
* 产品ID
*/
private int vid;
/**
* 客户ID
*/
private int uid;
/**
* 设备类型
* <p>
* 可选值:
* {@link com.jieli.bluetooth_connect.constant.JL_DeviceType#JL_DEVICE_TYPE_SOUNDBOX} --- 音箱类型
* {@link com.jieli.bluetooth_connect.constant.JL_DeviceType#JL_DEVICE_TYPE_CHARGING_BIN} --- 充电仓类型
* {@link com.jieli.bluetooth_connect.constant.JL_DeviceType#JL_DEVICE_TYPE_TWS_HEADSET_V1} --- TWS耳机-BR22系列
* {@link com.jieli.bluetooth_connect.constant.JL_DeviceType#JL_DEVICE_TYPE_TWS_HEADSET_V2} --- TWS耳机-BR23系列
* </p>
*/
private int deviceType = -1;
/**
* 协议版本
*/
private int version;
/**
* 弹窗标志
* <p>
* 说明: 1 - true 0 - false
* 描述: AC692X之前的广播包的属性
* </p>
*/
private boolean showDialog;
/**
* 设备的经典蓝牙地址
*/
private String edrAddr;
/**
* 经典蓝牙连接状态
* <p>
* 说明:1 -- 已连接 0 -- 未连接
* 描述:AC692X之前的广播包的属性
* </p>
*/
private int edrStatus;
/**
* 左设备(设备1)电量
* <p>
* 说明:取值范围 0-100 (0 视为设备不在线)
* </p>
*/
private int leftDeviceQuantity;
/**
* 左设备(设备1)充电标识
*/
private boolean isLeftCharging;
/**
* 右设备(设备2)电量
* <p>
* 说明:取值范围 0-100 (0 视为设备不在线)
* </p>
*/
private int rightDeviceQuantity;
/**
* 右设备(设备2)充电标识
*/
private boolean isRightCharging;
/**
* 充电仓(设备3)电量
* <p>
* 说明:取值范围 0-100 (0 视为设备不在线)
* </p>
*/
private int chargingBinQuantity;
/**
* 充电仓(设备3)充电标识
*/
private boolean isDeviceCharging;
/**
* TWS配对标识
* <p>
* 0 -- 未配对
* 1 -- 已配对
* </p>
*/
private int twsFlag;
/**
* 充电仓状态
* <p>
* 0 --- 关盖
* 1 --- 开盖
* </p>
*/
private int chargingBinStatus;
/**
* 主从标识
* <p>
* 0 --- 从机
* 1 --- 主机
* </p>
*/
private int mainDevFlag;
/**
* 行为/状态
* <p>
* 说明: 表示耳机状态
* 可选值:{@link com.jieli.bluetooth_connect.constant.BluetoothConstant#TWS_HEADSET_STATUS_DISMISS},
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#TWS_HEADSET_STATUS_DISCONNECTED},
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#TWS_HEADSET_STATUS_CONNECTED},
* {@link com.jieli.bluetooth_connect.constant.BluetoothConstant#TWS_HEADSET_STATUS_CONNECTING}
* </p>
*/
private int action;
/**
* 序列号
* <p>
* 说明:标识当前广播的序号,用于判断广播是否被过滤或过时
* </p>
*/
private int seq;
/**
* 充电仓模式
* <p>
* 0 --- 充电模式
* 1 --- 发射模式
* </p>
*/
private int chargingBinMode;
/**
* 哈希值
*/
private byte[] hash;
/**
* 接收信号强度
*/
private int rssi;
/**
* 是否允许连接
* <p>说明: 默认允许连接,特殊情况不允许</p>
*/
private boolean isEnableConnect = true;
/**
* 连接方式
* <p>
* 说明:0 -- BLE , 1 -- SPP
* </p>
*/
private int connectWay;
...
}
ErrorInfo
public class ErrorInfo {
private int code; //错误码
private String message; //错误描述
private BluetoothDevice device; //设备对象
...
}
3.3 历史记录操作回调
public interface OnHistoryRecordCallback {
/**
* 成功操作回调
*
* @param record 连接历史记录
*/
void onSuccess(HistoryRecord record);
/**
* 失败操作回调
*
* @param code 错误码
* @param message 错误描述
*/
void onFailed(int code, String message);
}
历史记录
public class HistoryRecord {
private int id;
//设备信息
private String name; //蓝牙设备名称
private String address = "11:22:33:44:55:66"; //蓝牙地址
private String mappedAddress; //蓝牙映射地址
private int devType; //设备类型
private int connectType; //连接方式
private int sdkFlag; //SDK标识
//产品信息
private int vid; //厂商ID
private int uid; //客户ID
private int pid; //产品ID
//经纬度信息
//left device gps(默认是左侧设备,也默认左侧设备是主机)
private double leftDevLatitude; //纬度
private double leftDevLongitude; //经度
private long leftDevUpdateTime; //更新时间
//right device gps
private double rightDevLatitude;
private double rightDevLongitude;
private long rightDevUpdateTime;
//额外信息
private long onlineTime; //上线时间
...
}
3.4 常量说明
public class BluetoothConstant {
/**
* 扫描BLE设备
*/
public static final int SCAN_TYPE_BLE = 0;
/**
* 扫描经典蓝牙设备
*/
public static final int SCAN_TYPE_CLASSIC = 1;
/**
* 扫描全部蓝牙设备
*/
public static final int SCAN_TYPE_ALL = 2;
public static final int CONNECT_TIMEOUT = 40 * 1000; //连接超时时间 - 40s
public static final int CONNECT_BLE_OR_SPP_TIMEOUT = 30 * 1000; //连接BLE/SPP超时时间 - 30s
public static final int PAIR_OR_UNPAIR_TIMEOUT = 30 * 1000; //配对或取消配对的超时时间 - 30s
public static final int DEFAULT_SCAN_TIMEOUT = 8000; //默认扫描时间 - 8s
public static final int DEFAULT_SEND_CMD_TIMEOUT = 2000; //默认发命令超时时间 - 2s
/**
* BLE的MTU最小值
*/
public final static int BLE_MTU_MIN = 20; //BLE最小MTU是23,(23 - 3 = 20)
/**
* BLE的MTU最大值
*/
public final static int BLE_MTU_MAX = 514; //BLE最大MTU是517,(517 - 3 = 514)
/* =========================================================================== *
* 连接状态
* =========================================================================== */
public final static int CONNECT_STATE_DISCONNECT = BluetoothProfile.STATE_DISCONNECTED;
public final static int CONNECT_STATE_CONNECTING = BluetoothProfile.STATE_CONNECTING;
public final static int CONNECT_STATE_CONNECTED = BluetoothProfile.STATE_CONNECTED;
/* =========================================================================== *
* 历史记录操作
* =========================================================================== */
public final static int HISTORY_OP_ADD = 0; //添加操作
public final static int HISTORY_OP_DELETE = 1; //删除操作
public final static int HISTORY_OP_MODIFY = 2; //修改操作
/* =========================================================================== *
* 耳机状态
* =========================================================================== */
/**
* 不显示弹窗
*/
public final static int TWS_HEADSET_STATUS_DISMISS = 0;
/**
* 蓝牙未连接
*/
public final static int TWS_HEADSET_STATUS_DISCONNECTED = 1;
/**
* 蓝牙已连接
*/
public final static int TWS_HEADSET_STATUS_CONNECTED = 2;
/**
* 蓝牙正在连接
*/
public final static int TWS_HEADSET_STATUS_CONNECTING = 3;
/* =========================================================================== *
* BLE过滤方式
* =========================================================================== */
/**
* 不过滤设备
*/
public static final int NONE_FILTER = 0;
/**
* 使用所有过滤规则
*/
public static final int ALL_FILTER = 1;
/**
* 只使用标志过滤规则
* <p>
* 例如"JLAISDK"作为设备标识
* </p>
*/
public static final int FLAG_FILTER = 2; //设备标识过滤方式 - 例如"JLAISDK"作为设备标识
/**
* 只使用Hash加密过滤规则
*/
public static final int HASH_FILTER = 3; //hash加密过滤方式 - 例如 对比"hash"值
/* =========================================================================== *
* 连接类型
* =========================================================================== */
/**
* BLE连接类型
*/
public static final int PROTOCOL_TYPE_BLE = 0;
/**
* SPP协议类型
*/
public static final int PROTOCOL_TYPE_SPP = 1;
...
}
Important
本SDK的连接状态是按照BluetoothProfile定义的连接状态, 使用杰理其他SDK时, 需要根据其他SDK要求, 进行状态转换。
3.5 蓝牙错误码
public class BluetoothError {
/**
* 没有错误
*/
public final static int ERR_NONE = 0;
/**
* 参数错误
*/
public final static int ERR_PARAM = 1;
/**
* 蓝牙未打开
*/
public final static int ERR_BLUETOOTH_NOT_ENABLE = 2;
/**
* 蓝牙配对/取消配对操作失败
*/
public final static int ERR_BLUETOOTH_BOND_OP = 3;
/**
* 蓝牙配对/取消配对超时
*/
public final static int ERR_BOND_OP_TIMEOUT = 4;
/**
* 蓝牙配对/取消配对进行中
*/
public final static int ERR_BOND_IN_PROGRESS = 5;
/**
* 调用反射方法异常
*/
public final static int ERR_USE_REFLECTION_METHOD = 6;
/**
* 操作正在进行
*/
public final static int ERR_OP_IN_PROGRESS = 7;
/**
* 扫描发生异常
*/
public final static int ERR_SCAN_EXCEPTION = 8;
/**
* 超时异常
*/
public final static int ERR_TIMEOUT = 9;
/**
* 未连接远端设备
*/
public final static int ERR_NOT_CONNECT_REMOTE = 10;
...
}
3.6 Gatt常见错误
public class GattError {
/* ================================================================================================================ *
* onConnectionStateChange 失败status说明
* 十进制 十六进制 说明 解决方案
* 8 0x08 连接超时 或者 心跳回复超时 直接返回错误
* 19 0x13 远端设备断开连接 直接返回错误
* 22 0x16 本地主机断开连接 直接返回错误
* 34 0x22 LMP回复超时 直接返回错误
* 62 0x3E 建立连接失败 直接返回错误
* 133 0x85 设备不可达 或者 系统操作异常 判断连接时间和剩余连接时间,决定是否重连
* 256 0x0100 连接被取消 直接返回错误
* @see https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/master/stack/include/gatt_api.h
* ================================================================================================================= */
public static final int GATT_SUCCESS = BluetoothGatt.GATT_SUCCESS;
public static final int GATT_CONN_L2C_FAILURE = 0x01;
public static final int GATT_CONN_TIMEOUT = 0x08;
public static final int GATT_CONN_TERMINATE_PEER_USER = 0x13;
public static final int GATT_CONN_TERMINATE_LOCAL_HOST = 0x16;
public static final int GATT_CONN_FAIL_ESTABLISH = 0x3E;
public static final int GATT_CONN_LMP_TIMEOUT = 0x22;
public static final int GATT_CONN_CANCEL = 0x0100;
public static final int GATT_ERROR = 0x0085; // Device not reachable
...
}