可能是因为某些设备默认开启了 BLE 外围自定安全机制,需要进行特殊处理。需要在连接设备前,根据设备的 MAC 地址设置 BLE 模块的配对方式。具体的示例代码如下:
import { BleManager } from "react-native-ble-plx"; import { Platform } from "react-native"; import BleManagerX from "react-native-ble-manager-x";
const bleManager = Platform.OS === "ios" ? new BleManager() : BleManagerX;
...
connectToDevice(device) { const { id, name } = device;
// Only for iOS
if (Platform.OS === "ios") {
bleManager.requestLEScan([], 5, true);
} else {
// For Android, set the proper bonding type before connecting
bleManager.setBlePeripheralBondingType(id, BleManagerX.BLE_PERIPHERAL_BONDING_NONE);
}
const devicePromise = bleManager.connectToDevice(id);
...
}