Giter Site home page Giter Site logo

xiaoyaoyou1212 / ble Goto Github PK

View Code? Open in Web Editor NEW
1.4K 63.0 361.0 2.85 MB

✨Android BLE基础操作框架,基于回调,操作简单。包含扫描、多连接、广播包解析、服务读写及通知等功能。

Home Page: http://www.huwei.tech/

License: Apache License 2.0

Java 100.00%
ble uuid characteristics

ble's Introduction

BLE

Codacy Badge License API

Android BLE基础操作框架,基于回调,操作简单。包含扫描、多连接、广播包解析、服务读写及通知等功能。

功能

  • 支持多设备连接管理;

  • 支持广播包解析;

  • 支持自定义扫描过滤条件;

  • 支持根据设备名称正则表达式过滤扫描设备;

  • 支持根据设备信号最小值过滤扫描设备;

  • 支持根据设备名称或 MAC 地址列表过滤扫描设备;

  • 支持根据设备 UUID 过滤扫描设备;

  • 支持根据指定设备名称或 MAC 地址搜索指定设备;

  • 支持连接设备失败重试;

  • 支持操作设备数据失败重试;

  • 支持绑定数据收发通道,同一种能力可绑定多个通道;

  • 支持注册和取消通知监听;

  • 支持配置最大连接数,超过最大连接数时会依据 Lru 算法自动断开最近最久未使用设备;

  • 支持配置扫描、连接和操作数据超时时间;

  • 支持配置连接和操作数据重试次数以及重试间隔时间。

简介

打造该库的目的是为了简化蓝牙设备接入的流程。该库是 BLE 操作的基础框架,只处理 BLE 设备通信逻辑,不包含具体的数据处理,如数据的分包与组包等。该库提供了多设备连接管理,可配置最大连接数量,并在超过最大连接数时会依据 Lru 算法自动断开最近最久未使用设备。该库还定制了常用的扫描设备过滤规则,也支持自定义过滤规则。该库所有操作都采用回调机制告知上层调用的结果,操作简单,接入方便。

版本说明

LatestVersion

最新版本更新记录

  • V2.0.6(2018-04-25)
    • 增加字节数组与整型数转换方法;
    • 增加间隔扫描的配置;
    • 增加获取服务、特征值、属性的方法。

安装包下载

BLE_V2.0.6.apk

代码托管

JCenter

常见问题

FAQ

效果展示

BLE效果

使用介绍

权限配置

蓝牙操作针对 6.0 以下系统需要配置如下权限:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

而 6.0 以上系统还需要增加模糊定位权限:

<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>

为了简便操作,库中对蓝牙操作需要的权限都做了相关设置不需要重复设置,但 6.0 以上系统需要动态申请模糊定位权限。

引入 SDK

在工程 module 的 build.gradle 文件中的 dependencies 中添加如下依赖:

compile 'com.vise.xiaoyaoyou:baseble:2.0.5'

构建完后就可以直接使用该库的功能了。

初始化

在使用该库前需要进行初始化,初始化代码如下所示:

//蓝牙相关配置修改
ViseBle.config()
        .setScanTimeout(-1)//扫描超时时间,这里设置为永久扫描
        .setConnectTimeout(10 * 1000)//连接超时时间
        .setOperateTimeout(5 * 1000)//设置数据操作超时时间
        .setConnectRetryCount(3)//设置连接失败重试次数
        .setConnectRetryInterval(1000)//设置连接失败重试间隔时间
        .setOperateRetryCount(3)//设置数据操作失败重试次数
        .setOperateRetryInterval(1000)//设置数据操作失败重试间隔时间
        .setMaxConnectCount(3);//设置最大连接设备数量
//蓝牙信息初始化,全局唯一,必须在应用初始化时调用
ViseBle.getInstance().init(this);

初始化可以是在 Application 中也可以是在 MainActivity 中,只需要是在使用蓝牙功能前就行。还有需要注意的是,蓝牙配置必须在蓝牙初始化前进行修改,如果默认配置满足要求也可以不修改配置。

