Skip to content

Commit 5d80921

Browse files
committed
avoid showing the arduino output if using android 5.1.1 which makes the app crash
1 parent f79d902 commit 5d80921

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
This is being done as a hobby, and for experimenting, so probably there might be some flaws; As an example, the vendor ID of Arduino is hardcoded to only work with Arduino devices, but this is my use case and please feel free to change it to match your needs.
77

8+
### Knows Issues
9+
On Android 5.1.1, the Arduino serial output cannot be shown. (It is said that an Android internal bug is the issue!)
10+
811
Suggestions and PRs are welcome! :)
912

1013
### More comes as the project evolves...

app/src/main/java/org/kabiri/android/usbterminal/MainActivity.kt

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import android.content.IntentFilter
88
import android.hardware.usb.UsbDevice
99
import android.hardware.usb.UsbDeviceConnection
1010
import android.hardware.usb.UsbManager
11+
import android.os.Build
1112
import android.os.Bundle
13+
import android.util.Log
1214
import android.view.Menu
1315
import android.view.MenuInflater
1416
import android.view.MenuItem
17+
import android.widget.Toast
1518
import androidx.appcompat.app.AppCompatActivity
1619
import com.felhr.usbserial.UsbSerialDevice
1720
import com.felhr.usbserial.UsbSerialInterface
@@ -22,6 +25,7 @@ import java.nio.charset.Charset
2225
class MainActivity : AppCompatActivity() {
2326

2427
companion object {
28+
private const val TAG = "MainActivity"
2529
private const val ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"
2630
}
2731

@@ -41,9 +45,14 @@ class MainActivity : AppCompatActivity() {
4145
when (intent?.action) {
4246
ACTION_USB_PERMISSION -> {
4347
synchronized(this) {
44-
val device: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
48+
val device: UsbDevice? =
49+
intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
4550

46-
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
51+
if (intent.getBooleanExtra(
52+
UsbManager.EXTRA_PERMISSION_GRANTED,
53+
false
54+
)
55+
) {
4756
tvOutput.append("\nPermission granted for ${device?.manufacturerName}")
4857
device?.apply {
4958
// setup the device communication.
@@ -59,20 +68,43 @@ class MainActivity : AppCompatActivity() {
5968
it.setParity(UsbSerialInterface.PARITY_NONE)
6069
it.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF)
6170
it.read { message ->
62-
try {
63-
val encoded =
64-
String(message, Charset.defaultCharset())
65-
tvOutput.append(encoded)
66-
} catch (e: UnsupportedEncodingException) {
67-
e.printStackTrace()
68-
tvOutput.append("\n${e.localizedMessage}")
71+
// check if the Android version is not 5.1.1 Lollipop
72+
// before printing the message into output.
73+
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
74+
Log.e(
75+
TAG,
76+
"Lollipop 5.1.1 is not supported to show the serial messages from the Arduino."
77+
)
78+
} else {
79+
message?.let {
80+
try {
81+
val encoded =
82+
String(
83+
message,
84+
Charset.defaultCharset()
85+
)
86+
tvOutput.append(encoded)
87+
} catch (e: UnsupportedEncodingException) {
88+
e.printStackTrace()
89+
tvOutput
90+
.append("\n${e.localizedMessage}")
91+
} catch (e: Exception) {
92+
Toast.makeText(
93+
this@MainActivity,
94+
e.localizedMessage,
95+
Toast.LENGTH_SHORT
96+
).show()
97+
}
98+
}
6999
}
70100
}
71101
tvOutput.append("\nSerial Connection Opened")
72102
} else {
73103
tvOutput.append("\nPort not opened")
74104
}
75-
} else { tvOutput.append("\nSerial Port was null") }
105+
} else {
106+
tvOutput.append("\nSerial Port was null")
107+
}
76108

77109
}
78110
} else {
@@ -102,7 +134,7 @@ class MainActivity : AppCompatActivity() {
102134
}
103135

104136
override fun onOptionsItemSelected(item: MenuItem): Boolean {
105-
return when(item.itemId) {
137+
return when (item.itemId) {
106138
R.id.actionConnect -> {
107139

108140
val usbDevices = usbManager.deviceList

0 commit comments

Comments
 (0)