代码拉取完成,页面将自动刷新
FastBle是一个处理蓝牙BLE设备的库,可以对蓝牙BLE设备进行过滤,扫描,连接,读取,写入等。
ohpm install @ohos/fastble
OpenHarmony ohpm环境配置等更多内容,请参考 如何安装OpenHarmony har包 。
BleManager.getInstance().init();
BleManager.getInstance()
.enableLog(true)
.setReConnectCount(1, 5000)
.setConnectOverTime(20000)
.setOperateTimeout(5000);
let scanRuleConfig: BleScanRuleConfig = new BleScanRuleConfig.Builder()
.setServiceUuids(serviceUuids) // 只扫描指定的服务的设备,可选
.setDeviceName(true, names) // 只扫描指定广播名的设备,可选
.setDeviceMac(mac) // 只扫描指定mac的设备,可选
.setAutoConnect(isAutoConnect) // 连接时的autoConnect参数,可选,默认false
.setScanTimeOut(10000) // 扫描超时时间,可选,默认10秒
.build();
BleManager.getInstance().initScanRule(scanRuleConfig);
let _this = this;
class TempBleScanCallback extends BleScanCallback {
onScanStarted(success: boolean): void {
console.info("onScanStarted success:" + success);
_this.clearScanDevice();
_this.is_loading = true;
_this.loading_rotate = 360;
_this.btn_scan_text = $r('app.string.stop_scan');
_this.connectedDevices = BleManager.getInstance().getAllConnectedDevice();
}
onLeScan(bleDevice: BleDevice): void {
console.info("onLeScan");
}
onScanning(bleDevice: BleDevice): void {
console.info("onScanning");
ArrayHelper.add(_this.bleDeviceList, bleDevice);
}
onScanFinished(scanResultList: Array<BleDevice>): void {
console.info("onScanFinished");
_this.is_loading = false;
_this.loading_rotate = 0;
_this.btn_scan_text = $r('app.string.start_scan');
_this.connectedDevices = BleManager.getInstance().getAllConnectedDevice();
}
}
BleManager.getInstance().scan(new TempBleScanCallback());
let _this = this;
class IndexBleGattCallback extends BleGattCallback {
public onStartConnect(): void {
_this.progressDialogCtrl.open();
}
public onConnectFail(bleDevice: BleDevice, exception: BleException): void {
_this.progressDialogCtrl.close();
prompt.showToast({ message: _this.toast_connect_fail, duration: 300, })
}
public onConnectSuccess(bleDevice: BleDevice, gatt: ble.GattClientDevice, status: number): void {
_this.progressDialogCtrl.close();
ArrayHelper.add(_this.connectedDevices, bleDevice.getMac());
}
public onDisConnected(isActiveDisConnected: boolean, device: BleDevice, gatt: ble.GattClientDevice, status: number): void {
_this.progressDialogCtrl.close();
_this.connectedDevices = BleManager.getInstance().getAllConnectedDevice();
if (isActiveDisConnected) {
prompt.showToast({ message: _this.toast_active_disconnected, duration: 300 })
} else {
prompt.showToast({ message: _this.toast_disconnected, duration: 300 })
}
}
}
BleManager.getInstance().connect(bleDevice, new IndexBleGattCallback());
BleManager.getInstance().cancelScan();
let bleReadClass: BleReadCallback = new BleReadClass()
BleManager.getInstance().read(
this.device,
this.characteristic.serviceUuid,
this.characteristic.characteristicUuid,
bleReadClass);
let tempBleWriteCallback: BleWriteCallback = new TempBleWriteCallback()
BleManager.getInstance().write(
this.device,
this.characteristic.serviceUuid,
this.characteristic.characteristicUuid,
HexUtil.hexStringToBytes(hex),
true,
true,
0,
tempBleWriteCallback);
public static getInstance(): BleManager;
public initScanRule(config: BleScanRuleConfig)
public scan(callback: BleScanCallback)
public cancelScan()
public connect(device: BleDevice | string, bleGattCallback: BleGattCallback)
public isConnected(device: BleDevice | string): boolean
public disconnect(bleDevice: BleDevice)
public read(bleDevice: BleDevice, uuid_service: string, uuid_read: string, callback: BleReadCallback)
public write(bleDevice: BleDevice, uuid_service: string, uuid_write: string, data: Uint8Array, split: boolean=true, sendNextWhenLastSuccess: boolean=true, intervalBetweenTwoPackage: number=0, callback: BleWriteCallback)
DevEco Studio 版本: 4.1 Canary(4.1.3.317)
OpenHarmony SDK:API11 (4.1.0.36)
|---- FastBle
| |---- entry # 示例代码文件夹
| |---- wxFastBle # fastble库文件夹
| |----src
|----main
|----ets
|----bluetooth 核心逻辑
|----callback #回调处理
|----exception #异常事件处理
|----scan #蓝牙扫描实现
|----data #数据处理实现
|----utils #工具类
|----BleManager.ets #蓝牙连接管理
| |---- index.ets # 对外接口
| |---- README.md # 安装使用方法
使用过程中发现任何问题都可以提 Issue 给我们,当然,我们也非常欢迎你给我们发 PR 。
本项目基于 Apache LICENSE ,请自由地享受和参与开源。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。