star-micronics / react-native-star-io10

react-native-star-io10 is a library for supporting application development for Star Micronics devices.
Other
83 stars 54 forks source link

Unable to search for devices and printers on latest android version. #67

Closed hamzaahzam closed 1 year ago

hamzaahzam commented 2 years ago

Description

Unable to search for devices and printers on latest android version.

Device where the bug occurs

Your printer

Your development environment

macOS

Also, the result of executing the command npx react-native info.

Code To Reproduce

const discoverPrinters = async () => {
    try {
      var printersToSave = [];
      var printerList = [];
      var interfaceTypes = [
        Platform.OS === 'ios'
          ? InterfaceType.BluetoothLE
          : InterfaceType.Bluetooth,
      ];
      var manager = await StarDeviceDiscoveryManagerFactory.create(
        interfaceTypes,
      );

      manager.discoveryTime = 5000;

      // Callback for printer found.
      manager.onPrinterFound = printer => {
        printersToSave.push(printer);
        printerList.push({
          label: printer.connectionSettings.identifier,
          value: printer,
        });
        if (printer.connectionSettings.identifier === printerId) {
          // setSelectedPrinter(printer)
          setSelectedPrinter({
            label: printer.connectionSettings.identifier,
            value: printer,
          });
          dispatch(updatePrinter(printer));
        }
        // if()
      };

      manager.onDiscoveryFinished = () => {
        setPrinters(printerList);
      };
      await manager.startDiscovery();
    } catch (error) {}
  };

Error

In android 12 while running the above code app crashes and in android 11 app doesn't crashes but unable to scan nearby printer. But same code works fine on android 10 and earlier versions.

bandit-ibayashi commented 2 years ago

@hamzaahzam Could you please refer to the following URL and check if the permissions are properly granted according to the target SDK version? https://github.com/star-micronics/react-native-star-io10#android

DevHumzaAhzam commented 2 years ago

@bandit-ibayashi I have added all the permissions for android it is crashing on my Redmi Note 10 Pro android version 12

DevHumzaAhzam commented 2 years ago

In Logs I am getting this error java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for android.content.AttributionSource@655f4942: AdapterService getBondedDevices

mohsinmughal1630 commented 2 years ago

same issue i am facing

bandit-ibayashi commented 2 years ago

@hamzaahzam

It is possible to request the permission android.permission.BLUETOOTH_CONNECT with the following code, but could you still not get the permission by doing this? https://github.com/star-micronics/react-native-star-io10/blob/e544aacab74b480540558641dc8003a7126f30d8/example/samples/discovery/App.tsx#L101

This code must be executed when Android 12 or later and targetSDKVersion is 31 or later.

https://github.com/star-micronics/react-native-star-io10/blob/e544aacab74b480540558641dc8003a7126f30d8/example/samples/discovery/App.tsx#L38

I executed our discovery sample code on a Google Pixel 6 with Android 12 and was able to successfully search for Bluetooth printers.

DevHumzaAhzam commented 2 years ago

@bandit-ibayashi Thanks after requesting android permission I was able to find printers and send request for printing.

But I am facing an issue on Redmi Note 10 Pro after requesting permission it starts searching for printers but always returns nothing on onDiscoveryFinished

bandit-ibayashi commented 2 years ago

@DevHumzaAhzam

Found printers are retrieved with onPrinterFound property, not onDiscoveryFinished. onDiscoveryFinished property indicates that it only notifies the user that the Discovery process is finished and does not return information on the printers found as a result of the Discovery process. You can also refer the sample code here.

Or would it be a case where onDiscoveryFinished is issued as soon as startDiscovery method is executed in your situation with Redmi Note 10 Pro?

bandit-ibayashi commented 1 year ago

We will close it once there have been no comments for a certain period of time. If you continue to experience this problem, please create a new Issue.