felHR85 / UsbSerial

Usb serial controller for Android
MIT License
1.8k stars 585 forks source link

NullPoint Exception #249

Open Scott12334 opened 5 years ago

Scott12334 commented 5 years ago

Hello, So I tried running the code, and the USB device code does not seem to be working. It returns a null value and then says there is a NullPointException. Thank you in advance, and I really appreciate this amazing library really makes life easy. Here are some screenshots. Yes I know the UsbDevice Device =null is wrong that was just a test Capture

felHR85 commented 5 years ago

Hi @Scott12334 It is a NullPointerException because both device and usbConnection are null. Please use this Service as an example. findSerialPortDevice() method is used to properly handle the device variable

Scott12334 commented 5 years ago

Sorry, I don't quite understand. How would I implement findSerialPortDevice()? The service you showed didn't show that. Sorry, I am new to android and I really appreciate your help.

felHR85 commented 5 years ago

@Scott12334 For Android beginners I recommend using the example as a starting point and make your changes there.

Scott12334 commented 5 years ago

Ok so you recommend I copy that code and make that my service?

felHR85 commented 5 years ago

@Scott12334 And even the Activity if you are not so sure.

Scott12334 commented 5 years ago

The problem is I understand my code right now, except for that one error. Is there a simple fix using findSerialPortDevice. Thank you

felHR85 commented 5 years ago

UsbSerialDevice serial = UsbSerialDevice.createUsbSerialDevice(device, usbConnection); This line can't work because both arguments are null. You are declaring it when the service class is init and thats the reason why both are null (even device is set directly to null). Please refer to the example

Scott12334 commented 5 years ago

So instead of doing this code serial.read(mCallback); int UnityPrint = serial.read(mCallback); do this mHandler.obtainMessage(MESSAGE_FROM_SERIAL_PORT, data).sendToTarget(); that way i dont have to use serial.

Scott12334 commented 5 years ago

I am really sorry but I don't see a part of the example that fixes that line. The example just skips that line

Scott12334 commented 5 years ago

Sorry about that I found it

Scott12334 commented 5 years ago

Ok so I found it but now I am having trouble using it. I simply just want to have that device that is found to become private UsbDevice device; None of the vendor ID or permission requests. Thank you for your help

Scott12334 commented 5 years ago

This is what I have tried: Capture