AllInOneYT / react-native-thermal-printer

MIT License
102 stars 53 forks source link

Not Working With Android 12 and above? #50

Open Yasa000 opened 1 year ago

lfrallon commented 1 year ago

react-native-thermal-printer@2.3.5 is working on Android 12.

lfrallon commented 1 year ago

react-native-thermal-printer@2.3.5 is working on Android 12.

react-native-thermal-printer@2.3.6 also works on Android 12.

But at first this is what I encountered using the latest version:

Error: UID 10476 / PID 3069 lacks permission android.permission.BLUETOOTH

And able to fix it through this mentioned thread. It's just probably device-specific I reckon.

Urolovshohjahon commented 1 year ago

can I use it by USB connection

qretsar commented 1 month ago

This solution works for EXPO SDK 51

  1. Create a file in the root directory : withCustomBluetoothPermission.js
  2. In the app.json or app.config. js add : plugins: [ withCustomBluetoothPermission, ],

File contents :

const { withAndroidManifest } = require("@expo/config-plugins");

module.exports = function withCustomBluetoothPermission(config) { return withAndroidManifest(config, async (config) => { const manifest = config.modResults;

// Find existing permissions or add them if they don't exist
const permissions = [
  { name: "android.permission.BLUETOOTH", toolsRemoveMaxSdkVersion: true },
  { name: "android.permission.BLUETOOTH_ADMIN" },
  { name: "android.permission.ACCESS_FINE_LOCATION" },
  { name: "android.permission.BLUETOOTH_SCAN", minSdkVersion: 31 },
  { name: "android.permission.BLUETOOTH_CONNECT", minSdkVersion: 31 },
  { name: "android.permission.BLUETOOTH_ADVERTISE", minSdkVersion: 31 },
];

permissions.forEach((permission) => {
  const existingPermission = manifest.manifest["uses-permission"].find(
    (perm) => perm["$"]["android:name"] === permission.name
  );

  if (existingPermission) {
    // Handle special cases like tools:remove="android:maxSdkVersion"
    if (permission.toolsRemoveMaxSdkVersion) {
      delete existingPermission["$"]["android:maxSdkVersion"];
      existingPermission["$"]["tools:remove"] = "android:maxSdkVersion";
    }

    if (permission.minSdkVersion) {
      existingPermission["$"]["tools:targetApi"] = permission.minSdkVersion;
    }
  } else {
    // If the permission doesn't exist, add it
    const newPermission = {
      $: { "android:name": permission.name },
    };

    if (permission.toolsRemoveMaxSdkVersion) {
      newPermission["$"]["tools:remove"] = "android:maxSdkVersion";
    }

    if (permission.minSdkVersion) {
      newPermission["$"]["tools:targetApi"] = permission.minSdkVersion;
    }

    manifest.manifest["uses-permission"].push(newPermission);
  }
});

return config;

}); };