januslo / react-native-bluetooth-escpos-printer

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

NullPointerException in onActivityResult due to Null Bundle Reference #255

Open Carlosmax1 opened 1 month ago

Carlosmax1 commented 1 month ago

NullPointerException in onActivityResult due to Null Bundle Reference

We encountered a NullPointerException in the RNBluetoothManagerModule class within the onActivityResult method. This issue arises when the data.getExtras() call returns null, leading to a failure when attempting to invoke data.getExtras().getString(EXTRA_DEVICE_ADDRESS).

Code Location:

The issue is located in the following method:

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
    ...
    switch (requestCode) {
        case REQUEST_CONNECT_DEVICE: {
            if (resultCode == Activity.RESULT_OK) {
                String address = data.getExtras().getString(EXTRA_DEVICE_ADDRESS); // Potential NullPointerException here
                ...
            }
            break;
        }
        case REQUEST_ENABLE_BT: {
            Promise promise = promiseMap.remove(PROMISE_ENABLE_BT);
            if (resultCode == Activity.RESULT_OK && promise != null) {
                ...
            } else {
                ...
            }
            break;
        }
    }
}

Explanation:

The NullPointerException occurs because data.getExtras() can return null, especially if the Intent does not contain any extras. This makes it unsafe to directly call getString on data.getExtras() without checking if it is null.

Fix:

To address this issue, we should add a null check for data.getExtras() before attempting to access it. This will prevent the application from crashing when no extras are present in the Intent. Here's the updated code:

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
    BluetoothAdapter adapter = this.getBluetoothAdapter();
    Log.d(TAG, "onActivityResult " + resultCode);
    switch (requestCode) {
        case REQUEST_CONNECT_DEVICE: {
            if (resultCode == Activity.RESULT_OK && data != null && data.getExtras() != null) {
                String address = data.getExtras().getString(EXTRA_DEVICE_ADDRESS);
                if (adapter != null && BluetoothAdapter.checkBluetoothAddress(address)) {
                    BluetoothDevice device = adapter.getRemoteDevice(address);
                    mService.connect(device);
                }
            }
            break;
        }
        case REQUEST_ENABLE_BT: {
            Promise promise = promiseMap.remove(PROMISE_ENABLE_BT);
            if (resultCode == Activity.RESULT_OK && promise != null) {
                if (adapter != null) {
                    WritableArray pairedDeivce = Arguments.createArray();
                    Set<BluetoothDevice> boundDevices = adapter.getBondedDevices();
                    for (BluetoothDevice d : boundDevices) {
                        try {
                            JSONObject obj = new JSONObject();
                            obj.put("name", d.getName());
                            obj.put("address", d.getAddress());
                            pairedDeivce.pushString(obj.toString());
                        } catch (Exception e) {
                            //ignore
                        }
                    }
                    promise.resolve(pairedDeivce);
                } else {
                    promise.resolve(null);
                }
            } else {
                Log.d(TAG, "BT not enabled");
                if (promise != null) {
                    promise.reject("ERR", new Exception("BT NOT ENABLED"));
                }
            }
            break;
        }
    }
}

Reasoning

Adding these null checks ensures that the onActivityResult method handles cases where the Intent does not contain extras gracefully. This prevents unexpected crashes and improves the stability of the module.

Please review the proposed fix and let me know if any additional changes are required.