rusel1989 / react-native-bluetooth-serial

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

BluetoothSerial.readFromDevice() comes back empty #117

Open gilbertocortez opened 4 years ago

gilbertocortez commented 4 years ago

Hello,

I am trying to read a smart bicycle trainer (Tacx Flux 2), and I am able to pair to the trainer, but then when I try to read from the device it comes back empty. I tried to set the reader on an interval as well, to see if maybe by moving the trainer it would update the value but still nothing. Trainer does send info to other apps like Zwift or Tacx App. Please help, here is my code:

Thank you for the help!

import React, { useState, useEffect } from 'react' import { StyleSheet, Text, View, Button, PermissionsAndroid, FlatList } from 'react-native' import BluetoothSerial from 'react-native-bluetooth-serial' import KeepAwake from 'react-native-keep-awake'

const IndoorTrainer = () => {

PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION) .then(result => { if (!result) { PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION); } });

// Initial states const [bluetoothDeviceInitialState, setbluetoothDeviceInitialState] = useState({ isEnabled: false, loading: true }); const [bluetoothDeviceState, setbluetoothDeviceState] = useState({ discovering: false, connected: false, devices: [], unpairedDevices: [] });

// Use effect when enable button is clicked useEffect(() => { if (bluetoothDeviceInitialState.isEnabled === true) { console.log('new try -------------------------'); console.log('Bluetooth has been enabled'); console.log(bluetoothDeviceInitialState); console.log(bluetoothDeviceState); afterEnable(); } else console.log('Bluetooth is not enabled'); }, [bluetoothDeviceInitialState]);

// Options completed after bluetooth is enabled const afterEnable = () => { BluetoothSerial.on('bluetoothEnabled', () => console.log('Bluetooth enabled')) BluetoothSerial.on('bluetoothDisabled', () => console.log('Bluetooth disabled')) BluetoothSerial.on('error', (err) => console.log(Error: ${err.message})) console.log('completed afterEnable()'); }

// After discover devices button is clicked const discoverUnpaired = () => { //console.log('Start discovering devices'); if (bluetoothDeviceState.discovering === true) { console.log('cancel on discoverUnpaired()'); return false } else { console.log('Start discovering bluetooth devices'); setbluetoothDeviceState({ discovering: false, connected: false, devices: [], unpairedDevices: [] });

  BluetoothSerial.discoverUnpairedDevices()
    .then((unpairedDevices) => {
      console.log('Found devices, adding to state');
      //console.log(unpairedDevices);
      setbluetoothDeviceState({
        discovering: false,
        connected: false,
        devices: [],
        unpairedDevices: unpairedDevices,
      })
    })
    .catch((err) => console.log(err.message))
}

}

useEffect(() => { console.log("Bluetooth devices updated"); //console.log(bluetoothDeviceState.unpairedDevices); }, [bluetoothDeviceState]);

// Request bluetooth to be enabled const requestEnable = () => { BluetoothSerial.requestEnable() .then((res) => setbluetoothDeviceInitialState({ isEnabled: true, loading: false }) ) .catch((err) => console.log(err.message)); }

/**

  • Connect to bluetooth device by id
  • @param {Object} device */ const connectToBluetoothDevice = (device) => { console.log(Trying to connect to device ${device.name}) BluetoothSerial.connect(device.id) .then((res) => { console.log(Connected to device ${device.name}) console.log(bluetoothDeviceState) }) .catch((err) => console.log(err.message)) }

    /**

  • [android]
  • Pair device */ const pairDevice = (device) => { console.log(Trying to pair to device ${device.name}) BluetoothSerial.pairDevice(device.id) .then((paired) => { console.log(Pairing to device ${device.name}) if (paired) { console.log(Paired to device ${device.name}) console.log(bluetoothDeviceState) } else { console.log(Device ${device.name} pairing failed) } }) .catch((err) => console.log(err.message)) }

    /**

    • Disconnect from bluetooth device */ const disconnect = (device) => { BluetoothSerial.unpairDevice(device.id) BluetoothSerial.disconnect() .then(() => console.log('Device disconnected')) .catch((err) => console.log(err.message)) }

    const readFromBluetoothDevice = () => { console.log('Reading from device') BluetoothSerial.readFromDevice(). then((data) => { console.log('Device read') console.log('Data: ' + data) }) .catch((err) => console.log(err.message)) }

    return (

    Bluethoot App

export default IndoorTrainer

const styles = StyleSheet.create({})

qingjiaowochengzd commented 4 years ago

me too

ambojialur commented 2 years ago

This code is not working for me

BluetoothSerial.readFromDevice()
        .then(data => {
          console.log('Data: ' + data);   
        })
        .catch(err => console.log(err.message));