情况是onPressIn
我需要向 ble 设备发送写入命令,直到onPressOut
被触发。
这是我的writeMessage
功能
const writeMesage = async (message = 'c1') => {
// bleManager.cancelTransaction(transactionId);
const senddata = base64.encode(message);
if (deviceId) {
const isConnected = await bleManager.isDeviceConnected(deviceId);
if (!isConnected) {
await bleManager
.connectToDevice(deviceId, { autoConnect: true, requestMTU: 200 })
.then((device) => {
(async () => {
const services =
await device.discoverAllServicesAndCharacteristics();
const characteristic = await getServicesAndCharacteristics(
services
);
})();
return device.discoverAllServicesAndCharacteristics();
})
.then((device) => {
console.log('then2', { device: device });
// return this.setupNotifications(device)
return true;
})
.then(
() => {
console.log('Listening...');
},
(error) => {
console.log('Connection error' + JSON.stringify(error));
}
);
await bleManager.stopDeviceScan();
} else {
await bleManager
.writeCharacteristicWithoutResponseForDevice(
deviceId,
serviceUUID,
characteristicsUUID,
senddata
)
.then((characteristics) => {
console.log({ characteristics });
})
.catch((e) => {
console.log({ e });
});
}
return;
} else {
console.log('No device is connected');
}
};
我正在实施它:
<TouchableOpacity
style={{
display: 'flex',
height: 50,
width: 50,
borderRadius: 100,
borderColor: '#58C2A2',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#1A8E6B'
}}
disabled={isLocked}
onPressIn={() => {
let interval = setInterval(() => {
writeMesage('c1');
}, 20);
setPressUpInterval(interval);
}}
onPressOut={() => clearInterval(pressUpInterval)}
>
<Image source={upArrow} />
</TouchableOpacity>
它一直工作到发现写入失败,然后设备断开连接。
LOG error in monitering [BleError: Device D4:36:39:6F:FF:41 was disconnected]