markusfisch / BinaryEye

Yet another barcode scanner for Android
https://play.google.com/store/apps/details?id=de.markusfisch.android.binaryeye
MIT License
1.26k stars 108 forks source link

Fixes issue of missing send due to invalid bluetooth socket connection #438

Closed KamaleiZestri closed 4 months ago

KamaleiZestri commented 4 months ago

There is no way to truly check if the bluetooth connection is still valid without failing to send, because [API level 14 is required for that function.](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#isConnected())

Currently, given the following situation:

  1. First connect to bluetooth socket and send scan.
  2. Disconnect from bluetooth socket randomly. (i.e. move outside bluetooth range of device)
  3. Attempt to send again.

The second sent scan will be missed due to the socket being invalid.

This PR has BinaryEye try to send the scan a second time whenever there is a failure due to a previously connected bluetooth socket so that the user does not have to physically scan twice.

markusfisch commented 4 months ago

Well, why not use [isConnected](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#isConnected()) where it's available? We can always use a runtime check:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES. ICE_CREAM_SANDWICH) {
    // use isConnected()
} else {
    // maybe use the workaround, or just keep it as it is?
}

The reason why I keep minSDK so low is to avoid excluding (people with) devices that are good and able to run this app (in principle). If there are some functions that only work on higher versions, that's perfectly okay! So I would really prefer a runtime check and isConnected() to a workaround.

KamaleiZestri commented 4 months ago

Cool. I totally agree with your rationale about keeping the minSDK low, I just didn't know if you wanted to have all functions work on all versions or not.

I looked into and tested out [isConnected()](https://developer.android.com/reference/android/bluetooth/BluetoothSocket#isConnected()) now and unfortunately it turns out that this function does not actively check the connection. It just refers to a previously stored variable of it's own.

So responding to the exception thrown by a failed connection/failed send is actually the only way to provide this functionality.

markusfisch commented 4 months ago

Ah, okay, I see. Thanks for looking into it anyway 👍 Then I'll merge this as it is, of course.