Giter Site home page Giter Site logo

Comments (3)

andiyogaswara avatar andiyogaswara commented on September 16, 2024

you can use this repo for code example using this package : https://github.com/prawito/bluetooth-printer-react-native, or you can watch it on youtube https://www.youtube.com/watch?v=ABNmLKrhYdY&t=931s&ab_channel=prawitohudoro, it works very well on my device that using android version greater than 10

from react-native-bluetooth-escpos-printer.

kitavidavis avatar kitavidavis commented on September 16, 2024

@andiyogaswara which android version is your device? My application crashes on devices with android versions greater than 12. I have tested with more than 5 devices.

from react-native-bluetooth-escpos-printer.

kitavidavis avatar kitavidavis commented on September 16, 2024

I found a solution for this. Turns out that on android versions greater than 12, we need to request permission to scan or connect with bluetooth. This, however, needs to be done with bluetooth on. Here is a sample implementation:

import { PermissionsAndroid } from 'react-native';

const requestBluetoothScanPermission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
        {
          title: 'Bluetooth Scan Permission',
          message: 'This app needs Bluetooth scan permission to scan for devices.',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        }
      );
      if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
        console.log('Bluetooth scan permission denied');
        // Handle permission denial
      } else {
        console.log('Bluetooth scan permission granted');
        // Proceed with scanning
      }
    } catch (err) {
      console.log(err);
    }
  };

  const requestBluetoothConnectPermission = async () => {
    try {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
          {
            title: 'Bluetooth Connect Permission',
            message: 'This app needs Bluetooth connect permission to connect to a device.',
            buttonNeutral: 'Ask Me Later',
            buttonNegative: 'Cancel',
            buttonPositive: 'OK',
          }
        );
        if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
          console.log('Bluetooth connect permission denied');
          // Handle permission denial
        } else {
          console.log('Bluetooth connect permission granted');
          // Proceed with connection
        }
    } catch (err) {
      console.log(err);
    }
  };

Then, in your app.js:

    useEffect(() => {
      const checkBT = async() => {
        const isEnabled = await BluetoothManager.checkBluetoothEnabled();

        if(isEnabled){
            // Request scan permission:
            requestBluetoothScanPermission();

            // Request connect permission:
            requestBluetoothConnectPermission();
        }

        setBTEnabled(isEnabled);
      }

      checkBT();
    }, [])

from react-native-bluetooth-escpos-printer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.