I am unable to obtain an input stream from a serial port.
`public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "HELLO";
private static final String TAG = "mainActivity";
private UsbManager manager;
private HashMap<String, UsbDevice> deviceList;
private UsbDevice device;
private UsbDeviceConnection connection;
private UsbSerialDevice serial;
private MavlinkConnection mavConn;
private MavlinkMessage message;
private SerialInputStream input;
private SerialOutputStream output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void onConnect(View view) throws IOException {
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
deviceList = manager.getDeviceList();
if(deviceList.isEmpty()) {
new AlertDialog.Builder(this)
.setTitle("Error!")
.setMessage("no devices connected")
.show();
return;
}
else {
TextView textView = findViewById(R.id.textView);
device = (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
connection = manager.openDevice(device);
serial = UsbSerialDevice.createUsbSerialDevice(device, connection);
if(serial != null){
if(serial.open()) {
serial.setBaudRate(57600);
}
else {
new AlertDialog.Builder(this)
.setTitle("Error!")
.setMessage("Port not open")
.show();
return;
}
}
else {
new AlertDialog.Builder(this)
.setTitle("Error!")
.setMessage("Serial is null")
.show();
return;
}
// ************************ all fine till here. displays deviceID on phone ************************************
textView.setText(Integer.toString(serial.getDeviceId()));
input = serial.getInputStream(); //crashes right here.
if(input != null) {
textView.setText("got stream");
}
else {
new AlertDialog.Builder(this)
.setTitle("Error!")
.setMessage("Null stream")
.show();
return;
}
/* ******************cannot test this piece of code due to lack of input stream**********************
output = serial.getOutputStream();
mavConn = MavlinkConnection.create(input, output);*/
connection.close();
}
}
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
@Override
public void onReceivedData(byte[] arg0)
{
/*cannot test this piece of code due to lack of input stream
while (true) {
try {
while (!((message = mavConn.next()) != null)) {
if(message.getPayload() instanceof Heartbeat) {
textView.setText(message.getPayload().toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}*/
}
};
}`
When the app is installed and run on my phone as a USB host with a Pixhawk microcontroller attached, I am able to view the serial port details like deviceID, baud rate, etc in the textView, but just when I do serial.getInputStream(), the app crashes.
I have no method to know why it crashes. I just get the "App keeps stopping" error message. I cannot view the log messages via Logcat since I'm not connected to my PC. I also cannot debugg over WiFi because I do majority of my work in my office where I use Ethernet LAN, not WiFi.
What are my options? Is debugging over WiFi the only way? Can't I print the logs to some text file on my phone and then view them later?
Details:
Android Studio Arctic Fox
Ubuntu 18.04.4 LTS
Samsung phone with Android 11
I am unable to obtain an input stream from a serial port.
`public class MainActivity extends AppCompatActivity {
}`
When the app is installed and run on my phone as a USB host with a Pixhawk microcontroller attached, I am able to view the serial port details like deviceID, baud rate, etc in the
textView
, but just when I doserial.getInputStream()
, the app crashes.I have no method to know why it crashes. I just get the "App keeps stopping" error message. I cannot view the log messages via Logcat since I'm not connected to my PC. I also cannot debugg over WiFi because I do majority of my work in my office where I use Ethernet LAN, not WiFi.
What are my options? Is debugging over WiFi the only way? Can't I print the logs to some text file on my phone and then view them later?