首页 > 解决方案 > 中断数据流并断开设备

问题描述

基本上我正在创建一个通过蓝牙与两个设备通信的应用程序。一开始我连接到第一个设备,然后连接到第二个设备。此时我开始从两个设备中恢复数据(一个数字流)。当用户单击“停止”时,我必须停止读取此数据并断开连接的设备。我该如何根据您的要求来实施呢?

我使用 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>     

标签: javascriptandroidreact-native

解决方案


模块中有一个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>  

推荐阅读