使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释

时间:2022-06-14 05:28:13

转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50909410

使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释


BleLib是Android低功耗蓝牙4.0及以上开发的辅助库。一行代码解决Ble初始化、扫描、连接、特性读写、设置通知等操作。

BleLib支持单个Ble连接或多个Ble设备同一时候连接。

BleLib中的关键类:

  • BleService是单个Ble连接操作的服务类
  • GattAttributes类中包括了蓝牙联盟规定的服务和特征的UUID值
  • MultipleBleService类是可多个蓝牙设备同一时候连接的服务类

用法

第一步:加入BleLib库依赖

因此。在你项目Module中的build.gradle文件里加入库依赖就可以。例如以下:

dependencies {
compile 'com.junkchen.blelib:blelib:1.0.4'
}

仅仅此一句就可以使用BleLib库,方便吧,要的就是这效果。

使用Android Studio时依照例如以下方式加入依赖比較好,获取的是最新的版本号,结果和上面是一样的,进入模块的库依赖设置。搜索blelib就可以获取:

使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释


第二步:绑定BleLib服务

BleLib库中的Ble服务类继承了Service,因此建议绑定服务进行使用。

(假设项目中须要同一时候连接多了Ble设备进行控制请绑定MultipleBleService)

private BleService mBleService;
private boolean mIsBind;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBleService = ((BleService.LocalBinder) service).getService();
if (mBleService.initialize()) {
if (mBleService.enableBluetooth(true)) {
mBleService.scanLeDevice(true);
Toast.makeText(BleScanActivity.this, "Bluetooth was opened", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(BleScanActivity.this, "not support Bluetooth", Toast.LENGTH_SHORT).show();
}
} @Override
public void onServiceDisconnected(ComponentName name) {
mBleService = null;
mIsBind = false;
}
}; private void doBindService() {
Intent serviceIntent = new Intent(this, BleService.class);
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} private void doUnBindService() {
if (mIsBind) {
unbindService(serviceConnection);
mBleService = null;
mIsBind = false;
}
}

第三步:初始化操作

当服务绑定后可进行初始化操作,推断该机是否支持蓝牙。调用例如以下方法:

mBleService.initialize();//Ble初始化操作  

该方法会返回一个boolean值。返回true表示初始化成功。支持蓝牙;返回false表示初始化操作失败。则兴许的全部操作都不能进行。


第四步:打开蓝牙

当初始化操作成功后就能够打开蓝牙了,调用例如以下方法:

mBleService.enableBluetooth(boolean enable);//打开或关闭蓝牙 

该方法须要传入一个boolean类型的參数。true表示打开蓝牙,false表示关闭蓝牙;并返回boolean參数,返回true表示蓝牙打开,否则关闭。


第五步:扫描Ble设备

当蓝牙打开后能够进行Ble设备的扫描了,调用例如以下方法:

mBleService.scanLeDevice(boolean enable, long scanPeriod);//启动或停止扫描Ble设备  

调用该方法须要传入一个boolean參数,true表示開始进行扫描Ble设备,false表示停止扫描,默认扫描10秒钟后停止,假设想要自己设定扫描的时间,能够输入一个long型參数,表示时间单位为毫秒,如3000表示3秒后停止扫描,扫描结束是会发出广播。

扫描的结果能够从扫描监听或者广播接收器两种方式获取,设置方法例如以下:

监听方式接收扫描到的Ble设备

//Ble扫描回调
mBleService.setOnLeScanListener(new BleService.OnLeScanListener() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
//每当扫描到一个Ble设备时就会返回,(扫描结果反复的库中已处理)
}
});

注意:设置监听一定要等到BleService服务绑定之后才进行,否则会造成空指针异常)

广播接收扫描到的Ble设备

private BroadcastReceiver bleReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BleService.ACTION_BLUETOOTH_DEVICE)) {
String tmpDevName = intent.getStringExtra("name");
String tmpDevAddress = intent.getStringExtra("address");
Log.i(TAG, "name: " + tmpDevName + ", address: " + tmpDevAddress);
} else if (intent.getAction().equals(BleService.ACTION_SCAN_FINISHED)) {
//扫描Ble设备停止
}
}
};

两种方式获取都能够。可是监听方式获取的信息会多一些。可依据自己的需求进行选择使用那种方式。


第六步:连接Ble服务

当扫描到Ble设备后就能够进行连接操作了。调用例如以下方法:

mBleService.connect(String address);//连接Ble
mBleService.disconnect();//取消连接

连接须要传入要连接的Ble设备的地址。

连接状态能够从连接监听或者广播接收器两种方式获取。设置方法例如以下:

监听获取Ble连接状态

//Ble连接回调
mBleService.setOnConnectListener(new BleService.OnConnectListener() {
@Override
public void onConnect(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
//Ble连接已断开
} else if (newState == BluetoothProfile.STATE_CONNECTING) {
//Ble正在连接
} else if (newState == BluetoothProfile.STATE_CONNECTED) {
//Ble已连接
} else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
//Ble正在断开连接
}
}
});

广播接收Ble连接状态

private BroadcastReceiver bleReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BleService.ACTION_GATT_CONNECTED)) {
//Ble已连接
} else if (intent.getAction().equals(BleService.ACTION_GATT_DISCONNECTED)) {
//Ble连接已断开
}
}
};

当连接上Ble后会进行服务的获取。假设服务和特性不能发现,那么就不能进行特性的读写和设置GATT通知。

服务发现监听设置例如以下:

//Ble服务发现回调
mBleService.setOnServicesDiscoveredListener(new BleService.OnServicesDiscoveredListener() {
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) { }
});

第七步:读写Ble特性和接收GATT通知

连接上Ble并获取服务之后就能够对特性进行读写,设置GATT通知。操作例如以下:

mBleService.setCharacteristicNotification();//设置通知
mBleService.readCharacteristic();//读取数据
mBleService.writeCharacteristic();//写入数据

特性读取的数据和GATT通知接收数据设置OnDataAvailableListener监听获取。设置例如以下:

//Ble数据回调
mBleService.setOnDataAvailableListener(new BleService.OnDataAvailableListener() {
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//处理特性读取返回的数据
} @Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
//处理通知返回的数据
}
});

总结

最后小小总结下使用BleLib库进行Android低功耗蓝牙Ble的开发步骤:

  1. 加入BleLib库依赖
  2. 绑定BleLib服务
  3. 初始化操作
  4. 打开蓝牙
  5. 扫描Ble设备
  6. 连接Ble服务
  7. 读写Ble特性和接收GATT通知

Author

2016/3/16 21:49:15