react-native-ble-manager icon indicating copy to clipboard operation
react-native-ble-manager copied to clipboard

Connect to device by getBondedPeripherals() function

Open erikthiago opened this issue 4 years ago • 5 comments

Version

Tell us which versions you are using:

  • react-native-ble-manager v7.2.0
  • react-native v0.61.4
  • Android v9

Expected behaviour

Connect to device by bonded list

Tell us what should happen

Connect to device

Steps to reproduce

  1. Get bonded devices list
  2. Select one
  3. Try connect

Code

let Bonded = () => {

    BleManager.start({ showAlert: false });

    BleManager.getBondedPeripherals()
        .then((bondedPeripheralsArray: any) => {
            setLista(bondedPeripheralsArray);
        }).catch((error) => {
            console.log(error.message);
        });
}

let Connect = async (item: any) => {

    BleManager.checkState();

    BleManager.connect(item.item.id)
        .then(() => {
            console.log("chegou aqui connect");

            // Success code
            ToastAndroid.showWithGravity(
                "Conectado ao dispositivo!",
                ToastAndroid.SHORT,
                ToastAndroid.CENTER
            );
        })
        .catch((error) => {
            // Failure code
            ToastAndroid.showWithGravity(
                "Erro: " + error,
                ToastAndroid.SHORT,
                ToastAndroid.CENTER
            );

            console.log(error);
        });
}

let RenderSeparator = () => { return ( <View style={{ height: 1, width: "86%", backgroundColor: "#CED0CE", marginLeft: "14%" }} /> ); };

let RenderItem = (item: any) => {
    const color = item.connected ? 'green' : '#fff';

    return (
        <TouchableHighlight onPress={() => Connect(item)} style = {styles.touch}>
            <View style={[styles.row, { backgroundColor: color }]} >
                <Text style={{ fontSize: 12, textAlign: 'center', color: '#333333', padding: 10 }}>{item.item.name}</Text>
                <Text style={{ fontSize: 12, textAlign: 'center', color: '#333333', padding: 10 }}>{item.item.id}</Text>
            </View>
        </TouchableHighlight>
    );
}

return (
    <View style={styles.container}>
        <TouchableOpacity onPress={Bonded} style={styles.button}>
            <Text style={styles.buttonText}>Dispositivos</Text>
        </TouchableOpacity>

        <FlatList 
            data={lista}
            renderItem={({ item }) => <RenderItem item={item} />}
            keyExtractor={(item, index) => index.toString()}
            ListEmptyComponent={() =>
                <Text style={styles.buttonText}>
                    Não há itens!
                </Text>
            }
            ItemSeparatorComponent={RenderSeparator}
        />
    </View>
);

}

erikthiago avatar Apr 21 '20 17:04 erikthiago

This operation is supported by this lib?

erikthiago avatar Apr 21 '20 17:04 erikthiago

Hi, I don't understand your question.

marcosinigaglia avatar May 07 '20 08:05 marcosinigaglia

I'm getting a list of bonded devices. I want to choose one and connect. But this operation return generic connection error. So, i want to know if this operation is supported in react-native-ble-manager. I'm using the above code. @marcosinigaglia

erikthiago avatar May 10 '20 22:05 erikthiago

Hi @marcosinigaglia , thank you very much for your job maintaining this package.

I'm having the same issue described by @erikthiago .

Basically, in my app I want to connect on bluetooth devices that are already paired with the android smartphone, in order to avoid scanning and thus requesting the user location. By calling BleManager.getBondedPeripherals() I correctly obtain the list of paired devices. When I try to connect to them though, I don't receive any answer.

try {
      let connectionResult = await BleManager.connect(device.id);
      console.log(connectionResult);
    } catch (err) {
      console.warn(err);
    }

I get no log and no warning, even waiting for a long amount of time.

Am I required to scan for devices to connect to a paired device?

Thanks

gbalduzzi avatar Mar 10 '21 10:03 gbalduzzi

Hi @gbalduzzi , I never worked with bonded device so I'm not sure that the code is perfect. The list of the peripherals is stored here but the getBondedPeripherals function is not updating the list so to use the peripheral you need to scan before. Try to change the method adding the missing peripheral in the list.

marcosinigaglia avatar Mar 22 '21 10:03 marcosinigaglia