设备扫描

库中针对设备扫描定义了几种常用过滤规则,如果不满足要求也可以自己定义过滤规则,下面针对库中提供的过滤规则使用方式一一介绍:

  • 扫描所有设备
ViseBle.getInstance().startScan(new ScanCallback(new IScanCallback() {
    @Override
    public void onDeviceFound(BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

    }

    @Override
    public void onScanTimeout() {

    }
}));
  • 扫描指定设备 MAC 的设备
//该方式是扫到指定设备就停止扫描
ViseBle.getInstance().startScan(new SingleFilterScanCallback(new IScanCallback() {
    @Override
    public void onDeviceFound(BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

    }

    @Override
    public void onScanTimeout() {

    }
}).setDeviceMac(deviceMac));
  • 扫描指定设备名称的设备
//该方式是扫到指定设备就停止扫描
ViseBle.getInstance().startScan(new SingleFilterScanCallback(new IScanCallback() {
    @Override
    public void onDeviceFound(BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

    }

    @Override
    public void onScanTimeout() {

    }
}).setDeviceName(deviceName));
  • 扫描指定 UUID 的设备
ViseBle.getInstance().startScan(new UuidFilterScanCallback(new IScanCallback() {
    @Override
    public void onDeviceFound(BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

    }

    @Override
    public void onScanTimeout() {

    }
}).setUuid(uuid));
  • 扫描指定设备 MAC 或名称集合的设备
ViseBle.getInstance().startScan(new ListFilterScanCallback(new IScanCallback() {
    @Override
    public void onDeviceFound(BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

    }

    @Override
    public void onScanTimeout() {

    }
}).setDeviceMacList(deviceMacList).setDeviceNameList(deviceNameList));
  • 扫描指定信号范围或设备正则名称的设备
ViseBle.getInstance().startScan(new RegularFilterScanCallback(new IScanCallback() {
    @Override
    public void onDeviceFound(BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

    }

    @Override
    public void onScanTimeout() {

    }
}).setDeviceRssi(rssi).setRegularDeviceName(regularDeviceName));

其中扫描到的设备列表由 BluetoothLeDeviceStore 管理,而单个设备信息都统一放到BluetoothLeDevice中,其中包含了设备的所有信息,如设备名称、设备地址、广播包解析信息等,设备的相关信息会在设备详情中进行介绍。

设备连接

设备连接有三种方式,一种是根据设备信息直接进行连接,另外两种是在没扫描的情况下直接通过设备名称或设备 MAC 进行扫描连接。三种连接方式使用如下:

  • 根据设备信息连接设备
ViseBle.getInstance().connect(bluetoothLeDevice, new IConnectCallback() {
    @Override
    public void onConnectSuccess(DeviceMirror deviceMirror) {

    }

    @Override
    public void onConnectFailure(BleException exception) {

    }

    @Override
    public void onDisconnect(boolean isActive) {

    }
});
  • 根据设备 MAC 直接扫描并连接
ViseBle.getInstance().connectByMac(deviceMac, new IConnectCallback() {
    @Override
    public void onConnectSuccess(DeviceMirror deviceMirror) {

    }

    @Override
    public void onConnectFailure(BleException exception) {

    }

    @Override
    public void onDisconnect(boolean isActive) {

    }
});
  • 根据设备名称直接扫描并连接
ViseBle.getInstance().connectByName(deviceName, new IConnectCallback() {
    @Override
    public void onConnectSuccess(DeviceMirror deviceMirror) {

    }

    @Override
    public void onConnectFailure(BleException exception) {

    }

    @Override
    public void onDisconnect(boolean isActive) {

    }
});

设备详情

