rusel1989 / react-native-bluetooth-serial

Port of https://github.com/don/BluetoothSerial for react native
475 stars 291 forks source link

Activity triggering using react-native-bluetooth-serial #99

Open mabn123 opened 5 years ago

mabn123 commented 5 years ago

Is it possible to trigger a navigation to a different activity/screen after detecting a specified bluetooth id? and how? I have been trying to make it happen but weren't successful

ivankfit commented 5 years ago

this should give you a hint connect (device) { this.setState({ connecting: true }) BluetoothSerial.connect(device.id) .then((res) => { console.log(Connected to device ${device.name}) this.setState({ device, connected: true, connecting: false }) //////add this line to the connect function this.props.navigation.navigate('Second',{device:device}); }) .catch((err) => console.log(err.message)) }

mabn123 commented 5 years ago

Thank you very much for the hint, however, my case requires me to utilize the BluetoothSerial.discoverUnpairedDevices() to scan for available unpaired devices and when a determined bluetooth id say is scanned, the function would automatically trigger a different activity/screen

mabn123 commented 5 years ago

This is what I have come out so far but the activity never triggered despite the fact that the bluetooth signal exist: ` discoverUnpaired () {

const { unpairedDevices } = this.state ;

if (this.state.discovering) {
  return false
} 

else {
  this.setState({ discovering: true })
  BluetoothSerial.discoverUnpairedDevices()
  .then((unpairedDevices) => {
    this.setState({ unpairedDevices, discovering: false })
  })
  .catch((err) => Toast.showShortBottom(err.message))

    unpairedDevices.filter(function(device){
      return device.id == this.state.DEVICE_ID;
    }).map(function(device,i){
        if (device.id === this.state.DEVICE_ID){
          this.props.navigation.navigate('Second');
        }
    });

}

}

`

mabn123 commented 5 years ago

The 'this.state.DEVICE_ID' is where the target device's device id is kept