1 Star 3 Fork 2

CHINASOFT3_OHOS / Bluetooth-LE-Library---Ohos

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

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

效果演示

安装教程

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设备的AdRecord和RSSI值 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/chinasoft3_ohos/Bluetooth-LE-Library---ohos.git
git@gitee.com:chinasoft3_ohos/Bluetooth-LE-Library---ohos.git
chinasoft3_ohos
Bluetooth-LE-Library---ohos
Bluetooth-LE-Library---Ohos
master

搜索帮助

14c37bed 8189591 565d56ea 8189591