xiaoyaoyou1212 / ble Goto Github PK
View Code? Open in Web Editor NEW✨Android BLE基础操作框架,基于回调,操作简单。包含扫描、多连接、广播包解析、服务读写及通知等功能。
Home Page: http://www.huwei.tech/
License: Apache License 2.0
✨Android BLE基础操作框架,基于回调,操作简单。包含扫描、多连接、广播包解析、服务读写及通知等功能。
Home Page: http://www.huwei.tech/
License: Apache License 2.0
支持多设备管理吗
03-15 10:24:39.052 9652-9652/com.vise.ble E/AndroidRuntime: FATAL EXCEPTION: main Process: com.vise.ble, PID: 9652 java.lang.ClassCastException: android.bluetooth.BluetoothGattDescriptor cannot be cast to android.bluetooth.BluetoothGattCharacteristic at com.vise.ble.DeviceControlActivity$7$1.a(DeviceControlActivity.java:331) at com.vise.baseble.a$4$8.run(ViseBluetooth.java:273) at android.os.Handler.handleCallback(Handler.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 03-15 10:28:26.203 2523-3930/? E/InputDispatcher: channel 'fc825a0 com.vise.ble/com.vise.ble.DeviceScanActivity (server)' ~ Channel is unrecoverably broken and will be disposed! 03-15 10:28:26.203 2523-3930/? E/InputDispatcher: channel '4f8f691 com.vise.ble/com.vise.ble.DeviceDetailActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
当连接过程中,多次连接失败,会导致app卡死,以及报线程过多
public BluetoothLeScanner getBluetoothLeScanner() { if (!getLeAccess()) return null; synchronized(mLock) { if (sBluetoothLeScanner == null) { sBluetoothLeScanner = new BluetoothLeScanner(mManagerService); } } return sBluetoothLeScanner; }
框架中很多地方调用了getBluetoothLeScanner() , 但是这个方法有可能返回null, 会导致很多NPE.
这几种情况会出现:
在onCharacteristicRead函数中,
for (IBleCallback bleCallback : bleCallbacks) {
if (bleCallback instanceof ICharacteristicCallback) {
if (status == BluetoothGatt.GATT_SUCCESS) {
((ICharacteristicCallback) bleCallback).onSuccess(characteristic);
} else {
bleCallback.onFailure(new GattException(status));
}
}
}
removeBleCallback(tempBleCallback);
问题1:为什么要遍历bleCallbacks,只要是ICharacteristicCallback,都用onSuccess或onFailure处理一次呢?如果我对不同的characteristic进行了读写操作,或者对同一个characteristic进行了读和写操作,那么在bleCallbacks中就会有多个ICharacteristicCallback,用每个回调都去处理读回的characterisitc,岂不是混乱了?
问题2:每次对Characteristic操作时,对Descriptor操作时,包括读Rssi时,都调用了listenAndTimer(),其中用tempBleCallback保留最新的操作回调。在onCharacteristicRead(),为什么要把最新的tempBleCallback回调remove呢?
每次修复的bug也让我们知道一下,我们也好修改代码
初始化之后,调用以下代码查找指定服务UUID的设备,始终查不到(但是按照设备名称可以查到):
ViseBle.getInstance().startLeScan(new UuidFilterScanCallback(new IScanCallback() {
@OverRide
public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {
List<BluetoothLeDevice> bleList = bluetoothLeDeviceStore.getDeviceList();
for(BluetoothLeDevice ble:bleList){
System.out.println("onDeviceFound > "+ ble.getName() + " uuid = "+ ble.getDevice().getUuids());
}
}
@Override
public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {
System.out.println("onScanFinish ");
}
@Override
public void onScanTimeout() {
System.out.println("onScanTimeout ");
}
}).setUuid("0000fe55-0000-1000-8000-00805f9b34fb"));
我看了源码是这样过滤的:
public BluetoothLeDeviceStore onFilter(BluetoothLeDevice bluetoothLeDevice) {
if (bluetoothLeDevice != null && bluetoothLeDevice.getDevice() != null
&& bluetoothLeDevice.getDevice().getUuids() != null
&& bluetoothLeDevice.getDevice().getUuids().length > 0) {
for (ParcelUuid parcelUuid : bluetoothLeDevice.getDevice().getUuids()) {
if (uuid != null && uuid == parcelUuid.getUuid()) {
bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
}
}
}
return bluetoothLeDeviceStore;
}
但是bluetoothLeDevice.getDevice().getUuids()返回一直是null,貌似在服务连接前该值一直是null!
当连接过程中,多次连接失败,会导致app卡死,以及报线程过多
直接调用MAC 地址 的connect 方法,一直连不上。。。Timeout Exception Occurred
时间设置的20秒
/**
* 蓝牙传输 16进制 高低位 读数的 转换
* @param bb 从蓝牙拿到的 16进制的 byte数值
* @param index 从哪一位开始截取,因为蓝牙一条有好几段数据
* @param cont 截取的长度 (1不用截,是偶数位) 其实就是 1 2 4 8 。如果是1 直接用buffer 或转String 截取
* @param big 如果高位在前 big 给一个true,如果地位在前,big给false
* @return 因为考虑到 有点数据比较长,所以用 long类型,int也可以用long
* 如果是要 String 的 类型 直接用 new String(byte[] by)就可以完成
*/
public static long getLong(byte[] bb, int index, int cont , boolean big) {
long lg = 0;
//TODO 高位在前 用这个转换
if (big) { // 高位在前 用这个这个 转换
switch (cont) {
case 2:
lg = ((((long) bb[index + 0] & 0xff) << 8)
| (((long) bb[index + 1] & 0xff) << 0));
break;
case 4:
lg = ((((long) bb[index + 0] & 0xff) << 24)
| (((long) bb[index + 1] & 0xff) << 16)
| (((long) bb[index + 2] & 0xff) << 8)
| (((long) bb[index + 3] & 0xff) << 0));
break;
case 8:
lg = ((((long) bb[index + 0] & 0xff) << 56)
| (((long) bb[index + 1] & 0xff) << 48)
| (((long) bb[index + 2] & 0xff) << 40)
| (((long) bb[index + 3] & 0xff) << 32)
| (((long) bb[index + 4] & 0xff) << 24)
| (((long) bb[index + 5] & 0xff) << 16)
| (((long) bb[index + 6] & 0xff) << 8)
| (((long) bb[index + 7] & 0xff) << 0));
break;
}
return lg;
} else {
// TODO 地位在前用这个。 (我们的 嵌入式给的都是地位在前)
switch (cont) {
case 2:
lg = ((((long) bb[index + 1] & 0xff) << 8)
| (((long) bb[index + 0] & 0xff) << 0));
break;
case 4:
lg = ((((long) bb[index + 3] & 0xff) << 24)
| (((long) bb[index + 2] & 0xff) << 16)
| (((long) bb[index + 1] & 0xff) << 8)
| (((long) bb[index + 0] & 0xff) << 0));
break;
case 8:
lg = ((((long) bb[index + 7] & 0xff) << 56)
| (((long) bb[index + 6] & 0xff) << 48)
| (((long) bb[index + 5] & 0xff) << 40)
| (((long) bb[index + 4] & 0xff) << 32)
| (((long) bb[index + 3] & 0xff) << 24)
| (((long) bb[index + 2] & 0xff) << 16)
| (((long) bb[index + 1] & 0xff) << 8)
| (((long) bb[index + 0] & 0xff) << 0));
break;
}
return lg;
}
调用stopScan没用,还是一直在扫描
虽然isConnected返回false,但数据通信正常。
之前用ViseBluetooth.getInstance().connect不会出现此种现象。
等待时间在10秒以上即可复现。
从手机发送数据开始,到设备接到数据后马上响应,时间间隔为60~100ms,蓝牙的数据传输速率应该不会这么慢啊,有没有什么好的解决方案?
测试手机:三星S7+
1.扫描出现卡顿,从progressBar可以看出
2.没多久就崩溃
api 23 以上 需要动态申请权限, 否则就无法显示连接的蓝牙设备
如何对写入指令后的蓝牙设备反馈数据进行监听和获取?
bindChannel(BluetoothLeDevice bluetoothLeDevice, PropertyType propertyType, UUID serviceUUID,
UUID characteristicUUID, UUID descriptorUUID)
这个方法只能拿到具体的一个 characteristicUUID 的值。
而如果需要那某一个 serviceUUID下的所有 的 characteristicUUID 的值,就不会写了。期待群主能加上。谢谢。另外上次给您的高低位的转换方法(已经得到我的验证)请加到工具类中。谢谢。
测试手机是乐视2,Android版本 6.0。设置好超时时间之后,超时回调并不会执行,而是一直扫描下去。同时我注意到扫描和连接的方法都是2个版本的,是不是需要个人在使用时判断系统版本来调整呢?
demo案例 为什么小米手机蓝牙扫描不到小米手机(2s/5c)
private IBleCallback bleWriteCallback = new IBleCallback() {
@OverRide
public void onSuccess(Object o, int type) {
if (o == null) {
return;
}
if (o instanceof BluetoothGattCharacteristic) {
if (((BluetoothGattCharacteristic) o).getValue() == null) {
return;
}
BleLog.i("Send onSuccess!");
Toast.makeText(mContext, "写成功", Toast.LENGTH_SHORT).show();
List descriptors = ((BluetoothGattCharacteristic) o).getDescriptors();
for (BluetoothGattDescriptor descriptor : descriptors) {
Log.d("notifysuccessws", HexUtil.encodeHexStr(descriptor.getValue()));
}
Log.d("notifysuccessw", HexUtil.encodeHexStr(((BluetoothGattCharacteristic) o).getValue()));
}
}
@OverRide
public void onFailure(BleException exception) {
if (exception == null) {
return;
}
Toast.makeText(mContext, "写失败", Toast.LENGTH_SHORT).show();
BleLog.i("notify fail:" + exception.getDescription());
}
};
写失败的时候 BleLog.i("notify fail:" + exception.getDescription()); 打印出来是
public class InitiatedException extends BleException {
public InitiatedException() {
super(BleExceptionCode.INITIATED_ERR, "Initiated Exception Occurred! ");
}
} 中的 Initiated Exception Occurred!
这个代表的是谁初始化错误了
只能搜索到一个周边没这个蓝牙,开了好几个手机还有硬件设备,都搜索不到。
连接上后选择可写服务, 能正常写入, 串口有数据打印.
但反过来, 串口发数据, 选择可通知服务, 并没有走接收消息的回调.
showGattServices()方法中
ViseBluetooth.getInstance().enableCharacteristicNotification(characteristic, bleCallback, true);
已经走到了.
但是串口发消息的时候ViseBluetooth中onCharacteristicChanged()
没有收到消息, 是什么原因导致的呢?
麻烦你了.
最开始的实现方式是循环发送:
for (int i = 0; i < paramStr.length(); i += 20) {
// 预加 最大包长度,如果依然小于总数据长度,可以取最大包数据大小
if ((i + 20) < paramStr.length()) {
String rangeStr = paramStr.substring(i, i + 20);
Log.d(BLETAG, rangeStr);
Write(mWriteCharacteristic, rangeStr);
} else {
String rangeStr = paramStr.substring(i, paramStr.length());
Log.d(BLETAG, rangeStr);
Write(mWriteCharacteristic, rangeStr);
}
}
第二种是这样:
private void nextRequest(BluetoothGattCharacteristic mCharacteristic) {
if (mOperationInProgress)
return;
// Get the first request from the init queue
String request = orderItemQueue != null ? orderItemQueue.poll() : null;
if (request == null) {
return;
}
mOperationInProgress = true;
boolean result = false;
result = Write(mCharacteristic, request);
// The result may be false if given characteristic or descriptor were not found on the device.
// In that case, proceed with next operation and ignore the one that failed.
mOperationInProgress = false;
nextRequest(mCharacteristic);
}
private void Write2(BluetoothGattCharacteristic mCharacteristic, List<String> paraStrs) {
orderItemQueue.clear();
orderItemQueue.addAll(paraStrs);
nextRequest(mCharacteristic);
}
第一次没用队列那样发送成功路就很高120多个字节 丢包率很小,
第二次硬件测试表示,丢包率很高
这是写入的时候捕获设备是否初始化的那个方法
handleAfterInitialed(getBluetoothGatt().writeCharacteristic(characteristic), bleCallback);
发送失败的时候(丢包的时候)就会返回这个 bleCallback.onFailure(new InitiatedException(),"");异常
大神我该怎么分包,按道理是不是我发送了就应该没问题了
如果发送跟接收的数据长度大于20。这个有做处理吗?
你好,我想运行你得示例,当我导入androidstuido的时候报错了
错误如下:Error:Could not get unknown property 'VERSION_NAME' for project ':baseble' of type org.gradle.api.Project.
请问下,这个是我配置问题,还是示例本身问题呢,望回复,谢谢!
Error:(207, 48) 错误: 找不到符号
符号: 方法 getRootPath(ChatActivity)
位置: 类 StorageUtils
定位权限希望可以适配下国产的一些手机,不过大部分手机没问题的
你代码中获取到的是配对状态,所以我建议加个获取连接状态方法,还有下面这个获取所有已连接的设备
bluetoothManager.getConnectedDevices(BluetoothProfile.GATT_SERVER);
我把代码写到 我的模块中
调用 ViseBluetooth.getInstance().writeCharacteristic,
@OverRide
public void onFailure(BleException exception) {
exception.getDescription();
}
BleException :Initiated Exception Occurred!
这个该如何解决??
我现在有两个蓝牙设备要和我的程序连接,都是调用用的 ViseBluetooth.getInstance().connectByName(bleName, false, connectCallback); 一个可以连接,另一个不可以
不能连接的拿个没有任何回调,很奇怪
可以指定uuid[]的service扫描方法,指定特定的devicename和MAC地址也可以传入list或者数组表示过滤多个名字或者mac的。。
假如绑定了多个写通道,能不能指定向其中一个通道写数据,而不是向所有通道写数据
有沒有任何自動連接到特定設備的選項?
通知的回调里面,得到数据后,TextView.setText() 显示数据,然后吧数据传到上一个页面。再打开该页面,在通知回调里面TextView.setText() 就不起作用了,返回的值不能显示到TextView上,搞了好久,不知道什么原因? @xiaoyaoyou1212
有个蓝牙设备在android4.3上可以连接成功,但是在7.0上就是不可以连接上,不知道为什么?求助
附上log如下:
04:14.944 14746-14746/com.vise.ble I/BluetoothDevice: connectGatt
03-23 18:04:14.944 14746-14746/com.vise.ble D/BluetoothGatt: connect() - device: 98:7B:F3:5F:C9:CF, auto: false
03-23 18:04:14.944 14746-14746/com.vise.ble D/BluetoothGatt: registerApp()
03-23 18:04:14.944 14746-14746/com.vise.ble D/BluetoothGatt: registerApp() - UUID=0b4c17ec-57ce-453d-8e9e-ddce49e5e287
03-23 18:04:14.951 14746-14816/com.vise.ble D/BluetoothGatt: onClientRegistered() - status=0 clientIf=11
03-23 18:04:14.952 14746-14746/com.vise.ble I/HwPointEventFilter: do not support AFT because of no config
03-23 18:04:14.964 14746-14746/com.vise.ble D/android.widget.GridLayout: horizontal constraints: x2-x0>=1312, x2-x1<=427, x1-x0<=369 are inconsistent; permanently removing: x2-x1<=427.
03-23 18:04:14.964 14746-14746/com.vise.ble D/android.widget.GridLayout: horizontal constraints: x2-x0>=1312, x2-x1<=32, x1-x0<=239 are inconsistent; permanently removing: x2-x1<=32.
03-23 18:04:14.984 14746-15107/com.vise.ble D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
03-23 18:04:15.038 14746-15107/com.vise.ble D/OpenGLRenderer: endAllActiveAnimators on 0x79167f6c00 (RippleDrawable) with handle 0x79167db080
03-23 18:04:15.273 14746-15078/com.vise.ble D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=11 device=98:7B:F3:5F:C9:CF
03-23 18:04:15.273 14746-15078/com.vise.ble I/Bluetooth:(ViseBluetooth.java:116).onConnectionStateChange: onConnectionStateChange status: 0 ,newState: 2 ,thread: 10815
03-23 18:04:15.274 14746-15078/com.vise.ble D/BluetoothGatt: discoverServices() - device: 98:7B:F3:5F:C9:CF
03-23 18:04:24.967 14746-14746/com.vise.ble D/BluetoothGatt: close()
03-23 18:04:24.968 14746-14746/com.vise.ble D/BluetoothGatt: unregisterApp() - mClientIf=0
03-23 18:04:24.969 14746-14746/com.vise.ble I/Bluetooth:(DeviceControlActivity.java:68).onConnectFailure: Connect Failure!
03-23 18:04:24.972 14746-14746/com.vise.ble D/HwRTBlurUtils: check blur style for HwToast-Toast, themeResId : 0x7f09008a, context : com.vise.ble.DeviceControlActivity@39a21c9, Nhwext : 5, get Blur : disable
03-23 18:04:25.049 14746-14746/com.vise.ble I/HwPointEventFilter: do not support AFT because of no config
03-23 18:04:25.079 14746-15107/com.vise.ble D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
03-23 18:04:35.789 14746-14759/com.vise.ble D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=11 device=98:7B:F3:5F:C9:CF
03-23 18:04:35.790 14746-14759/com.vise.ble I/Bluetooth:(ViseBluetooth.java:116).onConnectionStateChange: onConnectionStateChange status: 0 ,newState: 0 ,thread: 10811
03-23 18:04:35.790 14746-14759/com.vise.ble D/BluetoothGatt: close()
03-23 18:04:35.790 14746-14759/com.vise.ble D/BluetoothGatt: unregisterApp() - mClientIf=0
03-23 18:04:35.814 14746-14746/com.vise.ble I/Bluetooth:(DeviceControlActivity.java:77).onDisconnect: Disconnect!
D/BluetoothAdapter: startLeScan(): null
D/BluetoothAdapter: STATE_ON
D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 mClientIf=0
D/ActivityThreadInjector: clearCachedDrawables.
D/OpenGLRenderer: endAllActiveAnimators on 0x7f59a18000 (RippleDrawable) with handle 0x7f5404aa80
E/cr_PlatformServi-Google: UsageReporting query failed
D:\lianxi\BLE-master\baseble\src\main\java\com\vise\baseble\core\DeviceMirror.java
Error:(25, 20) 错误: 程序包com.vise.log不存在
Error:(105, 13) 错误: 找不到符号
符号: 变量 ViseLog
Error:(136, 13) 错误: 找不到符号
符号: 变量 ViseLog
Error:(141, 17) 错误: 找不到符号
符号: 变量 ViseLog
Error:(162, 13) 错误: 找不到符号
符号: 变量 ViseLog
应该是类库自动判断调用那个api才对
works on my Redmi note 3(android 6.0), but doesn't detect any devices on moto g5 plus(android 7.0), and lenovo (android 6.0)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.