Android BLE基础框架全新改版,714页PDF的鸿蒙学习笔记

public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@Override
public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@Override
public void onScanTimeout() {

}
}).setDeviceMac(deviceMac));

  • 扫描指定设备名称的设备

//该方式是扫到指定设备就停止扫描
ViseBle.getInstance().startScan(new SingleFilterScanCallback(new IScanCallback() {
@Override
public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@Override
public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@Override
public void onScanTimeout() {

}
}).setDeviceName(deviceName));

  • 扫描指定 UUID 的设备

ViseBle.getInstance().startScan(new UuidFilterScanCallback(new IScanCallback() {
@Override
public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@Override
public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@Override
public void onScanTimeout() {

}
}).setUuid(uuid));

  • 扫描指定设备 MAC 或名称集合的设备

ViseBle.getInstance().startScan(new ListFilterScanCallback(new IScanCallback() {
@Override
public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@Override
public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@Override
public void onScanTimeout()

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

{

}
}).setDeviceMacList(deviceMacList).setDeviceNameList(deviceNameList));

  • 扫描指定信号范围或设备正则名称的设备

ViseBle.getInstance().startScan(new RegularFilterScanCallback(new IScanCallback() {
@Override
public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {

}

@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,如:开始扫描与停止扫描、连接与断开连接、清除资源等,该库提供的功能尽量简单易用,这也正是该项目的宗旨。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值