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功能与原组件基本无差异
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.
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Комментарии ( 0 )