Javascript 中断数据流并断开设备
基本上,我正在创建一个通过蓝牙与两台设备通信的应用程序。开始时,我连接到第一个设备,然后连接到第二个设备。此时,我开始从两个设备恢复数据(一个数字流)。当用户单击“停止”时,我必须停止此数据读取并断开连接的设备。我怎样才能按照你的要求来实施呢 我使用react原生ble plx库 第一个问题是关于函数“stopConnection(device)” 我应该传递设备的值:Javascript 中断数据流并断开设备,javascript,android,react-native,Javascript,Android,React Native,基本上,我正在创建一个通过蓝牙与两台设备通信的应用程序。开始时,我连接到第一个设备,然后连接到第二个设备。此时,我开始从两个设备恢复数据(一个数字流)。当用户单击“停止”时,我必须停止此数据读取并断开连接的设备。我怎样才能按照你的要求来实施呢 我使用react原生ble plx库 第一个问题是关于函数“stopConnection(device)” 我应该传递设备的值:this.model(this.props.device)。 在我收到错误的那一刻。 无法读取未定义的属性“cancelConn
this.model(this.props.device)
。
在我收到错误的那一刻。
无法读取未定义的属性“cancelConnection”
第二个问题是数据流是连续的,所以当我点击Stop按钮时,大约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>
构造函数(道具){
超级(道具);
this.manager=新的BleManager();
此.state={
acc:[],
gyr:[],
mg:[],
};
scanDx(){
this.manager.startDeviceScan(null,null,(错误,设备)=>{
如果(错误){
返回;
}
if(device.name==this.model_dx(this.props.ModelDevice)){
这是.manager.stopDeviceScan();
装置
.connect()
.然后(()=>{
console.log(“ConnectedDX”);
控制台日志(“”);
这是scanSx();
})
.然后(()=>{
此.deviceService_Dx(设备);
})
.catch(()=>{
警惕。警惕(“警惕”);
Actions.homepage();
});
}
});
}
停止连接(设备){
设备。取消连接()
返回新承诺((解决、拒绝)=>{
解析(console.log(“断开”)
})
}
render(){
设备=this.model\u dx(this.props.deviceModel)
恒压=this.state.values.hasOwnProperty(this.pressure)
?this.state.values[this.Pressure].join(“”)
: "-";
返回(
第一个装置
{“Time:+this.state.Time}
{“Acc:+this.state.Acc.join(“”)}
{“Gyr:+this.state.Gyr.join(“”)}
{“Mg:+this.state.Mg.join(“”)}
{“压力:+压力}
{“Msg:+this.state.info}
第二个设备:
{“Time:+this.state.Time}
{“Acc:+this.state.Acc_dx.join(“”)}
{“Gyr:+this.state.Gyr_dx.join(“”)}
{“Mg:+this.state.Mg_dx.join(“”)}
{“压力:+压力}
{“Msg:+this.state.info}
此.stopConnection(设备)}>
停止
在react native ble plx
模块中有一个停止扫描的功能
取消设备连接(设备标识符:设备ID):承诺
用法
const manager=new BleManager();
manager.cancelDeviceConnection(设备ID)
或
如果您有通过'new Divice()'创建的设备
新设备(nativeDevice:nativeDevice,manager:BleManager)
取消连接():承诺
停止连接(设备){
设备。取消连接()
。然后((设备)=>{
//在具有服务和特性的设备上工作
})
.catch((错误)=>{
//处理错误
});
}
...
此.stopConnection(设备)}>
停止
在react native ble plx
模块中有一个停止扫描的功能
取消设备连接(设备标识符:设备ID):承诺
用法
const manager=new BleManager();
manager.cancelDeviceConnection(设备ID)
或
如果您有通过'new Divice()'创建的设备
新设备(nativeDevice:nativeDevice,manager:BleManager)
取消连接():承诺
停止连接(设备){
设备。取消连接()
。然后((设备)=>{
//在具有服务和特性的设备上工作
})
.catch((错误)=>{
//处理错误
});
}
...
此.stopConnection(设备)}>
停止
您正在将未定义(超出范围)的值传递给stopConnection(设备)函数
当设备连接时
您可以通过执行以下操作来保留device1和device2的引用
此.device1
和
此.device2
或将其放入数组中
停止时,您可以使用
this.device1.cancelConnection()
.then((device)=>{
})
.catch(() => {
});
或
您正在将未定义(超出范围)的值传递给stopConnection(设备)函数
当设备连接时
您可以通过执行以下操作来保留device1和device2的引用
此.device1
和
此.device2
或将其放入数组中
停止时,您可以使用
this.device1.cancelConnection()
.then((device)=>{
})
.catch(() => {
});
或
@Jack23如果我的回答有帮助,请添加“V”符号好吗?我收到错误时出现问题:“device.cancelConnection不是一个函数”。而且,我也不能从按钮执行操作时起将延迟减少大约10秒。你有设备创建者吗?如果没有,请创建一个构造函数或使用一个管理器。我根据你的建议编辑我的第一个代码!我使用一个构造函数,但不理解你指的是什么你没有创建设备。相反,已经创建了一个管理器。因此,d使用管理器作为我的答案断开连接。@Jack23如果我的答案有帮助,请添加“V”符号好吗?我收到错误时出现问题:“device.cancelConnection不是函数”。而且,我也不能从按钮执行操作时起将延迟减少大约10秒。你有设备创建者吗?如果没有,请创建一个构造函数或使用一个管理器。我根据你的建议编辑我的第一个代码!我使用一个构造函数,但不理解你指的是什么你没有创建设备。相反,已经创建了一个管理器。因此,d使用经理作为我的答案断开连接。我收到er时遇到问题
import {BleManager} from 'react-native-ble-plx';
let connectedDevice = null;
export default class BlePlx {
constructor() {
this.manager = new BleManager();
this.scanAndConnect()
}
onDeviceDisconnected = (device) => {
connectedDevice = null;
this.manager && this.manager.destroy();
this.manager = null;
};
disConnectDevice() {
if (connectedDevice) connectedDevice.cancelConnection().then((device) => this.onDeviceDisconnected(device)).catch(() => {
});
}
scanAndConnect() {
this.manager.startDeviceScan(null, {scanMode: 2}, (error, device) => {
if (error) {
// Handle error (scanning will be stopped automatically)
return
}
// Check if it is a device you are looking for based on advertisement data
// or other criteria.;
device.connect()
.then((device) => {
this.device = device;
connectedDevice = device;
return device.discoverAllServicesAndCharacteristics()
})
.then(this.onConnected)
.catch((error) => {
});
// Stop scanning as it's not necessary if you are scanning for one device.
//this.manager.stopDeviceScan();
// Proceed with connection.
});
}
onConnected = (device) => {
// Do work on device with services and characteristics
this.manager.servicesForDevice(device.id).then(this.getServices);
this.manager.onDeviceDisconnected(device.id, (error, device) => {
console.log('DisConnected ' + new Date().toISOString());
this.onDeviceDisconnected(device)
})
};
}