2. 功能说明
2.1 通用接口
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
registerBluetoothCallback |
boolean: 结果 |
注册蓝牙回调监听器 |
|
unregisterBluetoothCallback |
boolean: 结果 |
注销蓝牙回调监听器 |
|
getBluetoothOption |
void |
获取蓝牙配置信息 |
|
setBluetoothOption |
void |
设置蓝牙配置信息 |
|
destroy |
void |
void |
释放资源 |
2.1.1 初始化示例
//1. 配置连接库参数
BluetoothOption option = BluetoothOption.createDefaultOption()
.setPriority(BluetoothConstant.PROTOCOL_TYPE_BLE); //默认通讯方式
option.setUseMultiDevice(false); //是否支持多设备管理
if (option.getPriority() == BluetoothConstant.PROTOCOL_TYPE_BLE) { //BLE方式
option.setNeedChangeBleMtu(true); //是否需要调整MTU
option.setMtu(BluetoothConstant.BLE_MTU_MAX); //设置BLE的MTU调整值
//scan mode
// option.setBleScanMode(ScanSettings.SCAN_MODE_BALANCED); //BLE扫描模式
//可以修改BLE通讯的UUIDs
// option.setBleUUID(BluetoothConstant.UUID_SERVICE, BluetoothConstant.UUID_WRITE, BluetoothConstant.UUID_NOTIFICATION);
} else { //SPP方式
//可以修改SPP通讯的UUID
// option.setSppUUID(BluetoothConstant.UUID_SPP);
}
//scan device
option.setScanFilterData("JLAISDK");//搜索过滤条件
option.setBleScanStrategy(BluetoothConstant.ALL_FILTER); //扫描策略
//是否允许回连- 默认是true, 选择false时,经典蓝牙连接不会触发BLE/SPP的回连
option.setReconnect(false);
//2.初始化蓝牙管理器并配置参数
BluetoothManager manager = new BluetoothManager(context, option);
//3.配置参数可以获取和修改
BluetoothOption oldOption = manager.getBluetoothOption();
oldOption.setReconnect(true) //修改允许回连
.setBleScanStrategy(BluetoothConstant.NONE_FILTER); //修改不过滤设备
manager.setBluetoothOption(oldOption); //重新配置参数
final BluetoothEventCallback callback = new BluetoothEventCallback() {
@Override
public void onConnection(BluetoothDevice device, int status) {
super.onConnection(device, status);
//回调设备连接状态
}
};
//4. 注册蓝牙事件监听器
manager.registerBluetoothCallback(callback);
//5. 注销蓝牙事件监听器
// manager.unregisterBluetoothCallback(callback);
//6. 销毁蓝牙管理器
// manager.destroy();
蓝牙事件监听器的具体内容参考 3.2 蓝牙事件回调接口
2.2 扫描设备
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
isScanning |
void |
boolean: 结果 |
是否正在扫描设备 |
getScanType |
void |
int: 扫描设备的类型 |
获取扫描设备的类型 |
getDiscoveredBluetoothDevices |
void |
ArrayList<BluetoothDevice>: 发现蓝牙设备列表 |
获取发现蓝牙设备列表 |
startDeviceScan |
timeout: 超时值(单位: 毫秒) |
boolean: 操作结果 |
开始蓝牙设备扫描 |
startDeviceScan |
type: 扫描设备类型,
timeout: 超时值(单位: 毫秒)
|
boolean: 操作结果 |
开始蓝牙设备扫描 |
stopDeviceScan |
void |
boolean: 操作结果 |
停止蓝牙设备扫描 |
startBLEScan |
timeout: 超时值(单位: 毫秒) |
boolean: 操作结果 |
开始BLE设备扫描 |
stopBLEScan |
void |
boolean: 操作结果 |
停止BLE设备扫描 |
2.2.1 扫描设备示例
int scanType = BluetoothConstant.SCAN_TYPE_BLE;
long timeout = 16 * 1000;
//1. 初始化蓝牙管理器,建议单例模式使用
BluetoothManager manager = new BluetoothManager(context, BluetoothOption.createDefaultOption());
final BluetoothEventCallback eventCallback = new BluetoothEventCallback() {
@Override
public void onDiscoveryStatus(boolean bBle, boolean bStart) {
//回调发现设备状态
if (!bStart) { //搜索结束
//已发现设备列表
List<BluetoothDevice> deviceList = manager.getDiscoveredBluetoothDevices();
}
}
@Override
public void onDiscovery(BluetoothDevice device, BleScanMessage bleScanMessage) {
//回调发现设备
}
@Override
public void onShowDialog(BluetoothDevice device, BleScanMessage bleScanMessage) {
//回调弹窗连接的信息
}
};
//2. 注册蓝牙事件监听器
manager.registerBluetoothCallback(eventCallback);
//3.开始设备搜索
//type - 扫描类型
//timeout - 搜索超时时间
manager.startDeviceScan(type, timeout);
//3.1 搜索经典蓝牙
// manager.startDeviceScan(timeout);
//3.2 搜索BLE设备
// manager.startBLEScan(timeout);
//4. 主动停止搜索设备
/*if (manager.getScanType() == BluetoothConstant.SCAN_TYPE_BLE) {
manager.stopBLEScan();
} else {
manager.stopDeviceScan();
}*/
//5. 移除蓝牙事件监听器
// manager.unregisterBluetoothCallback(eventCallback);
蓝牙事件监听器的具体内容参考 3.2 蓝牙事件回调接口
2.3 连接设备
2.3.1 通用设备连接接口
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
getConnectedDevice |
void |
BluetoothDevice: 正在使用的蓝牙设备 |
已连接蓝牙设备对象 |
setConnectedDevice |
device : 蓝牙设备 |
void |
设置已连接且正在使用的蓝牙设备 |
isConnectedDevice |
device : 蓝牙设备 |
boolean: 结果 |
判断设备是否已连接 |
getConnectedBluetoothGatt |
void |
BluetoothGatt: 蓝牙Gatt对象 |
获取已连接的BluetoothGatt对象 |
getConnectedDeviceList |
void |
List<BluetoothDevice>:连接设备列表 |
获取已连接设备列表 |
getConnectingDevice |
void |
BluetoothDevice: 连接中设备对象 |
获取当前连接中设备对象 |
isConnecting |
void |
boolean: 结果 |
是否正在连接设备 |
connectBtDevice |
device : 蓝牙设备 |
boolean: 操作结果 |
连接蓝牙设备 |
disconnectBtDevice |
device : 蓝牙设备 |
void |
断开蓝牙设备 |
startConnectByBreProfiles |
device : 蓝牙设备 |
boolean: 操作结果 |
连接经典蓝牙设备 |
fastConnect |
void |
void |
快速连接 |
2.3.1.1 连接设备示例
1public void connectDevice(Context context, BluetoothDevice device) {
2 if (null == device) return;
3 //1. 初始化蓝牙管理器,建议单例模式使用
4 BluetoothManager manager = new BluetoothManager(context, BluetoothOption.createDefaultOption());
5 //2. 判断设备是否已连接
6 if (manager.isConnectedDevice(device)) { //设备已连接
7 //2.1 判断设备是否BLE连接
8 BluetoothGatt gatt = manager.getDeviceGatt(device); //获取已连接的GATT对象
9 if (gatt != null && BluetoothUtil.deviceEquals(gatt.getDevice(), device)) {
10 //BLE 通道连接
11 } else {
12 //SPP 通道连接
13 }
14 return;
15 }
16 //3. 判断是否正在连接中
17 if (manager.isConnecting()) {
18 //正在连接中,跳过
19 return;
20 }
21 //4. 注册蓝牙事件监听器
22 final BluetoothEventCallback eventCallback = new BluetoothEventCallback() {
23 @Override
24 public void onConnection(BluetoothDevice device, int status) {
25 //4.1 回调蓝牙设备连接状态
26 }
27 };
28 manager.registerBluetoothCallback(eventCallback);
29 //5. 连接设备
30 if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE || device.getType() == BluetoothDevice.DEVICE_TYPE_DUAL) {
31 //5.1 连接BLE通道
32 manager.connectBLEDevice(device);
33 } else {
34 //5.2 连接SPP通道
35 manager.connectSPPDevice(device);
36 }
37}
2.3.1.2 断开设备示例
1public void disconnectDevice(Context context, BluetoothDevice device) {
2 if (null == device) return;
3 //1. 初始化蓝牙管理器,建议单例模式使用
4 BluetoothManager manager = new BluetoothManager(context, BluetoothOption.createDefaultOption());
5 //2. 判断设备是否已连接
6 if (manager.isConnectedDevice(device)) { //设备已连接
7 //2.1 判断设备是否BLE连接
8 if (manager.isConnectedBLEDevice(device)) {
9 //断开BLE 通道
10 manager.disconnectBLEDevice(device);
11 } else {
12 //断开SPP 通道
13 manager.disconnectSPPDevice(device);
14 }
15 }
16}
2.3.2 经典蓝牙连接接口
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
isPaired |
device : 蓝牙设备 |
boolean: 结果 |
判断蓝牙设备是否已配对 |
getPairedDevices |
void |
List<BluetoothDevice>: 已配对的蓝牙设备列表 |
获取已配对的蓝牙设备列表 |
pair |
device : 蓝牙设备 |
boolean: 操作结果 |
请求蓝牙设备进行配对过程 |
tryToPair |
device : 蓝牙设备 |
boolean: 操作结果 |
尝试进行配对过程,推荐使用 |
unPair |
device : 蓝牙设备 |
boolean: 操作结果 |
请求蓝牙设备进行取消配对 |
tryToUnPair |
device : 蓝牙设备 |
boolean: 操作结果 |
尝试进行取消配对,推荐使用 |
isConnectedByHfp |
device : 蓝牙设备 |
int: 结果码 |
蓝牙设备是否已建立hfp服务 |
isConnectedByA2dp |
device : 蓝牙设备 |
int: 结果码 |
蓝牙设备是否已建立a2dp服务 |
isConnectedByProfile |
device : 蓝牙设备 |
int: 结果码 |
蓝牙设备是否已建立经典蓝牙服务 |
isBrEdrConnecting |
void |
boolean: 结果 |
判断经典蓝牙是否连接中 |
getConnectingBrEdrDevice |
void |
device : 蓝牙设备 |
获取连接中的经典蓝牙设备 |
connectByProfiles |
device : 蓝牙设备 |
boolean: 操作结果 |
连接经典蓝牙 |
disconnectByProfiles |
device : 蓝牙设备 |
boolean: 操作结果 |
断开经典蓝牙 |
setActivityBluetoothDevice |
device : 蓝牙设备 |
boolean: 操作结果 |
设置指定的设备为音频输出设备 |
getActivityBluetoothDevice |
void |
boolean: 操作结果 |
获取当前音频输出设备对象 |
getConnectedSppList |
void |
List<BluetoothDevice>: 已连接SPP列表 |
获取已连接SPP列表 |
isConnectedSppDevice |
device : 蓝牙设备 |
boolean: 结果 |
判断设备是否已连接SPP |
connectSPPDevice |
device : 蓝牙设备 |
boolean: 操作结果 |
连接Spp通道 |
disconnectSPPDevice |
device : 蓝牙设备 |
boolean: 操作结果 |
断开Spp通道 |
2.3.2.1 连接经典蓝牙示例
1public void connectEdr(Context context, BluetoothDevice device) {
2 if (null == device) return;
3 //1. 初始化蓝牙管理器,建议单例模式使用
4 BluetoothManager manager = new BluetoothManager(context, BluetoothOption.createDefaultOption());
5 if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE || device.getType() == BluetoothDevice.DEVICE_TYPE_DUAL) {
6 //BLE设备,不能连接经典蓝牙
7 return;
8 }
9 //2. 判断经典蓝牙的连接状态
10 int edrState = manager.isConnectedByProfile(device);
11 switch (edrState) {
12 case BluetoothProfile.STATE_CONNECTED:
13 //经典蓝牙已连接
14 return;
15 case BluetoothProfile.STATE_CONNECTING:
16 //经典蓝牙正在连接
17 return;
18 }
19 //经典蓝牙未连接
20 //3. 注册蓝牙事件监听器
21 manager.registerBluetoothCallback(new BluetoothEventCallback() {
22 @Override
23 public void onBtDeviceConnectStatus(BluetoothDevice device, int status) {
24 //回调经典蓝牙的连接状态
25 }
26 });
27 //4. 开始连接经典蓝牙
28 manager.startConnectByBreProfiles(device);
29}
2.3.2.2 断开经典蓝牙示例
1public boolean disconnectEdr(Context context, BluetoothDevice device) {
2 if (null == device) return false;
3 //1. 初始化蓝牙管理器,建议单例模式使用
4 BluetoothManager manager = new BluetoothManager(context, BluetoothOption.createDefaultOption());
5 //2. 断开经典蓝牙服务
6 return manager.disconnectByProfiles(device);
7}
2.3.3 BLE设备连接接口
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
getDeviceGatt |
device : 蓝牙设备 |
BluetoothGatt: 蓝牙Gatt对象 |
通过指定BLE设备对应的BluetoothGatt对象 |
getBleMtu |
device : 蓝牙设备 |
int: 指定设备的BLE MTU |
获取已连接BLE设备的MTU |
requestBleMtu |
device : 蓝牙设备
int: 请求MTU
|
boolean: 操作结果 |
请求调整BLE的MTU |
isConnectedBLEDevice |
device : 蓝牙设备 |
boolean: 操作结果 |
判断是否为已连接的BLE设备 |
connectBLEDevice |
device : 蓝牙设备 |
boolean: 操作结果 |
连接BLE设备 |
disconnectBLEDevice |
device : 蓝牙设备 |
boolean: 操作结果 |
断开BLE设备 |
2.4 发送数据
2.4.1 通用发送数据
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
sendDataToDevice |
device : 蓝牙设备
data: 发送数据
|
boolean: 操作结果 |
发送数据到设备 |
2.4.1.1 发送数据示例
1public boolean sendData(Context context, BluetoothDevice device, byte[] data) {
2 if (null == device) return false;
3 //1. 初始化蓝牙管理器,建议单例模式使用
4 BluetoothManager manager = new BluetoothManager(context, BluetoothOption.createDefaultOption());
5 //2. 判断设备是否连接
6 if(!manager.isConnectedDevice(device)){ //设备未连接
7 return false;
8 }
9 //3. 发送数据
10 return manager.sendDataToDevice(device, data);
11}
2.4.2 SPP发送数据
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
writeDataToSppDevice |
device : 蓝牙设备
data : 发送数据
|
boolean: 操作结果 |
通过Spp通道发送数据包 |
2.4.3 BLE发送数据
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
writeDataToBLEDevice |
device : 蓝牙设备
serviceUUID: 服务UUID
characteristicUUID: 属性UUID
data: 发送数据
|
boolean: 操作结果 |
通过BLE写服务发送数据包 |
2.5 历史记录管理
函数 |
参数 |
结果 |
描述 |
---|---|---|---|
getHistoryRecordList |
void |
List<HistoryRecord>: 历史记录列表 |
获取历史记录列表 |
getHistoryRecord |
address: 蓝牙设备地址 |
HistoryRecord: 历史记录 |
获取指定的历史记录 |
getMappedDeviceAddress |
address: 蓝牙设备地址 |
address: 蓝牙设备地址 |
获取映射地址 |
removeHistoryRecord |
address: 设备地址
callback: 操作回调
|
void |
删除历史记录 |
connectHistoryRecord |
callback: 操作回调
|
void |
回连历史记录 |
clearHistoryRecords |
void |
void |
清除连接历史记录 |
2.5.1 连接历史记录示例
public boolean connectHistory(Context context, String address) {
//1. 初始化蓝牙管理器,建议单例模式使用
BluetoothManager manager = new BluetoothManager(context, BluetoothOption.createDefaultOption());
//2. 获取缓存的连接历史
HistoryRecord record = manager.getHistoryRecord(address);
if (null == record) {
//没有连接历史记录
return false;
}
//3. 判断连接历史是否连接中
if (manager.isConnectedDevice(BluetoothUtil.getRemoteDevice(record.getAddress()))) {
//设备已连接
return true;
}
//4. 判断系统是否正在连接设备
if(manager.isConnecting()){
//正在连接设备, 跳过操作
return false;
}
//5. 开始连接连接历史记录
manager.connectHistoryRecord(record, new OnHistoryRecordCallback() {
@Override
public void onSuccess(HistoryRecord record) {
//回连成功
}
@Override
public void onFailed(int code, String message) {
//回连失败
}
});
return true;
}
2.5.2 移除历史记录示例
1public boolean removeHistory(Context context) {
2 //1. 初始化蓝牙管理器,建议单例模式使用
3 BluetoothManager manager = new BluetoothManager(context, BluetoothOption.createDefaultOption());
4 //2. 获取连接历史列表
5 List<HistoryRecord> recordList = manager.getHistoryRecordList();
6 //3. 判断是否存在连接历史列表
7 if(null == recordList || recordList.isEmpty()) return false;
8 //4. 开始移除连接历史
9 manager.removeHistoryRecord(recordList.get(0).getAddress(), new OnHistoryRecordCallback() {
10 @Override
11 public void onSuccess(HistoryRecord record) {
12 //移除成功
13 }
14
15 @Override
16 public void onFailed(int code, String message) {
17 //移除失败
18 }
19 });
20 return true;
21}