React native - how can i listen callBack function

21 Views Asked by At

I wrote an application that works with Bluetooth. I have no problem connecting to the application, but I cannot receive the data sent from the Bluetooth device from the application. I think I used the correct code block in the Bluetooth library documentation. Do I need to do anything extra? Can you help me?

Document link : https://www.npmjs.com/package/@infobiotech/react-native-bluetooth-serial-next

My code is here:

import React, {Component, useEffect, useState} from 'react';
import {View, Text, Button} from 'react-native';
import BluetoothSerial from 'react-native-bluetooth-serial-next';

class Test5 extends React.Component {
  constructor(props: {}) {
    super(props);
    this.state = {
      isEnabled: false,
      connectedDevice: null,
    };
  }

  componentDidMount() {
    BluetoothSerial.isEnabled().then(enabled => {
      this.setState({isEnabled: enabled});
    });
  }

  connectToDevice = () => {
    BluetoothSerial.list()
      .then(devices => {
        console.log(devices);
        var device1 = devices[0];
        for (let index = 0; index < devices.length; index++) {
          let device = devices[index];
          if (device.id === 'A0:B7:65:54:0C:26') {
            device1 = device;
            break;
          }
        }
        const device = device1;
        BluetoothSerial.connect(device.id)
          .then(() => {
            this.setState({connectedDevice: device});
            console.log('Connected to device:', device.name);
          })
          .catch(error => {
            console.error('Connection error:', error);
          });
      })
      .catch(error => {
        console.error('List error:', error);
      });
  };

  sendData = () => {
    BluetoothSerial.write('10')
      .then(() => {
        console.log('Data sent successfully');
      })
      .catch(error => {
        console.error('Send error:', error);
      });
  };

  readData = () => {
    BluetoothSerial.read((data, subscription) => {
      console.log('Receive Data : ' + data);
      if (this.imBoredNow && subscription) {
        BluetoothSerial.removeSubscription(subscription);
      }
    });
  };

  render() {
    const {isEnabled, connectedDevice} = this.state;
    return (
      <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
        <Text>Bluetooth Classic</Text>
        {isEnabled ? (
          <View>
            {connectedDevice ? (
              <View>
                <Text>Connected to: {connectedDevice.name}</Text>
                <Button title="Send Data" onPress={this.sendData} />
              </View>
            ) : (
              <Button title="Connect" onPress={this.connectToDevice} />
            )}
          </View>
        ) : (
          <Button
            title="Enable Bluetooth"
            onPress={() => BluetoothSerial.enable()}
          />
        )}
      </View>
    );
  }
}

export default Test5;
0

There are 0 best solutions below