对蓝牙感兴趣可以加群讨论学习(QQ:494309361)
![BleLib库结构图.png](https://camo.githubusercontent.com/bdce647c415718904f2b00b3c303dc88975552e99c8f3817f120c69d260f8370/687474703a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f333838343131372d326335613062393563646137353135382e706e673f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970253743696d61676556696577322f322f772f383430)
该类的主要是来描述并记录蓝牙的属性和状态,如记录蓝牙名称、蓝牙MAC地址、蓝牙别名(即修改之后的名称)、蓝牙连接状态等,该类处在被保护状态,禁止外部随意生成该对象,
如果有特别需求可以通过BleFactory.create(...)进行创建该对象(并不建议)
该类是蓝牙状态类,定义了蓝牙扫描、连接、通知使能、发送、接收等状态的常量值(连接异常等状态码可参考该类)
该类是最重要的一个类,主要是蓝牙操作中用到的各个方法的实现类,是整个蓝牙的核心功能实现,BleManager是对外提供所有蓝牙操作接口的
管理类,当BluetoothLeService处理之后要把结果返回到BleManager中,然后再由BleManager对外提供接口。
该类提供了几乎所有你需要用到的方法,包括蓝牙扫描、连接、断开、蓝牙当前连接状态等等,管理了蓝牙操作的所有接口和方法。
![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.0.4'
1.初始化蓝牙(判断设备是否支持BLE,蓝牙是否打开以及6.0动态授权蓝牙权限等)
private void initBle() {
mBle = Ble.getInstance();
Ble.Options options = new Ble.Options();
options.logBleExceptions = true;//设置是否输出打印蓝牙日志
options.throwBleException = true;//设置是否抛出蓝牙异常
options.autoConnect = false;//设置是否自动连接
options.scanPeriod = 12 * 1000;//设置扫描时长
options.connectTimeout = 10 * 1000;//设置连接超时时长
options.uuid_service = UUID.fromString("0000fee9-0000-1000-8000-00805f9b34fb");//设置主服务的uuid
options.uuid_write_cha = UUID.fromString("d44bc439-abfd-45a2-b575-925416129600");//设置可写特征的uuid
mBle.init(getApplicationContext(), options);
}
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(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: " + "发送数据失败!");
}
![Demo预览图.gif](https://camo.githubusercontent.com/08843ecef57be75f6fd86d41bd9cf42e60e6b49acbb9b184b52822a4e155d147/687474703a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f333838343131372d343966303830616434346236303934362e6769663f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970)