javascript - 中断数据流并断开设备
问题描述
基本上我正在创建一个通过蓝牙与两个设备通信的应用程序。一开始我连接到第一个设备,然后连接到第二个设备。此时我开始从两个设备中恢复数据(一个数字流)。当用户单击“停止”时,我必须停止读取此数据并断开连接的设备。我该如何根据您的要求来实施呢?
我使用 react-native-ble-plx 库
第一个问题是关于函数“stopConnection(设备)”我应该传递设备的值:this.model(this.props.device)
。目前我收到错误。
Cannot read property 'cancelConnection' of undefined
第二个问题是数据流是连续的,所以当我点击停止按钮大约 10 秒前通过停止(“尝试停止 xD”)。谢谢
constructor(props) {
super(props);
this.manager = new BleManager();
this.state = {
acc: [],
gyr: [],
mg: [],
};
scanDx() {
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
return;
}
if (device.name == this.model_dx(this.props.ModelDevice)) {
this.manager.stopDeviceScan();
device
.connect()
.then(() => {
console.log("ConnectedDX.");
console.log(" ");
this.scanSx();
})
.then(() => {
this.deviceService_Dx(device);
})
.catch(() => {
Alert.alert("Alert.");
Actions.homepage();
});
}
});
}
stopConnection (device) {
device.cancelConnection()
return new Promise((resolve, reject) => {
resolve(console.log("Disconnected."))
})
}
render() {
device = this.model_dx(this.props.deviceModel)
const pressure = this.state.values.hasOwnProperty(this.Pressure)
? this.state.values[this.Pressure].join(" ")
: "-";
return (
<View>
<Text>First Device </Text>
<Text>{"Time:" + this.state.time}</Text>
<Text>{"Acc:" + this.state.acc.join(" ")}</Text>
<Text>{"Gyr:" + this.state.gyr.join(" ")}</Text>
<Text>{"Mg:" + this.state.mg.join(" ")}</Text>
<Text>{"Pressure:" + pressure}</Text>
<Text>{"Msg:" + this.state.info}</Text>
<View>
<Text>Second Device: </Text>
<Text>{"Time:" + this.state.time}</Text>
<Text>{"Acc:" + this.state.acc_dx.join(" ")}</Text>
<Text>{"Gyr:" + this.state.gyr_dx.join(" ")}</Text>
<Text>{"Mg:" + this.state.mg_dx.join(" ")}</Text>
<Text>{"Pressure:" + pressure}</Text>
<Text>{"Msg:" + this.state.info}</Text>
</View>
<View>
<TouchableOpacity
style={[style.button, style.buttonOK]}
onPress={() => this.stopConnection(device)} >
<Text style={style.buttonTesto}>Stop</Text>
</TouchableOpacity> </View>
解决方案
模块中有一个react-native-ble-plx
停止扫描的功能。
cancelDeviceConnection(deviceIdentifier: DeviceId): 承诺
用法
const manager = new BleManager();
manager.cancelDeviceConnection(DeviceId)
或者
如果您有通过创建的设备'new Divice()'
新设备(nativeDevice:NativeDevice,经理:BleManager)
取消连接():承诺
stopConnection (device) {
device.cancelConnection()
.then((device) => {
// Do work on device with services and characteristics
})
.catch((error) => {
// Handle errors
});
}
...
<TouchableOpacity
style={[style.button, style.buttonOK]}
onPress={() => this.stopConnection(device)} >
<Text style={style.buttonTesto}>Stop</Text>
</TouchableOpacity> </View>
推荐阅读
- python - Python Subprocess.Run 没有运行 Inkscape pdf 到 svg
- node.js - ×REACT:我该如何解决:未处理的拒绝(TypeError):无法在“窗口”上执行“获取”:无效模式
- ios - 转换回登录视图控制器
- javascript - Acrobat Javascript - 复选框
- list - 我如何将目录排序到列表的顶部,而不必在 lua 中更改列表(现在按字母顺序排序)
- servicestack.redis - servicestack.redis getvalues 给出垃圾值
- botframework - MS Bot 框架 - 时间问题
- python-3.x - 如何找到节点的邻居?
- unicode - 如何在 jupyter 笔记本中使用原始 unicode 字符来制作四张卡片套装?
- haskell - 什么是棱镜?