januslo / react-native-bluetooth-escpos-printer

React-Native plugin for the bluetooth ESC/POS & TSC printers.
MIT License
349 stars 409 forks source link

Application crashes on android versions greater than 10. #239

Closed kitavidavis closed 4 months ago

kitavidavis commented 5 months ago

Hi @januslo , first, this is a great library. However, can you confirm if the library works on android versions greater than 10? My application crashes on android versions greater than 10 when enabling bluetooth.

andiyogaswara commented 5 months ago

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

kitavidavis commented 4 months ago

@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.

kitavidavis commented 4 months ago

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();
    }, [])