douglasjunior / react-native-easybluetooth-classic

⚛ A Library for easy implementation of Serial Bluetooth Classic on React Native (Android Only).
https://www.npmjs.com/package/easy-bluetooth-classic
MIT License
43 stars 7 forks source link

Can't Connect #33

Open daniyal-intellicel opened 5 years ago

daniyal-intellicel commented 5 years ago

This is my code, I have checked I am getting the device correctly but this is the error I am getting

Unable to connect to: IXI [9C:B6:D0:17:7E:4A]

This is my code

import React, { Component } from 'react'
import { View, Text, TouchableOpacity } from 'react-native'
import EasyBluetooth from 'easy-bluetooth-classic';

export default class App extends Component {
  state = {
    device: null
  }

  componentDidMount() {
    let config = {
      uuid: "00001101-0000-1000-8000-00805f9b34fb",
      deviceName: "Mi A2",
      bufferSize: 1024,
      characterDelimiter: "\n"
    }

      EasyBluetooth.init(config)
      .then(function (config) {
        console.log("config done!");
      })
      .catch(function (ex) {
        console.warn(ex);
      });

    this.onDeviceFoundEvent = EasyBluetooth.addOnDeviceFoundListener(this.onDeviceFound.bind(this));
    this.onStatusChangeEvent = EasyBluetooth.addOnStatusChangeListener(this.onStatusChange.bind(this));
    this.onDataReadEvent = EasyBluetooth.addOnDataReadListener(this.onDataRead.bind(this));
    this.onDeviceNameEvent = EasyBluetooth.addOnDeviceNameListener(this.onDeviceName.bind(this));
  }

  startScan = () => {
    EasyBluetooth.startScan()
      .then(function (devices) {
        console.log("all devices found:");
        console.log(devices);
      })
      .catch(function (ex) {
        console.warn(ex);
      });
  }

  connect = () => {
    console.log('device', this.state.device);

    EasyBluetooth.connect(this.state.device)
      .then(() => {
        console.log("Connected!");
      })
      .catch((ex) => {
        console.warn(ex);
      })
  }

  write = () => {
    EasyBluetooth.writeln("Works in React Native!")
      .then(() => {
        console.log("Writing...")
      })
      .catch((ex) => {
        console.warn(ex);
      })
  }

  onDeviceFound(device) {
    console.log("onDeviceFound");
    console.log(device);

    if (device.name === 'IXI') {
      this.setState({ device });
    }
  }

  onStatusChange(status) {
    console.log("onStatusChange");
    console.log(status);
  }

  onDataRead(data) {
    console.log("onDataRead");
    console.log(data);
  }

  onDeviceName(name) {
    console.log("onDeviceName");
    console.log(name);
  }

  componentWillUnmount() {
      this.onDeviceFoundEvent.remove();
      this.onStatusChangeEvent.remove();
      this.onDataReadEvent.remove();
      this.onDeviceNameEvent.remove();
  }

  render() {
    return (
      <View>
        <Button onPress={this.startScan}>Scan</Button>
        <Button onPress={this.connect}>Connect</Button>
      </View>
    )
  }
}

const Button = ({ onPress, children }) => (
  <TouchableOpacity onPress={onPress} style={{ borderWidth: 1, width: 100, height: 50, alignItems: 'center', justifyContent: 'center'}}>
    <Text>{children}</Text>
  </TouchableOpacity>
)