DEVICE INFO(设备信息)

  • 获取设备名称(Device Name):bluetoothLeDevice.getName()
  • 获取设备地址(Device Address):bluetoothLeDevice.getAddress()
  • 获取设备类别(Device Class):bluetoothLeDevice.getBluetoothDeviceClassName()
  • 获取主要设备类别(Major Class):bluetoothLeDevice.getBluetoothDeviceMajorClassName()
  • 获取服务类别(Service Class):bluetoothLeDevice.getBluetoothDeviceKnownSupportedServices()
  • 获取配对状态(Bonding State):bluetoothLeDevice.getBluetoothDeviceBondState()

RSSI INFO(信号信息)

  • 获取第一次信号时间戳(First Timestamp):bluetoothLeDevice.getFirstTimestamp()
  • 获取第一次信号强度(First RSSI):bluetoothLeDevice.getFirstRssi()
  • 获取最后一次信号时间戳(Last Timestamp):bluetoothLeDevice.getTimestamp()
  • 获取最后一次信号强度(Last RSSI):bluetoothLeDevice.getRssi()
  • 获取平均信号强度(Running Average RSSI):bluetoothLeDevice.getRunningAverageRssi()

SCAN RECORD INFO(广播信息)

根据扫描到的广播包AdRecordStore获取某个广播数据单元AdRecord的类型编号record.getType(),再根据编号获取广播数据单元的类型描述record.getHumanReadableType()以及该广播数据单元的长度及数据内容,最后通过AdRecordUtil.getRecordDataAsString(record)将数据内容转换成具体字符串。更多关于广播包解析可以参考Android BLE学习笔记中数据解析部分。

发送数据

在发送数据前需要先绑定写入数据通道,绑定通道的同时需要设置写入数据的回调监听,具体代码示例如下:

BluetoothGattChannel bluetoothGattChannel = new BluetoothGattChannel.Builder()
        .setBluetoothGatt(deviceMirror.getBluetoothGatt())
        .setPropertyType(PropertyType.PROPERTY_WRITE)
        .setServiceUUID(serviceUUID)
        .setCharacteristicUUID(characteristicUUID)
        .setDescriptorUUID(descriptorUUID)
        .builder();
