Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements for capacitor #80

Open
davi91 opened this issue Sep 6, 2024 · 0 comments
Open

Some improvements for capacitor #80

davi91 opened this issue Sep 6, 2024 · 0 comments

Comments

@davi91
Copy link

davi91 commented Sep 6, 2024

In order to implement this project in Capacitor, we had to make a change to the plugin.xml file:

   <js-module src="www/BluetoothPrinter.js" name="BluetoothPrinter">
       <clobbers target="cordova.plugins.btprinter" />
   </js-module>

With this change, we were able to use this library from cordova.plugins.

Additionally, we included a new method in the .java file to directly request permission from the user for the Bluetooth device, which is necessary for it to work from Android version 12 onwards, without needing to go through the "status" method:

    boolean sendPermissionChecker(CallbackContext callbackContext) {
        try {

            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter == null) {
                Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                this.cordova.getActivity().startActivityForResult(enableBluetooth, 0);
                callbackContext.success(0);
            }
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                this.cordova.getActivity().startActivityForResult(enableBluetooth, 0);
                callbackContext.success(0);
            }
            callbackContext.success(1);
        } catch (Exception e) {
            String errMsg = e.getMessage();
            Log.e(LOG_TAG, errMsg);
            e.printStackTrace();
            callbackContext.error(errMsg);
        }

        return false;
    }

In this same method, it would be appropriate for future versions to consider that this permission request system is outdated; it is now recommended to use the ActivityResultLauncher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant