这篇博客帮助用来帮助我们在App开发中需要连接蓝牙硬件设备接收与上传数据的,如果大家有什么不理解的或者更好的方法欢迎大家与我沟通。在这里我就不介绍蓝牙的基本知识了,有不理解的大家可以找一找。一起来看下如何使用吧。
1.使用准备
1.在项目级的build.gradle文件中加入aar文件。
implementation(name: 'Sbbluetooth', ext: "aar")
2.在清单文件中添加蓝牙权限
2.开始使用
1.在自定义的Application的onCreate里面初始化
SBluetoothContext.set(this);
2.使用
implementation(name: 'Sbbluetooth', ext: "aar")
2.在清单文件中添加蓝牙权限
2.开始使用
1.在自定义的Application的onCreate里面初始化
SBluetoothContext.set(this);
2.使用
1.在自定义的Application的onCreate里面初始化
SBluetoothContext.set(this);
2.使用
1.创建ClientManager类,单例管理。
public class ClientManager {
private static SBluetoothClient mClient;
public static SBluetoothClient getClient() {
if (mClient == null) {
synchronized (ClientManager.class) {
if (mClient == null) {
mClient = new SBluetoothClient(AppAplication.getInstance());
}
}
}
return mClient;
}
}
2.在需要扫描蓝牙设备的地方使用,使用之前先判断蓝牙是否开启,如果没有开启就打开蓝牙。
if (ClientManager.getClient().isBluetoothOpened()) {
ClientManager.getClient().openBluetooth();
}
3.搜索蓝牙设备并连接设备。
SearchRequest request = new SearchRequest.Builder()
.searchBluetoothLeDevice(3000, 3) // 扫BLE设备3次,每次3s
.searchBluetoothClassicDevice(5000) // 扫经典蓝牙5s
.searchBluetoothLeDevice(5000) // 扫BLE设备5s
.build();
ClientManager.getClient().search(request, searchResponse);
ClientManager.getClient().registerBluetoothBondListener(mBluetoothBondListener);
private List
4.发送和接收数据
private void notify(){
//通知接收数据
ClientManager.getClient().notify(“设备的mac地址”, UUID.fromString(Service), UUID.fromString(character), mNotifyRsp);
}
private final BleNotifyResponse mNotifyRsp = new BleNotifyResponse() {
@Override
public void onNotify(UUID service, UUID character, byte[] value) {
//在这里根据service和character判断当前收到的数据是否是你需要的,如果是就
//解析value里面的值就可以了,下面这个方法可以把值变成字符串形式
String s = String.format(ByteUtils.byteToString(value));
}
@Override
public void onResponse(int code) {
if (code == Constants.REQUEST_SUCCESS) {
Log.d("onResponse: ", "成功");
} else {
Log.d("onResponse: ", "失败");
}
}
};
//这个是给设备发送数据,需要把十六进制字符串转成字节数组发送,
//当设备收到数据并给手机回复数据时,会通过上面的mNotifyRsp把数据返回给手机,咱们可以在那里
//解析数据。要注意这里写的byte[]不能超过20字节,如果超过了需要自己分成几次写。建议的办法是第一个//byte放剩余要写的字节的长度。writeNoRsp方法比普通的write快2~3倍,建议用于固件升级。
ClientManager.getClient().write(mac, UUID.fromString(Service), UUID.fromString(character), HexStringToByteArray(s), new BleWriteResponse() {
@Override
public void onResponse(int code) {
Log.e("onResponse: ", "获取数据:" + code);
if (code == REQUEST_SUCCESS) {
Toast.makeText(ShouHuanActivity.this, "请求发送成功", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_FAILED) {
Toast.makeText(ShouHuanActivity.this, "请求发送失败", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_CANCELED) {
Toast.makeText(ShouHuanActivity.this, "请求发送取消", Toast.LENGTH_SHORT).show();
} else if (code == ILLEGAL_ARGUMENT) {
Toast.makeText(ShouHuanActivity.this, "请求参数非法", Toast.LENGTH_SHORT).show();
} else if (code == BLE_NOT_SUPPORTED) {
Toast.makeText(ShouHuanActivity.this, "蓝牙不支持", Toast.LENGTH_SHORT).show();
} else if (code == BLUETOOTH_DISABLED) {
Toast.makeText(ShouHuanActivity.this, "蓝牙未开启", Toast.LENGTH_SHORT).show();
} else if (code == SERVICE_UNREADY) {
Toast.makeText(ShouHuanActivity.this, "服务未开启", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_TIMEDOUT) {
Toast.makeText(ShouHuanActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_DENIED) {
Toast.makeText(ShouHuanActivity.this, "请求被拒绝", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_EXCEPTION) {
Toast.makeText(ShouHuanActivity.this, "请求报错", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_UNKNOWN) {
Toast.makeText(ShouHuanActivity.this, "未知请求", Toast.LENGTH_SHORT).show();
}
}
});
public static byte[] HexStringToByteArray(String hexString){
//hexString的长度对2取整,作为bytes的长度
int len = hexString.length()/2;
byte[] bytes = new byte[len];
byte high = 0;//字节高四位
byte low = 0;//字节低四位
for(int i=0;i
//当设备提供read操作时可用此方法直接获取返回值
ClientManager.getClient().read(mac, UUID.fromString(Service), UUID.fromString(character), new BleReadResponse() {
@Override
public void onResponse(int code, byte[] data) {
if (code == REQUEST_SUCCESS) {
Toast.makeText(ShouHuanActivity.this, "请求发送成功", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_FAILED) {
Toast.makeText(ShouHuanActivity.this, "请求发送失败", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_CANCELED) {
Toast.makeText(ShouHuanActivity.this, "请求发送取消", Toast.LENGTH_SHORT).show();
} else if (code == ILLEGAL_ARGUMENT) {
Toast.makeText(ShouHuanActivity.this, "请求参数非法", Toast.LENGTH_SHORT).show();
} else if (code == BLE_NOT_SUPPORTED) {
Toast.makeText(ShouHuanActivity.this, "蓝牙不支持", Toast.LENGTH_SHORT).show();
} else if (code == BLUETOOTH_DISABLED) {
Toast.makeText(ShouHuanActivity.this, "蓝牙未开启", Toast.LENGTH_SHORT).show();
} else if (code == SERVICE_UNREADY) {
Toast.makeText(ShouHuanActivity.this, "服务未开启", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_TIMEDOUT) {
Toast.makeText(ShouHuanActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_DENIED) {
Toast.makeText(ShouHuanActivity.this, "请求被拒绝", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_EXCEPTION) {
Toast.makeText(ShouHuanActivity.this, "请求报错", Toast.LENGTH_SHORT).show();
} else if (code == REQUEST_UNKNOWN) {
Toast.makeText(ShouHuanActivity.this, "未知请求", Toast.LENGTH_SHORT).show();
}
String s = Utils.byte2hex(data);
}
});
public static final String byte2hex(byte b[]) {
if (b == null) {
throw new IllegalArgumentException(
"Argument b ( byte array ) is null! ");
}
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xff);
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
5:读取Rss
mClient.readRssi(MAC, new BleReadRssiResponse() {
@Override
public void onResponse(int code, Integer rssi) {
if(code == REQUEST_SUCCESS) {
}
}
});
6.关闭Notify
mClient.unnotify(MAC, serviceUUID, characterUUID, new BleUnnotifyResponse() {
@Override
public void onResponse(int code) {
if(code == REQUEST_SUCCESS) {
}
}
});
7.关闭Indicate
mClient.unindicate(MAC, serviceUUID, characterUUID, new BleUnnotifyResponse() {
@Override
public void onResponse(int code) {
if(code == REQUEST_SUCCESS) {
}
}
});
3.总结
蓝牙连接设备时要判断好连接状态和搜索状态等与页面的关系,避免出现空指针的情况。有什么要讨论的问题可以留言呦。



