Android(Java)开发之从BLE广播包中解析UUID

更新时间:2023-06-09 01:56:05 阅读: 评论:0

Android(Java)开发之从BLE⼴播包中解析UUID
本⽂旨在于解析出16位UUID,对于128位的UUID应该⽅法⼤同⼩异,没测试过的不贴出来
16bit UUID:
128位的UUID相当长,设备间为了识别数据的类型需要发送长达16字节的数据。为了提⾼传输效率,蓝⽛技术联盟(SIG)定义了⼀个称
为“UUID基数”的128位通⽤唯⼀识别码,结合⼀个较短的16位数使⽤。⼆者仍然遵循通⽤唯⼀识别码的分配规则,只不过在设备间传输常⽤的UUID时,只发送较短的16位版本,接收⽅收到后补上蓝⽛UUID基数即可。
会计电算化报名蓝⽛UUID基数如下:
00000000 – 0000 – 1000 – 8000 – 008059B34FB
如要发送的16位UUID为0x2A01,完整的128的UUID便是:
00002A01 – 0000 – 1000 – 8000 – 008059B34FB
如果需要其他位的UUID⾃⼰去试验,欢迎把实验结果告诉我,不扯太多,还是扯代码:
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
final ScanRecordUtil scanRecordUtil = ScanRecordUtil.parFromBytes(scanRecord);
Logger.e(TAG, "scanRecordUtil" + Uuids16S());
}
};
这是我实验的扫描函数redtube官网
接下来是ScanRecordUtil这个类,这个类主要⽤来处理⼴播包的数据
/**
* 我只把我能⽤的写了出来,没⽤的我没看懂
*/
public class ScanRecordUtil {
private static final String TAG = "---ScanRecordUtil";
private static final int DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE = 0x03;
private static final int DATA_TYPE_LOCAL_NAME_COMPLETE = 0x09;
// Flags of the advertising data.
private final int mAdvertiFlags;
// Transmission power level(in dB).
private final int mTxPowerLevel;
// Local name of the Bluetooth LE device.
private final String mDeviceName;
//Raw bytes of scan record.
private final byte[] mBytes;
private List<String> mUuids16S;            //16位UUID,之前有其他的UUID占着名称我就取的这名称
/**
* Returns the advertising flags indicating the discoverable mode and capability of the device. * Returns -1 if the flag field is not t.
*/
public int getAdvertiFlags() {
return mAdvertiFlags;
}
/**
* Returns the transmission power level of the packet in dBm. Returns {@link Integer#MIN_VALUE} * if the field is not t. This value can be ud to calculate the path loss of a received * packet using the following equation: * <p> * <code>pathloss = txPowerLevel - rssi</code>
*/
public int getTxPowerLevel() {
return mTxPowerLevel;
}
/**
* Returns the local name of the BLE device. The is a UTF-8 encoded string.
* 拿到设备的名称
*/
@Nullable
public String getDeviceName() {
return mDeviceName;
}
/**
* Returns raw bytes of scan record.
*/
public byte[] getBytes() {
return mBytes;
}
private ScanRecordUtil( List<String> mUuids16S,  int advertiFlags, int txPowerLevel, String localName, byte[] bytes) {
mDeviceName = localName;
mAdvertiFlags = advertiFlags;
mTxPowerLevel = txPowerLevel;
mBytes = bytes;
this.mUuids16S = mUuids16S;
}
/**
* 获取16位UUID
* @return
*/
public List<String> getUuids16S() {
return mUuids16S;
}
/**
* 得到ScanRecordUtil 对象,主要逻辑
* @param scanRecord
* @return
*/
public static ScanRecordUtil parFromBytes(byte[] scanRecord) {
if (scanRecord == null) {
return null;
}
int currentPos = 0;
int advertiFlag = -1;
List<String> uuids16 = new ArrayList<>();
String localName = null;
int txPowerLevel = Integer.MIN_VALUE;
try {
while (currentPos < scanRecord.length) {
// length is unsigned int.
int length = scanRecord[currentPos++] & 0xFF;
int length = scanRecord[currentPos++] & 0xFF;
if (length == 0) {
小学3年级英语上册break;
}
// / Note the length includes the length of the field type itlf.
int dataLength = length - 1;
// fieldType is unsigned int.
//                获取⼴播AD type
int fieldType = scanRecord[currentPos++] & 0xFF;英文翻译字典
switch (fieldType) {
foolproof
2011高考真题ca DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE:
parServiceUuid16(scanRecord, currentPos, dataLength, uuids16);
break;
ca DATA_TYPE_LOCAL_NAME_COMPLETE:
localName = new String(extractBytes(scanRecord, currentPos, dataLength));
break;
default:
break;
}
currentPos += dataLength;
}
if (uuids_16.isEmpty()){
uuids_16 = null;
}
return new ScanRecordUtil(uuids16,  advertiFlag, txPowerLevel, localName, scanRecord);
} catch (Exception e) {
Log.e(TAG, "unable to par scan record: " + String(scanRecord));
// As the record is invalid, ignore all the pard results for this packet
// and return an empty record with raw scanRecord bytes in results
return new ScanRecordUtil( null, -1, Integer.MIN_VALUE, null, scanRecord);
}
}
/
**
* byte数组转16进制董事长年会致辞
* @param bytes
* @return
*/
public static String bytesToHexFun3(byte[] bytes) {
StringBuilder buf = new StringBuilder(bytes.length * 2);
for (byte b : bytes) { // 使⽤String的format⽅法进⾏转换
buf.append(String.format("%02x", new Integer(b & 0xff)));英语语法知识
}
String();
}
// 16位UUID
private static int parServiceUuid16(byte[] scanRecord, int currentPos, int dataLength, List<String> rviceUuids) { while (dataLength > 0) {
byte[] uuidBytes = extractBytes(scanRecord, currentPos, dataLength);
final byte[] bytes = new byte[uuidBytes.length];
Logger.e(TAG, "dataLength==uuidBytes.length" + (dataLength == uuidBytes.length));
for (int i = 0; i < uuidBytes.length; i++) {
bytes[i] = uuidBytes[uuidBytes.length - 1 - i];
evan什么意思
}
kunerrviceUuids.add(bytesToHexFun3(bytes));
dataLength -= dataLength;
currentPos += dataLength;
}
return currentPos;
}
// Helper method to extract bytes from byte array.
//b帮助我们解析byte数组
//b帮助我们解析byte数组
private static byte[] extractBytes(byte[] scanRecord, int start, int length) {
byte[] bytes = new byte[length];
System.arraycopy(scanRecord, start, bytes, 0, length);
return bytes;
}
}
⽤这些代码就能得到16位的UUID,这是我的项⽬⾥⾯的东西,对于你们能不能⽤我就不清楚了,既然我⽤了⽹上的资源解决了⾃⼰的问题,我也要把我的知识贡献出来,开源才能使社会发展更快!

本文发布于:2023-06-09 01:56:05,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/907370.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   设备   实验   解析   开源   数组
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图