对蓝牙感兴趣可以加群讨论学习(QQ:494309361)
有个人项目或者定制化需求的可加QQ:823581722 进行联系
![二维码.png](https://camo.githubusercontent.com/b6f48b09309c59e46ef37db4778b350747ae841e7979a09a43f1ab07077239f7/68747470733a2f2f7777772e70677965722e636f6d2f6170702f7172636f64652f44723864)
![BleLib库结构图.png](https://camo.githubusercontent.com/bdce647c415718904f2b00b3c303dc88975552e99c8f3817f120c69d260f8370/687474703a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f333838343131372d326335613062393563646137353135382e706e673f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970253743696d61676556696577322f322f772f383430)
该类的主要是来描述并记录蓝牙的属性和状态,如记录蓝牙名称、蓝牙MAC地址、蓝牙别名(即修改之后的名称)、蓝牙连接状态等,该类处在被保护状态,禁止外部随意生成该对象,
如果有特别需求可以通过BleFactory.create(...)进行创建该对象(并不建议)
该类是蓝牙状态类,定义了蓝牙扫描、连接、通知使能、发送、接收等状态的常量值(连接异常等状态码可参考该类)
该类是最重要的一个类,主要是蓝牙操作中用到的各个方法的实现类,是整个蓝牙的核心功能实现,BleManager是对外提供所有蓝牙操作接口的
管理类,当BluetoothLeService处理之后要把结果返回到BleManager中,然后再由BleManager对外提供接口。
该类提供了几乎所有你需要用到的方法,包括蓝牙扫描、连接、断开、蓝牙当前连接状态等等,管理了蓝牙操作的所有接口和方法。
![Version](https://camo.githubusercontent.com/a35b4f6dd05b4a14ef6b83927b1ab85ee5629dac8602c25ac5a6b2976929ecb8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e362e302d626c75652e737667)
![Version](https://camo.githubusercontent.com/aa08076c63394505bc87ba6a95e942d52373c7d161b212a975b6a368b472dcb6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e352e342d626c75652e737667)
1、添加过滤扫描设备接口
mBle = Ble.options()
.setLogBleExceptions(true)
.setThrowBleException(true)
.setAutoConnect(true)
.setFilterScan(true)//设置是否过滤扫描到的设备
.setConnectFailedRetryCount(3)
.setConnectTimeout(10 * 1000)
.setScanPeriod(12 * 1000)
.setUuidService(UUID.fromString("0000fee9-0000-1000-8000-00805f9b34fb"))
.setUuidWriteCha(UUID.fromString("d44bc439-abfd-45a2-b575-925416129600"))
.create(getApplicationContext());
2、优化自动重连接口
3、添加Android5.0发送广播的接口
byte[] payload = new byte[16];
payload[0] = 0x01;
mBle.startAdvertising(payload);
4、收到广播包并解析后的数据
BleScanCallback<BleDevice> scanCallback = new BleScanCallback<BleDevice>() {
@Override
public void onLeScan(final BleDevice device, int rssi, byte[] scanRecord) {
Log.e(TAG, "onLeScan: "+device.getBleAddress());
synchronized (mBle.getLocker()) {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
}
//收到广播包并解析后的数据
@Override
public void onParsedData(BleDevice device, ScanRecord scanRecord) {
super.onParsedData(device, scanRecord);
byte[] data = scanRecord.getManufacturerSpecificData(65535);//参数为厂商id
if (data != null){
Log.e(TAG, "onParsedData: "+ ByteUtils.BinaryToHexString(data));
}
}
};
![Version](https://camo.githubusercontent.com/63239c36a2353e604b1bed926e0ce82c6168f7ec32abee17ea6000bab4ec2619/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e352e332d626c75652e737667)
1、添加发送大数据量的进度回调
private void sendEntityData() throws IOException {
byte[] data = ByteUtils.toByteArray(getAssets().open("WhiteChristmas.bin"));
Log.e(TAG, "sendEntityData: "+data.length);
mBle.writeEntity(mBle.getConnetedDevices().get(0), data, 20, 50, new BleWriteEntityCallback<BleDevice>() {
@Override
public void onWriteSuccess() {
L.e("writeEntity", "onWriteSuccess");
}
@Override
public void onWriteFailed() {
L.e("writeEntity", "onWriteFailed");
}
@Override
public void onWriteProgress(double progress) {
Log.e("writeEntity", "当前发送进度: "+progress);
}
@Override
public void onWriteCancel() {
Log.e(TAG, "onWriteCancel: ");
}
});
}
2、修复持续接收通知回调过快导致数据重复的问题
3、修复多设备连接,同时断开所有设备,回调次数不完整的问题
![Version](https://camo.githubusercontent.com/27043b0051fce05d1dc46fb02bd7a6454e0f06724fe0db9fd8e82f83cd82870b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e352e32253230626574612d626c75652e737667)
1、添加自动连接的接口(使用新的初始化写法)
mBle = Ble.options()
.setLogBleExceptions(true)
.setThrowBleException(true)
.setAutoConnect(true)//自动重连
.setConnectFailedRetryCount(3)
.setConnectTimeout(10 * 1000)
.setScanPeriod(12 * 1000)
.setUuidService(UUID.fromString("0000fee9-0000-1000-8000-00805f9b34fb"))
.setUuidWriteCha(UUID.fromString("d44bc439-abfd-45a2-b575-925416129600"))
.create(getApplicationContext());
2、优化断开连接后,自动移除通知监听的问题
![Version](https://camo.githubusercontent.com/bedab267142c6c0a93c160e7736c8df38e3fddacc2969156f59992d1afdc862b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e352e302d626c75652e737667)
1、添加了发送大数据包(如:文件等)的接口
try {
//获取整个文件的总字节
byte[]data = toByteArray(getAssets().open("WhiteChristmas.bin"));
//发送大数据量的包
mBle.writeEntity(mBle.getConnetedDevices().get(0), data, 20, 50, new BleWriteEntityCallback<BleDevice>() {
@Override
public void onWriteSuccess() {
L.e("writeEntity", "onWriteSuccess");
}
@Override
public void onWriteFailed() {
L.e("writeEntity", "onWriteFailed");
}
});
} catch (IOException e) {
e.printStackTrace();
}
2、修复了正在扫描时,关闭蓝牙导致停止扫描onStop()不回调的问题
![Version](https://camo.githubusercontent.com/446b9e9cb1830f030d7a034a60863f871c383db31d505ddef4e648c66a015204/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e332e302d626c75652e737667)
1、添加通过mac地址连接的接口:
String address = device.getBleAddress();//或者 String address = "3E:9A:4A:71:F6:4D";
mBle.connect(address, new BleConnCallback<BleDevice>() {
@Override
public void onConnectionChanged(BleDevice device) {
}
});
2、添加BLE4.2的设置MTU的接口:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//此处第二个参数 不是特定的 比如你也可以设置500 但是如果设备不支持500个字节则会返回最大支持数
mBle.setMTU(mBle.getConnetedDevices().get(0).getBleAddress(), 250, new BleMtuCallback<BleDevice>() {
@Override
public void onMtuChanged(BleDevice device, int mtu, int status) {
super.onMtuChanged(device, mtu, status);
ToastUtil.showToast("最大支持MTU:"+mtu);
}
});
}else {
ToastUtil.showToast("设备不支持MTU");
}
![Version](https://camo.githubusercontent.com/35b1508604a36c5649fd15c8796ed432018e1cd7b0fad830fbb7948825623b27/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e322e302d626c75652e737667)
修复连接多个设备在onChanged()回调中判断设备对象
/*设置通知的回调*/
private BleNotiftCallback<BleDevice> bleNotiftCallback = new BleNotiftCallback<BleDevice>() {
@Override
public void onChanged(BleDevice device, BluetoothGattCharacteristic characteristic) {
UUID uuid = characteristic.getUuid();
Log.e(TAG, "onChanged==uuid:" + uuid.toString());
Log.e(TAG, "onChanged==address:"+ device.getBleAddress());
Log.e(TAG, "onChanged==data:" + Arrays.toString(characteristic.getValue()));
}
};
![Version](https://camo.githubusercontent.com/0d6cbc99460c8a916850caf1d39751f62f98c639a507605ec832f90c903c60de/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e312e352d626c75652e737667)
![Version](https://camo.githubusercontent.com/75cdc9354864f81ac2390602520ddd48f17065efcbec39c3e2db3d5bac9414bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e312e322d626c75652e737667)
1、修复上个版本依赖地址变化,导致依赖时出现问题。
2、添加清理蓝牙缓存接口
![Version](https://camo.githubusercontent.com/b742dd0ec7caaa1673110e6c7b63cca43ee1564f9ac70ef9070de29384db632d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e312e312d626c75652e737667)
![Version](https://camo.githubusercontent.com/636229996bd07d318b589b651da008f508d5e96a8b04b812e88025da898610f6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e312e302d626c75652e737667)
该版本添加跳转到测试界面(先连接设备),在测试界面操作该蓝牙对象的扫描、连接、断开、通知等回调的接口(任意界面都可以随心所欲的操作或者拿到mcu返回的数据)
1、在其他界面你也想拿到蓝牙设备传过来的数据,你可以这样做:(重要)
//测试通知
public void testNotify(BleDevice device) {
if(device != null){
mNotifyStatus.setText("设置通知监听成功!!!");
mBle.startNotify(device, new BleNotiftCallback<BleDevice>() {
@Override
public void onChanged(BluetoothGattCharacteristic characteristic) {
Log.e(TAG, "onChanged: " + Arrays.toString(characteristic.getValue()));
mNotifyValue.setText("收到MCU通知值:\n"+Arrays.toString(characteristic.getValue()));
}
});
}
}
2、在其他界面也想连接或者断开上个界面的设备对象,你可以这么做:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//测试连接或断开
final BleDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
if (mBle.isScanning()) {
mBle.stopScan();
}
if (device.isConnected()) {
//2.1.0版本新增接口(mBle.disconnect(device)接口仍可正常使用)
mBle.disconnect(device, connectCallback);
} else if (!device.isConnectting()) {
mBle.connect(device, connectCallback);
}
}
});
3、扫描、发送数据、读取数据等接口都可如上正常使用(与之前版本一样)
![Version](https://camo.githubusercontent.com/5a3d5c560eba7dcc5477ecdb9971a662bdc1f6310fd457614a52270b625ec17d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e302e352d626c75652e737667)
![Version](https://camo.githubusercontent.com/0de69bd0de9cbc370b0e3fb11fabe80ec13476a744edf08672c8ab7702b3f711/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e302e342d626c75652e737667)
该版本修复读、写、通知特征uuid相同的情况下,出现无法设置通知的BUG
![Version](https://camo.githubusercontent.com/913968ac2b4e0be7687a1c602c492546af519e40f6bc9c86b9f44ff80f277051/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e302e332d626c75652e737667)
![Version](https://camo.githubusercontent.com/082610667579fd12ba0421ffdaf34a13fb862e06e4a891d4f155b210bfbbdbec/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e302e322d626c75652e737667)
该版本增加主动读取数据以及读取远程RSSI值的接口
![Version](https://camo.githubusercontent.com/82678b6b653ad0d220a4187084bb2a2be53cc4843e579b3b191ab9ffb890d92e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76322e302e302d626c75652e737667)
该版本重构V 1.x版本,作为全新升级版本,重大类、接口进行重命名,使各个接口完成单一功能,并使之得到单一功能的回调。
注:至此V 2.0.0版本以后不再维护1.x版本
![Version](https://camo.githubusercontent.com/7f799ddde4b365be1c55e7ccdcc9721e9be19842cb507328e60a72429dc477ad/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76312e302e342d626c75652e737667)
![Version](https://camo.githubusercontent.com/ec7c9ab4c29a33bf10d9b50721a70fe32aba59a0b9d79d67ea02bf8cab9f9987/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f426c654c69622d76312e302e302d626c75652e737667)
初始版本,可完成基本的BLE蓝牙一系列操作(扫描、连接、断开、设置通知、发送数据等等)
![Download](https://camo.githubusercontent.com/8908a2155313beaa53388661caf4dd476da2bc996361cc9dd9dc8c5fa4d979e3/68747470733a2f2f6170692e62696e747261792e636f6d2f7061636b616765732f73757065726c69752f6d6176656e2f426c654c69622f696d616765732f646f776e6c6f61642e737667)
compile 'cn.com.superLei:blelibrary:2.6.0'
1.初始化蓝牙(判断设备是否支持BLE,蓝牙是否打开以及6.0动态授权蓝牙权限等)
private void initBle() {
//方式1
mBle = Ble.options()//开启配置
.setLogBleExceptions(true)//设置是否输出打印蓝牙日志(非正式打包请设置为true,以便于调试)
.setThrowBleException(true)//设置是否抛出蓝牙异常
.setAutoConnect(true)//设置是否自动连接
.setFilterScan(true)//设置是否过滤扫描到的设备
.setConnectFailedRetryCount(3)
.setConnectTimeout(10 * 1000)//设置连接超时时长(默认10*1000 ms)
.setScanPeriod(12 * 1000)//设置扫描时长(默认10*1000 ms)
.setUuidService(UUID.fromString("0000fee9-0000-1000-8000-00805f9b34fb"))//主服务的uuid
.setUuidWriteCha(UUID.fromString("d44bc439-abfd-45a2-b575-925416129600"))//可写特征的uuid
.create(getApplicationContext());
或者
//方式2 使用默认配置
mBle = Ble.create(getApplicationContext());
//方式3(同方式1)
Ble.Options options = Ble.options()
.setLogBleExceptions(true)
.setAutoConnect(true)...;
mBle = Ble.create(getApplicationContext(), options);
注意:若进行数据交互,必须进行配置uuid的各个值
}
mBle.startScan(scanCallback);
BleScanCallback<BleDevice> scanCallback = new BleScanCallback<BleDevice>() {
@Override
public void onLeScan(final BleDevice device, int rssi, byte[] scanRecord) {
Toast.makeText(BleActivity.this, "ssss", Toast.LENGTH_SHORT).show();
synchronized (mBle.getLocker()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
}
};
mBle.connect(device, connectCallback);
private BleConnCallback<BleDevice> connectCallback = new BleConnCallback<BleDevice>() {
@Override
public void onConnectionChanged(BleDevice device) {
if (device.isConnected()) {
setNotify(device);
}
Log.e(TAG, "onConnectionChanged: " + device.isConnected());
mLeDeviceListAdapter.notifyDataSetChanged();
setConnectedNum();
}
@Override
public void onConnectException(BleDevice device, int errorCode) {
super.onConnectException(device, errorCode);
Toast.makeText(BleActivity.this, "连接异常,异常状态码:" + errorCode, Toast.LENGTH_SHORT).show();
}
};
private void setNotify(BleDevice device) {
/*连接成功后,设置通知*/
mBle.startNotify(device, new BleNotiftCallback<BleDevice>() {
@Override
public void onChanged(BleDevice device, BluetoothGattCharacteristic characteristic) {
Log.e(TAG, "onChanged: " + Arrays.toString(characteristic.getValue()));
}
@Override
public void onReady(BleDevice device) {
Log.e(TAG, "onReady: ");
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt) {
Log.e(TAG, "onServicesDiscovered is success ");
}
@Override
public void onNotifySuccess(BluetoothGatt gatt) {
Log.e(TAG, "onNotifySuccess is success ");
}
});
}
5.读取远程Rssi
mBle.readRssi(mBle.getConnetedDevices().get(0), new BleReadRssiCallback<BleDevice>() {
@Override
public void onReadRssiSuccess(int rssi) {
super.onReadRssiSuccess(rssi);
Log.e(TAG, "onReadRssiSuccess: " + rssi);
Toast.makeText(BleActivity.this, "onReadRssiSuccess:"+ rssi, Toast.LENGTH_SHORT).show();
}
});
boolean result = mBle.write(device, changeLevelInner(), new BleWriteCallback<BleDevice>() {
@Override
public void onWriteSuccess(BluetoothGattCharacteristic characteristic) {
Toast.makeText(BleActivity.this, "发送数据成功", Toast.LENGTH_SHORT).show();
}
});
if (!result) {
Log.e(TAG, "changeLevelInner: " + "发送数据失败!");
}
//找到你需要升级文件的路径(一般情况都是保存再服务器上,一旦有更新会自动提示,然后APP下载并保存到本地,生成对应的file对象)
File file = new File(...);
//读写SD卡权限,此处略(6.0及以上需添加)
OtaManager mOtaManager = new OtaManager(BleActivity.this);
boolean result = mOtaManager.startOtaUpdate(file, (BleDevice) mBle.getConnetedDevices().get(0), mBle);
Log.e("OTA升级结果:", result + "");
![Demo预览图.gif](https://camo.githubusercontent.com/08843ecef57be75f6fd86d41bd9cf42e60e6b49acbb9b184b52822a4e155d147/687474703a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f333838343131372d343966303830616434346236303934362e6769663f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970)
![](https://camo.githubusercontent.com/ecacb94f26d3cea7119d9cd9ce3afe1bfb90af7199980fe3426df60b053b4caf/68747470733a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f333838343131372d356432326165383431383061393365642e6a70673f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970253743696d61676556696577322f322f772f323430)
注:打赏后可留言您的需求以及建议,本人会进行定期的更新优化库的体验,多谢支持!