鸿蒙开源组件——轻松访问Bluetooth LE设备的AdRecord和RSSI值

jacksky
发布于 2021-11-29 17:10
浏览
0收藏

Bluetooth-LE-Library---Ohos

项目介绍

  • 项目名称:Bluetooth-LE-Library---Ohos
  • 所属系列:openharmony的第三方组件适配移植
  • 功能:该库可轻松访问Bluetooth LE设备的AdRecord和RSSI值。它为iBeacons提供了其他功能。差异点因为openharmony目前暂不支持系统分享原因,通过intent分享功能没有实现。
  • 项目移植状态:主功能完成
  • 调用差异:无
  • 开发版本:sdk6,DevEco Studio 2.2 Beta1
  • 基线版本: Release v1.1.1

效果演示鸿蒙开源组件——轻松访问Bluetooth LE设备的AdRecord和RSSI值-鸿蒙开发者社区

安装教程

1.在项目根目录下的build.gradle文件中,

allprojects {
   repositories {
       maven {
           url 'https://s01.oss.sonatype.org/content/repositories/releases/'
       }
   }
}

 

2.在entry模块的build.gradle文件中,

dependencies {
   implementation('com.gitee.chinasoft_ohos:Bluetooth_LE_Library:1.0.0')
   ......  
}

使用说明

1、您将需要以下权限来访问蓝牙硬件:

  • ohos.permission.USE_BLUETOOTH
  • ohos.permission.LOCATION
  • ohos.permission.DISCOVER_BLUETOOTH

2、在MainAbilitySlice中获取蓝牙本机管理对象,并设置回调监听。

// 获取蓝牙本机管理对象
BluetoothHost bluetoothHost = BluetoothHost.getDefaultHost(this);
bluetoothHost.enableBt();
ScanCallback centralManagerCallback = new ScanCallback();
BleCentralManager centralManager = new BleCentralManager(this, centralManagerCallback);

3、开始扫描蓝牙设备,并在回调里面获取扫描到蓝牙数据

List<BleScanFilter> filters = new ArrayList<>();
centralManager.startScan(filters);

public class ScanCallback implements BleCentralManagerCallback {
        List<BleScanResult> results = new ArrayList<>();

        @Override
        public void scanResultEvent(BleScanResult resultCode) {
            // 对扫描结果进行处理
            results.add(resultCode);
            int rssi = resultCode.getRssi();
            byte[] scanRecord = resultCode.getRawData();
            BluetoothLeDevice deviceLe = new BluetoothLeDevice(resultCode,
                    rssi, scanRecord, System.currentTimeMillis());
            getUITaskDispatcher().asyncDispatch(() -> {
                for (int index = 0; index < listDevice.size(); index++) {
                    if (deviceLe.getAddress().equals(listDevice.get(index).getAddress())) {
                        BluetoothLeDevice item = listDevice.get(index);
                        item.updateRssiReading(System.currentTimeMillis(), rssi);
                        return;
                    }
                }
            });
        }

        @Override
        public void scanFailedEvent(int resultCode) {
        }

        @Override
        public void groupScanResultsEvent(List<BleScanResult> list) {
        }
    }

支持功能:

* getAddress() : Gets the MAC Address of the device
* getAdRecordStore(): Gives access to a device's Ad Records
* getDevice(): Gives access to the standard BluetoothDevice object
* getFirstRssi(): Retrieves the RSSI value which was used when the object was created
* getFirstTimestamp() : Retrieves the timestamp (in millis) which was used when the object was created
* getRssi() : Gets the current RSSI measurement (see note below).
* getScanRecord() : Retrieves the RAW scan record array
* getTimestamp() : Gets the timestamp of the last RSSI measurement
* getRunningAverageRssi() : Retrieves the internally calculated running average RSSI value (see note below).

测试信息

CodeCheck代码测试无异常

CloudTest代码测试无异常

病毒安全检测通过

当前版本demo功能与原组件基本无差异

版本迭代

  • 1.0.0

版权和许可信息

Author: [Alexandros Schillings](https://github.com/alt236).

The Accuracy calculation algorithm was taken from: http://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing
The AdRecord parser was taken from: https://github.com/devunwired/accessory-samples

All logos are the property of their respective owners.

The code in this project is licensed under the Apache Software License 2.0.

Copyright (c) 2014-2017 Alexandros Schillings.

 

 

Bluetooth-LE-Library---ohos-master.zip 587.23K 40次下载
已于2021-11-29 17:10:15修改
收藏
回复
举报
回复
    相关推荐