januslo / react-native-bluetooth-escpos-printer

React-Native plugin for the bluetooth ESC/POS & TSC printers.
MIT License
359 stars 416 forks source link

BluetoothManager.enableBluetooth() not trigger on the first event , but if you click again it will work. #128

Open michaelVictoriaDev opened 3 years ago

michaelVictoriaDev commented 3 years ago

my snippet code

constructor(props) {
    super(props);
    this.state = {
        // bluetooth
        devices: null,
        pairedDs: [],
        foundDs: [],
        bleOpend: false,
        loading: true,
        boundAddress: '',
        debugMsg: ''
    };

}

<Switch value={this.state.bleOpend} onValueChange={(v) => {

    console.log('bleOpend', this.state.bleOpend)
    console.log('VVVVVVVm', !v)
    if (!v) {
        console.log('WHY TRUE')
        BluetoothManager.disableBluetooth().then(() => {
            this.setState({
                bleOpend: false,
                loading: false,
                foundDs: [],
                pairedDs: []
            });
        }, (err) => { alert(err) });

    } else {
        console.log('WHY FALSE')

        BluetoothManager.enableBluetooth().then((r) => {
            debugger
            var paired = [];
            if (r && r.length > 0) {
                for (var i = 0; i < r.length; i++) {
                    try {
                        paired.push(JSON.parse(r[i]));
                    } catch (e) {
                        //ignore
                    }
                }
            }
            this.setState({
                bleOpend: true,
                loading: false,
                pairedDs: paired
            })
        }, (err) => {
            this.setState({
                loading: false
            })
            alert(err)
        });
    }
}} />

the output ezgif com-video-to-gif (2)