github地址:https://github.com/qindachang/BluetoothLELibraryjava
该库只支持1对1链接,若是你想1对多设备链接,请移步至 BluetoothLE-Multi-Libraryandroid
demo运行环境 Android Studio 2.3git
低功耗蓝牙库。优点:github
引入方式缓存
添加依赖app
做为第一步,依赖这个库添加到您的项目。如何使用库, Gradle是推荐的方式使用这个库的依赖。ide
添加如下代码在你的APP级别 app build.gradle:post
compile 'com.qindachang:BluetoothLELibrary:0.7.4'
权限:gradle
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
5.0以上须要ui
<!-- 只有当你的 targets API 等于或大于 Android 5.0 (API level 21) 才须要此权限 --> <uses-feature android:name="android.hardware.location.gps" />
6.0以上设备须要
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
前戏
是否支持蓝牙
mBluetoothLe.isSupportBluetooth();
判断蓝牙是否打开
mBluetoothLe.isBluetoothOpen();
请求打开蓝牙
mBluetoothLe.enableBluetooth(activity.this);
关闭蓝牙
mBluetoothLe.disableBluetooth();
1、获取单例实例
BluetoothLe mBluetoothLe = BluetoothLe.getDefault();
2、初始化
mBluetoothLe.init(this);//必须调用init()初始化
或者使用配置方式进行初始化,这样你能够针对本身的蓝牙产品,作出个性化的蓝牙队列请求。
such as : 发送队列间隔时间设置,因某些公司蓝牙操做要求时间间隔,例如150ms间隔才能发送下一条数据
在Application的onCreate()方法中参照如下方式配置:
public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); BluetoothConfig config = new BluetoothConfig.Builder() .enableQueueInterval(true)//开启队列定时 .setQueueIntervalTime(150)//设置定时150ms时长(才会发下一条),单位ms .build(); BluetoothLe.getDefault().init(this, config); } }
固然,你也可使用自动的方式来配置蓝牙队列请求。这个时间间隔是经过读取远程蓝牙设备的最小间隔和最大间隔计算得出,保证了队列的最大可用性。
BluetoothConfig config = new BluetoothConfig.Builder() .enableQueueInterval(true) .setQueueIntervalTime(BluetoothConfig.AUTO)//发送时间间隔将根据蓝牙硬件自动得出 .build(); BluetoothLe.getDefault().init(this, config);
上述的读取远程蓝牙设备的最小间隔和最大间隔,你能够在连上蓝牙发现服务后读取:
@Override
public void onServicesDiscovered(BluetoothGatt gatt) { mStringBuilder.append("发现服务啦\n"); mTvText.setText(mStringBuilder.toString()); mHandler.postDelayed(new Runnable() { @Override public void run() { //查看远程蓝牙设备的链接参数,如蓝牙的最小时间间隔和最大时间间隔等.. Log.d("debug", mBluetoothLe.readConnectionParameters().toString()); } }, 1000); }
在使用途中修改以上配置:
修改配置:
BluetoothConfig config = new BluetoothConfig.Builder() .enableQueueInterval(false) .build(); mBluetoothLe.changeConfig(config);
3、扫描
扫描过程已携带6.0动态权限申请:地理位置权限
mBluetoothLe.setScanPeriod(15000)//设置扫描时长,单位毫秒,默认10秒 .setScanWithDeviceAddress("00:20:ff:34:aa:b3")//根据硬件地址过滤扫描 .setScanWithServiceUUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")//设置根据服务uuid过滤扫描 .setScanWithDeviceName("ZG1616")//设置根据设备名称过滤扫描 .setReportDelay(0)//若是为0,则回调onScanResult()方法,若是大于0, 则每隔你设置的时长回调onBatchScanResults()方法,不能小于0 .startScan(Activity activity);
根据多个硬件地址、服务uuid、设备名称过滤扫描,你能够这样:
.setScanWithDeviceAddress(new String[]{"00:20:ff:34:aa:b3","f3:84:55:b4:ab:7f"}) .setScanWithServiceUUID(new String[]{"0000180d-0000-1000-8000-00805f9b34fb","6E400001-B5A3-F393-E0A9-E50E24DCCA9E"}) .setScanWithDeviceName(new String[]{"ZG1616","HaHa"})
获取蓝牙扫描状态:
mBluetoothLe.getScanning();
中止扫描
mBluetoothLe.stopScan();
4、链接蓝牙、蓝牙链接状态
//发送数据、开启通知等操做,必须等待onServicesDiscovered()发现服务回调后,才能去操做 //参数:false为关闭蓝牙自动重连,若是为true则自动重连 mBluetoothLe.startConnect(false, mBluetoothDevice);
获取蓝牙链接状态:
mBluetoothLe.getConnected();
获取发现服务状态:
mBluetoothLe.getServicesDiscovered();
断开链接
mBluetoothLe.disconnect();
5、发送数据(到蓝牙特征)
//如下两个参数为硬件工程师提供,请你与你司的硬件工程师沟通 private static final String SERVICE_UUID = "0000180d-0000-1000-8000-00805f9b34fb"; private static final String WRITE_UUID = "0000fff5-0000-1000-8000-00805f9b34fb"; mBluetoothLe.writeDataToCharacteristic(bytes, SERVICE_UUID, WRITE_UUID);
6、Notification类型通知
private static final String SERVICE_UUID = "0000180d-0000-1000-8000-00805f9b34fb"; private static final String HEART_NOTIFICATION_UUID = "00002a37-0000-1000-8000-00805f9b34fb"; private static final String STEP_NOTIFICATION_UUID = "0000fff3-0000-1000-8000-00805f9b34fb";
开启一个通知
mBluetoothLe.enableNotification(true, SERVICE_UUID, STEP_NOTIFICATION_UUID);
开启多个通知
mBluetoothLe.enableNotification(true, SERVICE_UUID, new String[]{HEART_NOTIFICATION_UUID, STEP_NOTIFICATION_UUID});
7、Indication类型通知
开启一个通知
mBluetoothLe.enableIndication(true, SERVICE_UUID, STEP_NOTIFICATION_UUID);
开启多个通知
mBluetoothLe.enableIndication(true, SERVICE_UUID, new String[]{HEART_NOTIFICATION_UUID, STEP_NOTIFICATION_UUID});
8、读取数据
private static final String SERVICE_UUID = "0000180d-0000-1000-8000-00805f9b34fb"; private static final String READ_UUID = "0000fff5-0000-1000-8000-00805f9b34fb"; mBluetoothLe.readCharacteristic(SERVICE_UUID, READ_UUID);
9、蓝牙信号强度、距离
mBluetoothLe.setReadRssiInterval(2000)//设置读取信号强度间隔时间,单位毫秒 .setOnReadRssiListener(TAG, new OnLeReadRssiListener() { @Override public void onSuccess(int rssi, int cm) { } });
中止监听蓝牙信号强度
mBluetoothLe.stopReadRssi();
//监听扫描 //Every Bluetooth-LE commands status will be callback in here. Flowing listener: mBleManager.setOnScanListener(TAG, new OnLeScanListener() { @Override public void onScanResult(BluetoothDevice bluetoothDevice, int rssi, ScanRecord scanRecord) { } @Override public void onBatchScanResults(List<ScanResult> results) { } @Override public void onScanCompleted() { } @Override public void onScanFailed(ScanBleException e) { } });
更多的相似于
mBleManager.setOnConnectListener(...)//监听链接 mBleManager.setOnNotificationListener(...)//监听通知 mBleManager.setOnIndicateListener(...)//监听通知 mBleManager.setOnWriteCharacteristicListener(...)//监听写 mBleManager.setOnReadCharacteristicListener(...)//监听读 mBleManager.setOnReadRssiListener(...)//监听信号强度
拥有TAG的监听将能够在多个界面中产生回调,这能够帮助你实现多个Activity或Fragment监听蓝牙状态的需求。如不使用TAG的监听,将只有一个回调。
使用TAG监听,须要在生命周期onDestroy()中调用mBluetoothLe.destroy(TAG); 如不使用TAG监听,须要在生命周期onDestroy()中调用mBluetoothLe.destroy();
清理蓝牙缓存
请你在链接上蓝牙后,再执行这步操做
mBluetoothLe.clearDeviceCache();
关闭GATT
在你退出应用的时候使用
mBluetoothLe.close();
取消队列
假设当你在for循环中发送100条数据,想要在中途取消余下的发送
mBluetoothLe.clearQueue();
在Activity生命周期onDestroy() 中使用:
mBluetoothLe.destroy();
若是你使用了tag标签的监听,使用: (它的好处是你能够在多个界面产生多个相同的回调)
mBluetoothLe.destroy(TAG);
取消对应tag:
mBluetoothLe.cancelTag(TAG);
取消所有tag:
mBluetoothLe.cancelAllTag();
强烈建议阅读Demo : MainActivity.java / activity_main.xml
如何在多个Activity和Fragment中使用:https://github.com/qindachang/BluetoothLELibrary/tree/master/app/src/main/java/com/qindachang/bluetoothlelibrary/ui/demo