NPE during attempt connection with device
See original GitHub issue-
Device: [Motorola Moto g7 Power]
-
OS: [Android 10, API 29]
-
React Native version: [0.63.1]
-
RN Bluetooth Classic version: [1.60.0-rc.6]
Describe the bug
When I try to connect with device, then an error appears: NPE in native module (android).
In details error comes from method
public void connectToDevice(String address, ReadableMap parameters, Promise promise) in RNBluetoothClassicModule
To Reproduce Steps to reproduce the behavior:
- Start discovery
- Try to connect with device with invoking:
connect = async (address: string): Promise<BluetoothDevice> => { return await RNBluetoothClassic.connectToDevice(address); };
- See error as mentioned above.
Logs
CatalystInstanceImpl caught native exception java.lang.NullPointerException: Attempt to invoke interface method 'java.util.HashMap com.facebook.react.bridge.ReadableMap.toHashMap()' on a null object reference at kjd.reactnative.bluetooth.Utilities.mapToProperties(Utilities.java:33) at kjd.reactnative.bluetooth.RNBluetoothClassicModule.connectToDevice(RNBluetoothClassicModule.java:728) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151) at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loop(Looper.java:241) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226) at java.lang.Thread.run(Thread.java:919)
Expected behavior An error not appear, get default parameters or the method should require additional parameters.
I’m not sure i made something mistake so please read it and tell me am i wrong or it’s just bug in code. This error appears when i try to establish connection with real device by invoking code as mentioned in 2 step. Thank you for any answer. I really appreciate your work, specially the part with iOS. Im considering to use your library but i have a little problem with android module.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
First off, if you’re attempting to connect to an unpaired device, I don’t know what will happen. I’ve never attempted it. Looking at the code for
connectToDevice
it’s definitely not checking that the device is paired before attempting it.(but you just added stack trace, so it doesn’t look like that)
The line in question
Properties properties = Utilities.mapToProperties(parameters);
definitely looks like the second parameter ofpublic void connectToDevice(String address, ReadableMap parameters, Promise promise)
is null.I thought that I had defaults set in both:
BluetoothDevice.ts#connect()
BluetoothModule.ts#connectToDevice()
The latter being the most important, but I guess not. You should be able to get around it by
RNBluetoothClassic.connectToDevice(address, {});
Super shady, but should be doable until I add in the defaults.
Good catch!
Was able to replicate and resolve this on Android.
v1.60.0-rc.7 available.