deviceMirror.bindChannel(new IBleCallback() {
    @Override
    public void onSuccess(byte[] data, BluetoothGattChannel bluetoothGattChannel, BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onFailure(BleException exception) {

    }
}, bluetoothGattChannel);
deviceMirror.writeData(data);

这里的 deviceMirror 在设备连接成功后就可以获取到,需要注意的是,服务一样的情况下写入数据的通道只需要注册一次,如果写入数据的通道有多个则可以绑定多个。写入数据必须要在绑定写入数据通道后进行,可以在不同的地方多次写入。

接收数据

与发送数据一样,接收设备发送的数据也需要绑定接收数据通道,这里有两种方式,一种是可通知方式、一种是指示器方式,使用方式如下:

  • 可通知方式
BluetoothGattChannel bluetoothGattChannel = new BluetoothGattChannel.Builder()
        .setBluetoothGatt(deviceMirror.getBluetoothGatt())
        .setPropertyType(PropertyType.PROPERTY_NOTIFY)
        .setServiceUUID(serviceUUID)
        .setCharacteristicUUID(characteristicUUID)
        .setDescriptorUUID(descriptorUUID)
        .builder();
deviceMirror.bindChannel(new IBleCallback() {
    @Override
    public void onSuccess(byte[] data, BluetoothGattChannel bluetoothGattChannel, BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onFailure(BleException exception) {

    }
}, bluetoothGattChannel);
deviceMirror.registerNotify(false);
  • 指示器方式
BluetoothGattChannel bluetoothGattChannel = new BluetoothGattChannel.Builder()
        .setBluetoothGatt(deviceMirror.getBluetoothGatt())
        .setPropertyType(PropertyType.PROPERTY_INDICATE)
        .setServiceUUID(serviceUUID)
        .setCharacteristicUUID(characteristicUUID)
        .setDescriptorUUID(descriptorUUID)
        .builder();
deviceMirror.bindChannel(new IBleCallback() {
    @Override
    public void onSuccess(byte[] data, BluetoothGattChannel bluetoothGattChannel, BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onFailure(BleException exception) {

    }
}, bluetoothGattChannel);
deviceMirror.registerNotify(true);

在绑定通道后需要注册通知,并需要在收到注册成功的回调时调用如下代码设置监听:

deviceMirror.setNotifyListener(bluetoothGattInfo.getGattInfoKey(), new IBleCallback() {
    @Override
    public void onSuccess(byte[] data, BluetoothGattChannel bluetoothGattChannel, BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onFailure(BleException exception) {

    }
});

所有设备发送过来的数据都会通过上面的监听得到,如果不想监听也可以取消注册,使用方式如下:

deviceMirror.unregisterNotify(isIndicate);

isIndicate 表示是否是指示器方式。

读取数据

由于读取设备信息基本每次的通道都不一样,所以这里与上面收发数据有点不一样,每次读取数据都需要绑定一次通道,使用示例如下:

BluetoothGattChannel bluetoothGattChannel = new BluetoothGattChannel.Builder()
        .setBluetoothGatt(deviceMirror.getBluetoothGatt())
        .setPropertyType(PropertyType.PROPERTY_READ)
        .setServiceUUID(serviceUUID)
        .setCharacteristicUUID(characteristicUUID)
        .setDescriptorUUID(descriptorUUID)
        .builder();
deviceMirror.bindChannel(new IBleCallback() {
    @Override
    public void onSuccess(byte[] data, BluetoothGattChannel bluetoothGattChannel, BluetoothLeDevice bluetoothLeDevice) {

    }

    @Override
    public void onFailure(BleException exception) {

    }
}, bluetoothGattChannel);
deviceMirror.readData();

总结

从以上的描述中可以知道,设备相关的所有操作都统一交给 ViseBle 进行处理,并且该类是单例模式,全局只有一个,管理很方便。使用该库提供的功能前必须要调用 ViseBle.getInstance().init(context); 进行初始化。每连接成功一款设备都会在设备镜像池中添加一款设备镜像,该设备镜像是维护设备连接成功后所有操作的核心类,在断开连接时会将该设备镜像从镜像池中移除,如果连接设备数量超过配置的最大连接数,那么设备镜像池会依据 Lru 算法自动移除最近最久未使用设备并断开连接。ViseBle 中封装了几个常用的 API,如:开始扫描与停止扫描、连接与断开连接、清除资源等,该库提供的功能尽量简单易用,这也正是该项目的宗旨。

感谢

在此要感谢两位作者提供的开源库android-lite-bluetoothLEBluetooth-LE-Library---Android,这两个开源库对于本项目的完成提供了很大的帮助。

关于我

Website

GitHub

CSDN

最后

如果觉得该项目有帮助,请点下Star,如果想支持作者的开源行动,请随意赞赏,赞赏通道如下:

微信支付

您的支持是我开源的动力。

如果有好的想法和建议,也欢迎Fork项目参与进来。使用中如果有任何问题和建议都可以进群交流,QQ群二维码如下:

QQ群 (此群已满)

QQ群

ble's People

Contributors

xiaoyaoyou1212 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ble's Issues

扫描崩溃

测试手机:三星S7+
1.扫描出现卡顿,从progressBar可以看出
2.没多久就崩溃

Demo中蓝牙连接上模块后无法接收数据

连接上后选择可写服务, 能正常写入, 串口有数据打印.
但反过来, 串口发数据, 选择可通知服务, 并没有走接收消息的回调.
showGattServices()方法中
ViseBluetooth.getInstance().enableCharacteristicNotification(characteristic, bleCallback, true);
已经走到了.
但是串口发消息的时候ViseBluetooth中onCharacteristicChanged()
没有收到消息, 是什么原因导致的呢?
麻烦你了.

扫描超时的回调不执行

  测试手机是乐视2,Android版本 6.0。设置好超时时间之后,超时回调并不会执行,而是一直扫描下去。同时我注意到扫描和连接的方法都是2个版本的,是不是需要个人在使用时判断系统版本来调整呢?   

线程过多

当连接过程中,多次连接失败,会导致app卡死,以及报线程过多

扫描过滤条件增加

可以指定uuid[]的service扫描方法,指定特定的devicename和MAC地址也可以传入list或者数组表示过滤多个名字或者mac的。。

有个蓝牙设备在android4.3上可以连接成功,但是在7.0上就是不可以连接上

有个蓝牙设备在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!

无法扫描指定 UUID 的设备!?

初始化之后,调用以下代码查找指定服务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!

疑问

如何对写入指令后的蓝牙设备反馈数据进行监听和获取?

写数据失败报Initiated Exception Occurred! 错

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!
这个代表的是谁初始化错误了

高低位转换方法

 /**
 *   蓝牙传输 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;

    }

大佬能否添加一个获取某一个serviceUUID下面的所有characteristicUUID的值

bindChannel(BluetoothLeDevice bluetoothLeDevice, PropertyType propertyType, UUID serviceUUID,
UUID characteristicUUID, UUID descriptorUUID)
这个方法只能拿到具体的一个 characteristicUUID 的值。
而如果需要那某一个 serviceUUID下的所有 的 characteristicUUID 的值,就不会写了。期待群主能加上。谢谢。另外上次给您的高低位的转换方法(已经得到我的验证)请加到工具类中。谢谢。

选择Unknown Service的时候会抛出异常.

image
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!

导入androidstudio报错

你好,我想运行你得示例,当我导入androidstuido的时候报错了
错误如下:Error:Could not get unknown property 'VERSION_NAME' for project ':baseble' of type org.gradle.api.Project.
请问下,这个是我配置问题,还是示例本身问题呢,望回复,谢谢!

getBluetoothLeScanner() 会返回空, 导致很多NPE

public BluetoothLeScanner getBluetoothLeScanner() { if (!getLeAccess()) return null; synchronized(mLock) { if (sBluetoothLeScanner == null) { sBluetoothLeScanner = new BluetoothLeScanner(mManagerService); } } return sBluetoothLeScanner; }

框架中很多地方调用了getBluetoothLeScanner() , 但是这个方法有可能返回null, 会导致很多NPE.
这几种情况会出现:

  • 蓝牙打开过程中bluetoothAdapter.getBluetoothLeScanner().startScan(leScanCallback);
  • 扫描过程中关掉了蓝牙bluetoothAdapter.getBluetoothLeScanner().stopScan(leScanCallback);

扫不出来,应该是手机问题和自己写了个BLE一样的问题

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

权限适配问题

定位权限希望可以适配下国产的一些手机,不过大部分手机没问题的

Initiated Exception Occurred!

我把代码写到 我的模块中
调用 ViseBluetooth.getInstance().writeCharacteristic,
@OverRide
public void onFailure(BleException exception) {
exception.getDescription();

        }

BleException :Initiated Exception Occurred!
这个该如何解决??

编译的时候报错,提示找不到log文件

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

BluetoothGattCallback::onCharacteristicRead函数处理问题

在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呢?

通知回调的问题?

通知的回调里面,得到数据后,TextView.setText() 显示数据,然后吧数据传到上一个页面。再打开该页面,在通知回调里面TextView.setText() 就不起作用了,返回的值不能显示到TextView上,搞了好久,不知道什么原因? @xiaoyaoyou1212

ChatActivity 51行

Error:(207, 48) 错误: 找不到符号
符号: 方法 getRootPath(ChatActivity)
位置: 类 StorageUtils

数据传输速度慢

从手机发送数据开始,到设备接到数据后马上响应,时间间隔为60~100ms,蓝牙的数据传输速率应该不会这么慢啊,有没有什么好的解决方案?

超过20自己的数据,怎么分包发送

最开始的实现方式是循环发送:
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(),"");异常
大神我该怎么分包,按道理是不是我发送了就应该没问题了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.