expo-ble模块测试demo 联调BLE模块 take 4 添加MTU设置和log

This commit is contained in:
Yudi Xiao 2025-12-11 12:23:26 +08:00
parent 1d3a7eba45
commit 1689fff721
3 changed files with 21 additions and 2 deletions

View File

@ -2,10 +2,26 @@
安装 bun https://bun.com/docs/installation#windows 安装 bun https://bun.com/docs/installation#windows
```bash ```powershell
powershell -c "irm bun.sh/install.ps1|iex" powershell -c "irm bun.sh/install.ps1|iex"
``` ```
检查bun是否正确安装
```powershell
& "$env:USERPROFILE\.bun\bin\bun" --version
```
添加bun到系统路径
```powershell
[System.Environment]::SetEnvironmentVariable(
"Path",
[System.Environment]::GetEnvironmentVariable("Path", "User") + ";$env:USERPROFILE\.bun\bin",
[System.EnvironmentVariableTarget]::User
)
```
安装项目依赖 安装项目依赖
```bash ```bash

View File

@ -1,6 +1,7 @@
import {BleManager, Device, Characteristic, BleError as PlxError, ScanOptions} from 'react-native-ble-plx'; import {BleManager, Device, Characteristic, BleError as PlxError, ScanOptions} from 'react-native-ble-plx';
import {Platform, PermissionsAndroid} from 'react-native'; import {Platform, PermissionsAndroid} from 'react-native';
import {BleDevice, ConnectionState, BleError, ScanResult} from './types'; import {BleDevice, ConnectionState, BleError, ScanResult} from './types';
import {BLE_UUIDS} from "@/ble";
export class BleClient { export class BleClient {
private static instance: BleClient; private static instance: BleClient;
@ -90,7 +91,8 @@ export class BleClient {
if (!this.manager) throw new Error('BLE not supported on web'); if (!this.manager) throw new Error('BLE not supported on web');
try { try {
this.emit('connectionStateChange', {deviceId, state: ConnectionState.CONNECTING}); this.emit('connectionStateChange', {deviceId, state: ConnectionState.CONNECTING});
const device = await this.manager.connectToDevice(deviceId); const device = await this.manager.connectToDevice(deviceId, {requestMTU: BLE_UUIDS.REQUEST_MTU});
console.log("Connected to device with MTU = ", device.mtu);
this.connectedDevice = await device.discoverAllServicesAndCharacteristics(); this.connectedDevice = await device.discoverAllServicesAndCharacteristics();
this.emit('connectionStateChange', {deviceId, state: ConnectionState.CONNECTED}); this.emit('connectionStateChange', {deviceId, state: ConnectionState.CONNECTED});

View File

@ -6,6 +6,7 @@ export const BLE_UUIDS = {
BROADCAST_CHARACTERISTIC: "000002c1-0000-1000-8000-00805f9b34fb", BROADCAST_CHARACTERISTIC: "000002c1-0000-1000-8000-00805f9b34fb",
WRITE_CHARACTERISTIC: '000002c5-0000-1000-8000-00805f9b34fb', WRITE_CHARACTERISTIC: '000002c5-0000-1000-8000-00805f9b34fb',
READ_CHARACTERISTIC: '000002c6-0000-1000-8000-00805f9b34fb', READ_CHARACTERISTIC: '000002c6-0000-1000-8000-00805f9b34fb',
REQUEST_MTU: 517,
} as const; } as const;
export const FRAME_CONSTANTS = { export const FRAME_CONSTANTS = {