蓝牙框架FastBle原理详解
搞过蓝牙开发的小伙伴都比较清楚,android4.3之后低功耗的蓝牙隐藏着很多坑,比如搜索设备如果频繁的调用startScan方法是会不回调搜索接口的,这是因为蓝牙底层做了优化;比如蓝牙关闭重新搜索搜不到设备或者连接不上设备有可能你没调用BluetoothGatt.close()方法,在蓝牙断开或主动断开的时候你必须调用close方法,因为手机蓝牙可以存在的连接的数量是有限制的,一般是最多存在多少个未回收的BluetoothGatt,不管它是连接状态还是断开未处理状态,反正再重新连接时就连不上了;蓝牙连接的connect在有些机型上必须在UI线程中执行,否则连接不上,比如三星的一些手机;还有蓝牙的读写必须是串行的,如果你连续调用write方法那么有可能哪一条发送会直白;比如蓝牙接收数据和发送数据,如果嵌入式那边不可以改变mtu大小的话,默认是一次只能发送20个字节,再多的话只能通过多次拆分write才能保证数据的完整性。
那么从这些角度咱们再去分析FastBle是怎么避免这些问题以及如何封装成框架供小伙伴们使用的呢,想成为架构师的小伙伴前期可以多研究一些框架的原理,慢慢将所学架构思想用于实践慢慢肯定可以成为架构师。
FastBle使用都来在于BleManager,它像Glide、像EventBus、像Arouter等等,都是首先一个外观模式类给用户调用它所有的功能,也就是所有的框架架构模式都有相似性。
public static BleManager getInstance() {
return BleManagerHolder.sBleManager;
}
/**
静态内部类的单例模式
**/
private static class BleManagerHolder {
private static final BleManager sBleManager = new BleManager();
}
首先采用了静态内部类实现延迟加载的单例类,单例实现的基本方式不了解的小伙伴可以研究一下,静态内部类的实现方式好处多多,java官方demo就是这种方式。
好了继续,蓝牙连接的顺序是让用户授权蓝牙权限和定位权限、然后必须打开蓝牙,然后搜索,前面两部暂且不提,看一下这个框架的搜索是怎么实现的?首先配置扫描的参数如下:
配置扫描的参数
BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder()
.setServiceUuids(serviceUuids) // 只扫描指定的服务的设备,可选
.setDeviceName(true, names) // 只扫描指定广播名的设备,可选
.setDeviceMac(mac) // 只扫描指定mac的设备,可选
.setAutoConnect(isAutoConnect) // 连接时的autoConnect参数,可选,默认false
.setScanTimeOut(10000) // 扫描超时时间,可选,默认10秒
.build();
BleManager.getInstance().initScanRule(scanRuleConfig);
然后开始搜索:
BleManager.getInstance().scan(new BleScanCallback() {
@Override
//开始搜索
public void onScanStarted(boolean success) {
mDeviceAdapter.clearScanDevice();
mDeviceAdapter.notifyDataSetChanged();
img_loading.startAnimation(operatingAnim);
img_loading.setVisibility(View.VISIBLE);
btn_scan.setText(getString(R.string.stop_scan));
}
//每搜到一条的回调
@Override
public void onLeScan(BleDevice bleDevice) {
super.onLeScan(bleDevice);
}
//正在搜索
@Override
public void onScanning(BleDevice bleDevice) {
mDeviceAdapter.addDevice(bleDevice);
mDeviceAdapter.notifyDataSetChanged();
}
//搜索结束返回集合中所有的搜索
@Override
public void onScanFinished(List<BleDevice> scanResultList) {
img_loading.clearAnimation();
img_loading.setVisibility(View.INVISIBLE);
btn_scan.setText(getString(R.string.start_scan));
}
});
内部实现如下:
public void scan(BleScanCallback callback) {
if (callback == null) {
throw new IllegalArgumentException("BleScanCallback can not be Null!");
}
/***
* 开始调用方法时,肯定回调蓝牙搜索开始的回调
*/
if (!isBlueEnable()) {
BleLog.e("Bluetooth not enable!");
callback.onScanStarted(false);
return;
}
UUID[] serviceUuids = bleScanRuleConfig.getServiceUuids();
String[] deviceNames = bleScanRuleConfig.getDeviceNames();
String deviceMac = bleScanRuleConfig.getDeviceMac();
boolean fuzzy = bleScanRuleConfig.isFuzzy();
long timeOut = bleScanRuleConfig.getScanTimeOut();
//拿到当前的配置开始真正的扫描
BleScanner.getInstance().scan(serviceUuids, deviceNames, deviceMac, fuzzy, timeOut, callback);
}
真正的扫描在BleScanner中实现,如下:
private synchronized void startLeScan(UUID[] serviceUuids, String[] names, String mac, boolean fuzzy,
boolean needConnect, long timeOut, BleScanPresenterImp imp) {
//搜索的状态类
if (mBleScanState != BleScanState.STATE_IDLE) {
BleLog.w("scan action already exists, complete the previous scan action first");
if (imp != null) {
imp.onScanStarted(false);
}
return;
}
mBleScanPresenter.prepare(names, mac, fuzzy, needConnect, timeOut, imp);
boolean success = BleManager.getInstance().getBluetoothAdapter()
.startLeScan(serviceUuids, mBleScanPresenter);
mBleScanState = success ? BleScanState.STATE_SCANNING : BleScanState.STATE_IDLE;
mBleScanPresenter.notifyScanStarted(success);
}
注意上面的synchronized ,保证线程安全性,其中BleScanPresenter主要用来做超时计时的关闭搜索和扫描途中设备搜索上了的回调,内部采用了HanderThread来实现回调方法的实现减少耗时,如下所示初始化
public void prepare(String[] names, String mac, boolean fuzzy, boolean needConnect,
long timeOut, BleScanPresenterImp bleScanPresenterImp) {
mDeviceNames = names;
mDeviceMac = mac;
mFuzzy = fuzzy;
mNeedConnect = needConnect;
mScanTimeout = timeOut;
mBleScanPresenterImp = bleScanPresenterImp;
mHandlerThread = new HandlerThread(BleScanPresenter.class.getSimpleName());
mHandlerThread.start();
mHandler = new ScanHandler(mHandlerThread.getLooper(), this);
mHandling = true;
}
然后通过BluetoothAdapter.startScan()真正实现回调,系统回调方法都在BleScanPresenter实现,然后最终回调给自己封装的方法
private BleScanPresenter mBleScanPresenter = new BleScanPresenter() {
@Override
public void onScanStarted(boolean success) {
BleScanPresenterImp callback = mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onScanStarted(success);
}
}
@Override
public void onLeScan(BleDevice bleDevice) {
if (mBleScanPresenter.ismNeedConnect()) {
BleScanAndConnectCallback callback = (BleScanAndConnectCallback)
mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onLeScan(bleDevice);
}
} else {
BleScanCallback callback = (BleScanCallback) mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onLeScan(bleDevice);
}
}
}
@Override
public void onScanning(BleDevice result) {
BleScanPresenterImp callback = mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onScanning(result);
}
}
@Override
public void onScanFinished(List<BleDevice> bleDeviceList) {
if (mBleScanPresenter.ismNeedConnect()) {
final BleScanAndConnectCallback callback = (BleScanAndConnectCallback)
mBleScanPresenter.getBleScanPresenterImp();
if (bleDeviceList == null || bleDeviceList.size() < 1) {
if (callback != null) {
callback.onScanFinished(null);
}
} else {
if (callback != null) {
callback.onScanFinished(bleDeviceList.get(0));
}
final List<BleDevice> list = bleDeviceList;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
BleManager.getInstance().connect(list.get(0), callback);
}
}, 100);
}
} else {
BleScanCallback callback = (BleScanCallback) mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onScanFinished(bleDeviceList);
}
}
}
开始扫描成功后就延迟超时时间关闭扫描,实现如下:
public final void notifyScanStarted(final boolean success) {
mBleDeviceList.clear();
removeHandlerMsg();
if (success && mScanTimeout > 0) {
mMainHandler.postDelayed(new Runnable() {
@Override
public void run() {
BleScanner.getInstance().stopLeScan();
}
}, mScanTimeout);
}
mMainHandler.post(new Runnable() {
@Override
public void run() {
onScanStarted(success);
}
});
}
扫描结束后接下来就是连接蓝牙的操作,如下代码:
BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
@Override
public void onStartConnect() {
progressDialog.show();
}
//连接失败那么是不是需要重新连接呢
@Override
public void onConnectFail(BleDevice bleDevice, BleException exception) {
img_loading.clearAnimation();
img_loading.setVisibility(View.INVISIBLE);
btn_scan.setText(getString(R.string.start_scan));
progressDialog.dismiss();
Toast.makeText(MainActivity.this, getString(R.string.connect_fail), Toast.LENGTH_LONG).show();
}
连接成功之后就可以做读写操作了
@Override
public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
progressDialog.dismiss();
mDeviceAdapter.addDevice(bleDevice);
mDeviceAdapter.notifyDataSetChanged();
}
//收到蓝牙断开的通知,需不需要重新连接在这里写,框架做了主动关闭gatt
@Override
public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {
progressDialog.dismiss();
mDeviceAdapter.removeDevice(bleDevice);
mDeviceAdapter.notifyDataSetChanged();
if (isActiveDisConnected) {
Toast.makeText(MainActivity.this, getString(R.string.active_disconnected), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, getString(R.string.disconnected), Toast.LENGTH_LONG).show();
ObserverManager.getInstance().notifyObserver(bleDevice);
}
}
});
这里的BleDevice相当于一个包装类,包装了BluetoothDevice,接着向下走
public BluetoothGatt connect(BleDevice bleDevice, BleGattCallback bleGattCallback) {
if (bleGattCallback == null) {
throw new IllegalArgumentException("BleGattCallback can not be Null!");
}
if (!isBlueEnable()) {
BleLog.e("Bluetooth not enable!");
bleGattCallback.onConnectFail(bleDevice, new OtherException("Bluetooth not enable!"));
return null;
}
if (Looper.myLooper() == null || Looper.myLooper() != Looper.getMainLooper()) {
BleLog.w("Be careful: currentThread is not MainThread!");
}
if (bleDevice == null || bleDevice.getDevice() == null) {
bleGattCallback.onConnectFail(bleDevice, new OtherException("Not Found Device Exception Occurred!"));
} else {
BleBluetooth bleBluetooth = multipleBluetoothController.buildConnectingBle(bleDevice);
boolean autoConnect = bleScanRuleConfig.isAutoConnect();
return bleBluetooth.connect(bleDevice, autoConnect, bleGattCallback);
}
return null;
}
可以看出框架封装的连接必须在ui线程里执行,这里就避免了一个坑位,真正走到原生api的连接如下:
ublic synchronized BluetoothGatt connect(BleDevice bleDevice,
boolean autoConnect,
BleGattCallback callback,
int connectRetryCount) {
BleLog.i("connect device: " + bleDevice.getName()
+ "\nmac: " + bleDevice.getMac()
+ "\nautoConnect: " + autoConnect
+ "\ncurrentThread: " + Thread.currentThread().getId()
+ "\nconnectCount:" + (connectRetryCount + 1));
if (connectRetryCount == 0) {
this.connectRetryCount = 0;
}
addConnectGattCallback(callback);
lastState = LastState.CONNECT_CONNECTING;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
bluetoothGatt = bleDevice.getDevice().connectGatt(BleManager.getInstance().getContext(),
autoConnect, coreGattCallback, TRANSPORT_LE);
} else {
bluetoothGatt = bleDevice.getDevice().connectGatt(BleManager.getInstance().getContext(),
autoConnect, coreGattCallback);
}
if (bluetoothGatt != null) {
if (bleGattCallback != null) {
bleGattCallback.onStartConnect();
}
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_OVER_TIME;
mainHandler.sendMessageDelayed(message, BleManager.getInstance().getConnectOverTime());
} else {
disconnectGatt();
refreshDeviceCache();
closeBluetoothGatt();
lastState = LastState.CONNECT_FAILURE;
BleManager.getInstance().getMultipleBluetoothController().removeConnectingBle(BleBluetooth.this);
if (bleGattCallback != null)
bleGattCallback.onConnectFail(bleDevice, new OtherException("GATT connect exception occurred!"));
}
return bluetoothGatt;
}
这里如果连接失败进行了disconnectGatt(); refreshDeviceCache(); closeBluetoothGatt();操作,这里避免了蓝牙缓存出现问题或者连接次数缓存达到上限没有销毁而进行的清除缓存的优化,其实就是这面的这些api操作
bluetoothGatt.disconnect();
private synchronized void refreshDeviceCache() {
try {
final Method refresh = BluetoothGatt.class.getMethod("refresh");
if (refresh != null && bluetoothGatt != null) {
boolean success = (Boolean) refresh.invoke(bluetoothGatt);
BleLog.i("refreshDeviceCache, is success: " + success);
}
} catch (Exception e) {
BleLog.i("exception occur while refreshing device: " + e.getMessage());
e.printStackTrace();
}
}
bluetoothGatt.close();
每一个BleBluetooth就是封装的一条连接,然后就是最重要的连接上蓝牙的时候系统的回调方法
coreGattCallBack实现如下:
private BluetoothGattCallback coreGattCallback = new BluetoothGattCallback() {
//蓝牙连接改变的回调方法
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
BleLog.i("BluetoothGattCallback:onConnectionStateChange "
+ '\n' + "status: " + status
+ '\n' + "newState: " + newState
+ '\n' + "currentThread: " + Thread.currentThread().getId());
bluetoothGatt = gatt;
mainHandler.removeMessages(BleMsg.MSG_CONNECT_OVER_TIME);
if (newState == BluetoothProfile.STATE_CONNECTED) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_DISCOVER_SERVICES;
mainHandler.sendMessageDelayed(message, 500);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
if (lastState == LastState.CONNECT_CONNECTING) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_FAIL;
message.obj = new BleConnectStateParameter(status);
mainHandler.sendMessage(message);
} else if (lastState == LastState.CONNECT_CONNECTED) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_DISCONNECTED;
BleConnectStateParameter para = new BleConnectStateParameter(status);
para.setActive(isActiveDisconnect);
message.obj = para;
mainHandler.sendMessage(message);
}
}
}
//发现设备服务的回调方法
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
BleLog.i("BluetoothGattCallback:onServicesDiscovered "
+ '\n' + "status: " + status
+ '\n' + "currentThread: " + Thread.currentThread().getId());
bluetoothGatt = gatt;
if (status == BluetoothGatt.GATT_SUCCESS) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_DISCOVER_SUCCESS;
message.obj = new BleConnectStateParameter(status);
mainHandler.sendMessage(message);
} else {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_DISCOVER_FAIL;
mainHandler.sendMessage(message);
}
}
//读硬件返回信息的数据的回调方法
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Iterator iterator = bleNotifyCallbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object callback = entry.getValue();
if (callback instanceof BleNotifyCallback) {
BleNotifyCallback bleNotifyCallback = (BleNotifyCallback) callback;
if (characteristic.getUuid().toString().equalsIgnoreCase(bleNotifyCallback.getKey())) {
Handler handler = bleNotifyCallback.getHandler();
if (handler != null) {
Message message = handler.obtainMessage();
message.what = BleMsg.MSG_CHA_NOTIFY_DATA_CHANGE;
message.obj = bleNotifyCallback;
Bundle bundle = new Bundle();
bundle.putByteArray(BleMsg.KEY_NOTIFY_BUNDLE_VALUE, characteristic.getValue());
message.setData(bundle);
handler.sendMessage(message);
}
}
}
}
iterator = bleIndicateCallbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object callback = entry.getValue();
if (callback instanceof BleIndicateCallback) {
BleIndicateCallback bleIndicateCallback = (BleIndicateCallback) callback;
if (characteristic.getUuid().toString().equalsIgnoreCase(bleIndicateCallback.getKey())) {
Handler handler = bleIndicateCallback.getHandler();
if (handler != null) {
Message message = handler.obtainMessage();
message.what = BleMsg.MSG_CHA_INDICATE_DATA_CHANGE;
message.obj = bleIndicateCallback;
Bundle bundle = new Bundle();
bundle.putByteArray(BleMsg.KEY_INDICATE_BUNDLE_VALUE, characteristic.getValue());
message.setData(bundle);
handler.sendMessage(message);
}
}
}
}
}
//写入成功后的回调方法
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Iterator iterator = bleNotifyCallbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object callback = entry.getValue();
if (callback instanceof BleNotifyCallback) {
BleNotifyCallback bleNotifyCallback = (BleNotifyCallback) callback;
if (descriptor.getCharacteristic().getUuid().toString().equalsIgnoreCase(bleNotifyCallback.getKey())) {
Handler handler = bleNotifyCallback.getHandler();
if (handler != null) {
Message message = handler.obtainMessage();
message.what = BleMsg.MSG_CHA_NOTIFY_RESULT;
message.obj = bleNotifyCallback;
Bundle bundle = new Bundle();
bundle.putInt(BleMsg.KEY_NOTIFY_BUNDLE_STATUS, status);
message.setData(bundle);
handler.sendMessage(message);
}
}
}
}
iterator = bleIndicateCallbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object callback = entry.getValue();
if (callback instanceof BleIndicateCallback) {
BleIndicateCallback bleIndicateCallback = (BleIndicateCallback) callback;
if (descriptor.getCharacteristic().getUuid().toString().equalsIgnoreCase(bleIndicateCallback.getKey())) {
Handler handler = bleIndicateCallback.getHandler();
if (handler != null) {
Message message = handler.obtainMessage();
message.what = BleMsg.MSG_CHA_INDICATE_RESULT;
message.obj = bleIndicateCallback;
Bundle bundle = new Bundle();
bundle.putInt(BleMsg.KEY_INDICATE_BUNDLE_STATUS, status);
message.setData(bundle);
handler.sendMessage(message);
}
}
}
}
}
//写入成功的回调方法
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Iterator iterator = bleWriteCallbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object callback = entry.getValue();
if (callback instanceof BleWriteCallback) {
BleWriteCallback bleWriteCallback = (BleWriteCallback) callback;
if (characteristic.getUuid().toString().equalsIgnoreCase(bleWriteCallback.getKey())) {
Handler handler = bleWriteCallback.getHandler();
if (handler != null) {
Message message = handler.obtainMessage();
message.what = BleMsg.MSG_CHA_WRITE_RESULT;
message.obj = bleWriteCallback;
Bundle bundle = new Bundle();
bundle.putInt(BleMsg.KEY_WRITE_BUNDLE_STATUS, status);
bundle.putByteArray(BleMsg.KEY_WRITE_BUNDLE_VALUE, characteristic.getValue());
message.setData(bundle);
handler.sendMessage(message);
}
}
}
}
}
//读取成功的回调方法
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Iterator iterator = bleReadCallbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object callback = entry.getValue();
if (callback instanceof BleReadCallback) {
BleReadCallback bleReadCallback = (BleReadCallback) callback;
if (characteristic.getUuid().toString().equalsIgnoreCase(bleReadCallback.getKey())) {
Handler handler = bleReadCallback.getHandler();
if (handler != null) {
Message message = handler.obtainMessage();
message.what = BleMsg.MSG_CHA_READ_RESULT;
message.obj = bleReadCallback;
Bundle bundle = new Bundle();
bundle.putInt(BleMsg.KEY_READ_BUNDLE_STATUS, status);
bundle.putByteArray(BleMsg.KEY_READ_BUNDLE_VALUE, characteristic.getValue());
message.setData(bundle);
handler.sendMessage(message);
}
}
}
}
}
//读取信号强度成功的回调方法
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
if (bleRssiCallback != null) {
Handler handler = bleRssiCallback.getHandler();
if (handler != null) {
Message message = handler.obtainMessage();
message.what = BleMsg.MSG_READ_RSSI_RESULT;
message.obj = bleRssiCallback;
Bundle bundle = new Bundle();
bundle.putInt(BleMsg.KEY_READ_RSSI_BUNDLE_STATUS, status);
bundle.putInt(BleMsg.KEY_READ_RSSI_BUNDLE_VALUE, rssi);
message.setData(bundle);
handler.sendMessage(message);
}
}
}
//改变mtu传输数据上线的回调方法
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
if (bleMtuChangedCallback != null) {
Handler handler = bleMtuChangedCallback.getHandler();
if (handler != null) {
Message message = handler.obtainMessage();
message.what = BleMsg.MSG_SET_MTU_RESULT;
message.obj = bleMtuChangedCallback;
Bundle bundle = new Bundle();
bundle.putInt(BleMsg.KEY_SET_MTU_BUNDLE_STATUS, status);
bundle.putInt(BleMsg.KEY_SET_MTU_BUNDLE_VALUE, mtu);
message.setData(bundle);
handler.sendMessage(message);
}
}
}
};
连接看完,接下来看一下读和写,在看读写前首先需要了解蓝牙数据传输的一些知识,蓝牙的描述符分Notification与Indication。
GATT_Indication:从机通知主机后,主机需要调用simpleprofile_writeattrcb,读取从机的数据。也就是说整个过程需要主机响应回复,否则通讯失败!主机和从机通过Handle Value Confirmation 进行确认。
GATT_Notification:从机直接发送给主机。不需要主机回复,有可能数据丢失!
蓝牙连接之后需要找到相应的服务通道,然后在此通道上进行传值,这时需要确定是上面什么模式
框架实现方法如下:
BleManager.getInstance().indicate(
bleDevice,
characteristic.getService().getUuid().toString(),
characteristic.getUuid().toString(),
new BleIndicateCallback() {
@Override
public void onIndicateSuccess() {
runOnUiThread(new Runnable() {
@Override
public void run() {
addText(txt, "indicate success");
}
});
}
@Override
public void onIndicateFailure(final BleException exception) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addText(txt, exception.toString());
}
});
}
@Override
public void onCharacteristicChanged(byte[] data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addText(txt, HexUtil.formatHexString(characteristic.getValue(), true));
}
});
}
});
BleManager.getInstance().notify(
bleDevice,
characteristic.getService().getUuid().toString(),
characteristic.getUuid().toString(),
new BleNotifyCallback() {
@Override
public void onNotifySuccess() {
runOnUiThread(new Runnable() {
@Override
public void run() {
addText(txt, "notify success");
}
});
}
@Override
public void onNotifyFailure(final BleException exception) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addText(txt, exception.toString());
}
});
}
@Override
public void onCharacteristicChanged(byte[] data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addText(txt, HexUtil.formatHexString(characteristic.getValue(), true));
}
});
}
});
真正实现如下:
private boolean setCharacteristicNotification(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
boolean useCharacteristicDescriptor,
boolean enable,
BleNotifyCallback bleNotifyCallback) {
if (gatt == null || characteristic == null) {
notifyMsgInit();
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("gatt or characteristic equal null"));
return false;
}
boolean success1 = gatt.setCharacteristicNotification(characteristic, enable);
if (!success1) {
notifyMsgInit();
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("gatt setCharacteristicNotification fail"));
return false;
}
BluetoothGattDescriptor descriptor;
if (useCharacteristicDescriptor) {
descriptor = characteristic.getDescriptor(characteristic.getUuid());
} else {
descriptor = characteristic.getDescriptor(formUUID(UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR));
}
if (descriptor == null) {
notifyMsgInit();
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("descriptor equals null"));
return false;
} else {
descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
boolean success2 = gatt.writeDescriptor(descriptor);
if (!success2) {
notifyMsgInit();
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("gatt writeDescriptor fail"));
}
return success2;
}
}
其实最重要的就下面的几句代码:
boolean success1 = gatt.setCharacteristicNotification(characteristic, enable);
BluetoothGattDescriptor descriptor;
if (useCharacteristicDescriptor) {
descriptor = characteristic.getDescriptor(characteristic.getUuid());
} else {
descriptor = characteristic.getDescriptor(formUUID(UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR));
}
descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
boolean success2 = gatt.writeDescriptor(descriptor);
服务和特性值都是在onServicesDiscovered后过滤出来,
接下来看一下写操作:
BleManager.getInstance().write(
bleDevice,
characteristic.getService().getUuid().toString(),
characteristic.getUuid().toString(),
HexUtil.hexStringToBytes(hex),
new BleWriteCallback() {
@Override
public void onWriteSuccess(final int current, final int total, final byte[] justWrite) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addText(txt, "write success, current: " + current
+ " total: " + total
+ " justWrite: " + HexUtil.formatHexString(justWrite, true));
}
});
}
@Override
public void onWriteFailure(final BleException exception) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addText(txt, exception.toString());
}
});
}
});
}
});
真正实现如下:
public void write(BleDevice bleDevice,
String uuid_service,
String uuid_write,
byte[] data,
boolean split,
boolean sendNextWhenLastSuccess,
long intervalBetweenTwoPackage,
BleWriteCallback callback) {
if (callback == null) {
throw new IllegalArgumentException("BleWriteCallback can not be Null!");
}
if (data == null) {
BleLog.e("data is Null!");
callback.onWriteFailure(new OtherException("data is Null!"));
return;
}
if (data.length > 20 && !split) {
BleLog.w("Be careful: data's length beyond 20! Ensure MTU higher than 23, or use spilt write!");
}
BleBluetooth bleBluetooth = multipleBluetoothController.getBleBluetooth(bleDevice);
if (bleBluetooth == null) {
callback.onWriteFailure(new OtherException("This device not connect!"));
} else {
if (split && data.length > getSplitWriteNum()) {
new SplitWriter().splitWrite(bleBluetooth, uuid_service, uuid_write, data,
sendNextWhenLastSuccess, intervalBetweenTwoPackage, callback);
} else {
bleBluetooth.newBleConnector()
.withUUIDString(uuid_service, uuid_write)
.writeCharacteristic(data, callback, uuid_write);
}
}
}
写入的二进制如果超过20个字节就会被切分写入,来看一下切分的实现:
private void splitWrite() {
if (mData == null) {
throw new IllegalArgumentException("data is Null!");
}
if (mCount < 1) {
throw new IllegalArgumentException("split count should higher than 0!");
}
//将数据切分放在队列里面
mDataQueue = splitByte(mData, mCount);
mTotalNum = mDataQueue.size();
write();
}
//每次写入成功接下来发送下一条指令达到串联
private void write() {
if (mDataQueue.peek() == null) {
release();
return;
}
byte[] data = mDataQueue.poll();
mBleBluetooth.newBleConnector()
.withUUIDString(mUuid_service, mUuid_write)
.writeCharacteristic(
data,
new BleWriteCallback() {
@Override
public void onWriteSuccess(int current, int total, byte[] justWrite) {
int position = mTotalNum - mDataQueue.size();
if (mCallback != null) {
mCallback.onWriteSuccess(position, mTotalNum, justWrite);
}
if (mSendNextWhenLastSuccess) {
Message message = mHandler.obtainMessage(BleMsg.MSG_SPLIT_WRITE_NEXT);
mHandler.sendMessageDelayed(message, mIntervalBetweenTwoPackage);
}
}
@Override
public void onWriteFailure(BleException exception) {
if (mCallback != null) {
mCallback.onWriteFailure(new OtherException("exception occur while writing: " + exception.getDescription()));
}
if (mSendNextWhenLastSuccess) {
Message message = mHandler.obtainMessage(BleMsg.MSG_SPLIT_WRITE_NEXT);
mHandler.sendMessageDelayed(message, mIntervalBetweenTwoPackage);
}
}
},
mUuid_write);
if (!mSendNextWhenLastSuccess) {
Message message = mHandler.obtainMessage(BleMsg.MSG_SPLIT_WRITE_NEXT);
mHandler.sendMessageDelayed(message, mIntervalBetweenTwoPackage);
}
}
代码先将超出部分切分,然后放在队列里面,依次写入成功再进行下一次的写人,达到了串行的执行,为啥是串行,源码奉上:
public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
&& (characteristic.getProperties() &
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;
BluetoothGattService service = characteristic.getService();
if (service == null) return false;
BluetoothDevice device = service.getDevice();
if (device == null) return false;
synchronized(mDeviceBusy) {
if (mDeviceBusy) return false;
mDeviceBusy = true;
}
try {
mService.writeCharacteristic(mClientIf, device.getAddress(),
characteristic.getInstanceId(), characteristic.getWriteType(),
AUTHENTICATION_NONE, characteristic.getValue());
} catch (RemoteException e) {
Log.e(TAG,"",e);
mDeviceBusy = false;
return false;
}
return true;
}
这里有一个mDeviceBusy 的变量,它如果等于true,则不写入,它只要在写入成功或者失败才会被置为false,用户才能重新写入或读取如下:
public void onCharacteristicWrite(String address, int status, int handle) {
if (VDBG) Log.d(TAG, "onCharacteristicWrite() - Device=" + address
+ " handle=" + handle + " Status=" + status);
if (!address.equals(mDevice.getAddress())) {
return;
}
//关键点
synchronized(mDeviceBusy) {
mDeviceBusy = false;
}
BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle);
if (characteristic == null) return;
if ((status == GATT_INSUFFICIENT_AUTHENTICATION
|| status == GATT_INSUFFICIENT_ENCRYPTION)
&& (mAuthRetryState != AUTH_RETRY_STATE_MITM)) {
try {
final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ?
AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM;
mService.writeCharacteristic(mClientIf, address, handle,
characteristic.getWriteType(), authReq, characteristic.getValue());
mAuthRetryState++;
return;
} catch (RemoteException e) {
Log.e(TAG,"",e);
}
}
mAuthRetryState = AUTH_RETRY_STATE_IDLE;
runOrQueueCallback(new Runnable() {
@Override
public void run() {
if (mCallback != null) {
mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic,
status);
}
}
});
}
读的原理和写类似,感兴趣的小伙伴可以仔细翻阅一下,今天就先写到这里了。
上一篇: DCT C语言 修改了c程序以及matlab的程序
下一篇: SLAM精度评测工具EVO使用