From 93df4cb2284900d06afcfcf5884fca19af45d9d7 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Fri, 13 Sep 2024 15:45:58 +0200 Subject: [PATCH 01/20] Update ScreenCaptureService.java --- .../ScreenCaptureService.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/ScreenCaptureService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/ScreenCaptureService.java index dc4806eb..76189baf 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/ScreenCaptureService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/ScreenCaptureService.java @@ -149,7 +149,7 @@ public void onImageAvailable(ImageReader reader) { int roiY = fullHeight - roiHeight; // Create a new bitmap for the region of interest - Bitmap roiBitmap = Bitmap.createBitmap(fullBitmap, 0, roiY, fullWidth, roiHeight); + Bitmap roiBitmap = Bitmap.createBitmap(fullBitmap, 0, 0, fullWidth, 300); // Recycle the full bitmap as we no longer need it fullBitmap.recycle(); @@ -186,8 +186,9 @@ public void onFailure(Exception e) { } }); } - } catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); + isRunning = false; } } } From fd0e51a330f817d3860144122645a665c4c751e4 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Fri, 13 Sep 2024 15:49:53 +0200 Subject: [PATCH 02/20] Update QZService.java --- .../QZService.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index be775bac..7ca37034 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -182,6 +182,32 @@ public String[] getOCR() { } } + if (lines[i].toLowerCase().contains("cadence")) { + try { + QZService.lastCadence = "Changed RPM " + lines[i-1].trim(); + } catch (Exception e) { + QZService.lastCadence = ""; + } + + } + if (lines[i].toLowerCase().contains("resistance")) { + try { + QZService.lastResistance = "Changed Resistance " + lines[i-1].trim(); + QZService.lastResistanceFloat = Float.parseFloat(lines[i-1].trim()); + } catch (Exception e) { + QZService.lastResistance = ""; + QZService.lastResistanceFloat = 0.0f; + } + + } + if (lines[i].toLowerCase().contains("watt")) { + try { + QZService.lastWattage = "Changed Watt " + lines[i-1].trim(); + } catch (Exception e) { + QZService.lastWattage = ""; + } + + } if (lines[i].toLowerCase().contains("speed")) { try { QZService.lastSpeed = "Changed KPH " + lines[i-1].trim(); From 98dfb018285a4013ba29797ae599c41cbf7aeffa Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Fri, 13 Sep 2024 15:50:34 +0200 Subject: [PATCH 03/20] Update main.yml --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d7cfd7ed..b8411f78 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -46,7 +46,7 @@ jobs: BUILD_TOOLS_VERSION: ${{ env.BUILD_TOOL_VERSION }} - name: Archive apk - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: apk path: ${{steps.sign_app.outputs.signedReleaseFile}} @@ -72,4 +72,4 @@ jobs: tag_name: 3.4.8 #${{ steps.get_version.outputs.VERSION }} asset_path: ${{steps.sign_app.outputs.signedReleaseFile}} asset_name: QZCompanionNordictrackTreadmill.apk - asset_content_type: application/zip \ No newline at end of file + asset_content_type: application/zip From b673bd6e1913c21cf3b68210b12a8aace6043023 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Fri, 13 Sep 2024 15:53:41 +0200 Subject: [PATCH 04/20] Update QZService.java --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 7ca37034..fb32f5f5 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -43,8 +43,8 @@ public class QZService extends Service { static float lastResistanceFloat = 0; static String lastSpeed = ""; static String lastInclination = ""; - String lastWattage = ""; - String lastCadence = ""; + static String lastWattage = ""; + static String lastCadence = ""; static String lastResistance = ""; String lastGear = ""; static String lastHeart = ""; From d4986104e91a2fb20cf769e720134f4e93ef1c32 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Mon, 16 Sep 2024 17:54:31 +0200 Subject: [PATCH 05/20] Update QZService.java --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index fb32f5f5..3ac49954 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -202,7 +202,7 @@ public String[] getOCR() { } if (lines[i].toLowerCase().contains("watt")) { try { - QZService.lastWattage = "Changed Watt " + lines[i-1].trim(); + QZService.lastWattage = "Changed Watts " + lines[i-1].trim(); } catch (Exception e) { QZService.lastWattage = ""; } From 81c0525bb25166841d7b16bfa7fe29a7b0596d38 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Fri, 20 Sep 2024 10:53:53 +0200 Subject: [PATCH 06/20] pausing the parse engine leaving only the ocr one --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 66eb1892..fb13b8a4 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -64,7 +64,7 @@ public void onCreate() { runnable = new Runnable() { @Override public void run() { - writeLog( "Service run"); parse(); getOCR(); + writeLog( "Service run"); /*parse();*/ getOCR(); } }; } finally { @@ -234,6 +234,7 @@ public String[] getOCR() { return result; } socket.close(); + handler.postDelayed(runnable, 100); return result; } From b3a939e17f437bd4d03fbd2f346b2146ebaf61b9 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Fri, 20 Sep 2024 12:31:54 +0200 Subject: [PATCH 07/20] fixing dirty wattage values --- .../qzcompanionnordictracktreadmill/QZService.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index fb13b8a4..111c48a8 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -205,9 +205,13 @@ public String[] getOCR() { } if (lines[i].toLowerCase().contains("watt")) { try { - QZService.lastWattage = "Changed Watts " + lines[i-1].trim(); + String[] parts = lines[i-1].trim().split("\\s+"); + String lastPart = parts[parts.length - 1]; + + if (lastPart.matches("-?\\d+(\\.\\d+)?")) { + QZService.lastWattage = "Changed Watts " + lastPart; + } } catch (Exception e) { - QZService.lastWattage = ""; } } From f87c040f41796312671cbe24eb681bf09a48242f Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Tue, 24 Sep 2024 11:10:57 +0200 Subject: [PATCH 08/20] fixing wattage cadence and resistance --- .../QZService.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 111c48a8..3a4adaeb 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -205,11 +205,11 @@ public String[] getOCR() { } if (lines[i].toLowerCase().contains("watt")) { try { - String[] parts = lines[i-1].trim().split("\\s+"); - String lastPart = parts[parts.length - 1]; - - if (lastPart.matches("-?\\d+(\\.\\d+)?")) { - QZService.lastWattage = "Changed Watts " + lastPart; + String numberStr = lines[i-1].trim().replaceAll("[^0-9]", " ").trim(); + String[] numbers = numberStr.split("\\s+"); + if (numbers.length > 0) { + int watts = Integer.parseInt(numbers[numbers.length - 1]); + QZService.lastWattage = "Changed Watts " + watts; } } catch (Exception e) { } @@ -233,6 +233,12 @@ public String[] getOCR() { sendBroadcast(QZService.lastSpeed); if(!QZService.lastInclination.equals("")) sendBroadcast(QZService.lastInclination); + if(!QZService.lastWattage.equals("")) + sendBroadcast(QZService.lastWattage); + if(!QZService.lastCadence.equals("")) + sendBroadcast(QZService.lastCadence); + if(!QZService.lastResistance.equals("")) + sendBroadcast(QZService.lastResistance); } catch (Exception ex) { ex.printStackTrace(); return result; From c799779a20a14679589c87edb8d710d364f38de7 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Sat, 30 Nov 2024 15:42:50 +0100 Subject: [PATCH 09/20] fixing merge --- .../qzcompanionnordictracktreadmill/MainActivity.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/MainActivity.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/MainActivity.java index 46162fda..f94f698c 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/MainActivity.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/MainActivity.java @@ -418,9 +418,10 @@ public void run() { .show(); } - if (savedInstanceState == null) { // Verifica se è la prima creazione + // With OCR we can't move it to back here, only after asking the permission, restore it somewhere + /*if (savedInstanceState == null) { // Verifica se è la prima creazione moveTaskToBack(true); // Sposta l'Activity in background - } + }*/ } private boolean isAccessibilityServiceEnabled(Context context, Class accessibilityService) { From 6278b9d0a656fa2fe301fb7e18c7bf69f3f039e8 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Sat, 30 Nov 2024 15:49:08 +0100 Subject: [PATCH 10/20] trying to improve errors --- .../ScreenCaptureService.java | 173 +++++++++++++----- 1 file changed, 126 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/ScreenCaptureService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/ScreenCaptureService.java index 76189baf..dbef6248 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/ScreenCaptureService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/ScreenCaptureService.java @@ -29,6 +29,7 @@ import java.nio.ByteBuffer; import java.util.Objects; +import com.google.android.gms.tasks.OnCompleteListener; import com.google.mlkit.vision.common.InputImage; import com.google.mlkit.vision.text.Text; import com.google.mlkit.vision.text.TextRecognition; @@ -42,6 +43,7 @@ import android.graphics.Rect; import android.graphics.Point; +import androidx.annotation.NonNull; import androidx.core.util.Pair; import android.util.Log; import android.os.Build; @@ -125,70 +127,147 @@ private static int getVirtualDisplayFlags() { } private class ImageAvailableListener implements ImageReader.OnImageAvailableListener { + private static final String TAG = "OCR"; + private static final int PROCESSING_TIMEOUT_MS = 5000; + private final Handler timeoutHandler = new Handler(); + + private class BitmapHolder { + Bitmap fullBitmap; + Bitmap roiBitmap; + + void recycleBitmaps() { + if (fullBitmap != null && !fullBitmap.isRecycled()) { + fullBitmap.recycle(); + Log.d(TAG, "Full bitmap recycled"); + } + if (roiBitmap != null && !roiBitmap.isRecycled()) { + roiBitmap.recycle(); + Log.d(TAG, "ROI bitmap recycled"); + } + } + } + @Override public void onImageAvailable(ImageReader reader) { - try (Image image = mImageReader.acquireLatestImage()) { - if (image != null && !isRunning) { - Log.d("OCR","running"); - Image.Plane[] planes = image.getPlanes(); - ByteBuffer buffer = planes[0].getBuffer(); - int pixelStride = planes[0].getPixelStride(); - int rowStride = planes[0].getRowStride(); - int rowPadding = rowStride - pixelStride * mWidth; - - isRunning = true; - - // Create full bitmap - int fullWidth = mWidth + rowPadding / pixelStride; - int fullHeight = mHeight; - Bitmap fullBitmap = Bitmap.createBitmap(fullWidth, fullHeight, Bitmap.Config.ARGB_8888); - fullBitmap.copyPixelsFromBuffer(buffer); - - // Calculate the region of interest (last 100 pixels) - int roiHeight = Math.min(100, fullHeight); - int roiY = fullHeight - roiHeight; - - // Create a new bitmap for the region of interest - Bitmap roiBitmap = Bitmap.createBitmap(fullBitmap, 0, 0, fullWidth, 300); - - // Recycle the full bitmap as we no longer need it - fullBitmap.recycle(); + Log.d(TAG, "onImageAvailable called"); + + // If already processing, acquire and discard the image + if (isRunning) { + Log.d(TAG, "System busy - acquiring and discarding latest image"); + Image skippedImage = mImageReader.acquireLatestImage(); + if (skippedImage != null) { + skippedImage.close(); + Log.d(TAG, "Skipped image closed"); + } + return; + } + + final BitmapHolder bitmapHolder = new BitmapHolder(); + Image image = null; + + try { + // Acquire the most recent image + image = mImageReader.acquireLatestImage(); + if (image == null) { + Log.e(TAG, "Acquired image is null"); + return; + } - // Use roiBitmap for OCR - InputImage inputImage = InputImage.fromBitmap(roiBitmap, 0); + isRunning = true; + Log.d(TAG, "Starting image processing"); + + // Set timeout + timeoutHandler.postDelayed(new Runnable() { + @Override + public void run() { + if (isRunning) { + isRunning = false; + bitmapHolder.recycleBitmaps(); + Log.w(TAG, "Processing timeout reached - forcing reset"); + } + } + }, PROCESSING_TIMEOUT_MS); + + // Get image data + Image.Plane[] planes = image.getPlanes(); + ByteBuffer buffer = planes[0].getBuffer(); + int pixelStride = planes[0].getPixelStride(); + int rowStride = planes[0].getRowStride(); + int rowPadding = rowStride - pixelStride * mWidth; + + // Create full bitmap + int fullWidth = mWidth + rowPadding / pixelStride; + int fullHeight = mHeight; + Log.d(TAG, "Creating bitmap with dimensions: " + fullWidth + "x" + fullHeight); + + bitmapHolder.fullBitmap = Bitmap.createBitmap(fullWidth, fullHeight, Bitmap.Config.ARGB_8888); + bitmapHolder.fullBitmap.copyPixelsFromBuffer(buffer); - Task result = recognizer.process(inputImage) + // Create ROI + int roiHeight = Math.min(300, fullHeight); + Log.d(TAG, "Creating ROI bitmap with height: " + roiHeight); + + bitmapHolder.roiBitmap = Bitmap.createBitmap(bitmapHolder.fullBitmap, 0, 0, fullWidth, roiHeight); + bitmapHolder.fullBitmap.recycle(); + Log.d(TAG, "Full bitmap recycled after ROI creation"); + + // Process OCR + InputImage inputImage = InputImage.fromBitmap(bitmapHolder.roiBitmap, 0); + Log.d(TAG, "Starting OCR processing"); + + recognizer.process(inputImage) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Text result) { - Log.d("OCR","processed"); - // Process OCR result as before - String resultText = result.getText(); - lastText = resultText; - lastTextExtended = ""; - for (Text.TextBlock block : result.getTextBlocks()) { - String blockText = block.getText(); - Rect blockFrame = block.getBoundingBox(); - // Adjust the Y coordinate of the bounding box - if (blockFrame != null) { - blockFrame.offset(0, roiY); + Log.d(TAG, "OCR processing successful"); + try { + String resultText = result.getText(); + lastText = resultText; + lastTextExtended = ""; + + for (Text.TextBlock block : result.getTextBlocks()) { + String blockText = block.getText(); + Rect blockFrame = block.getBoundingBox(); + if (blockFrame != null) { + blockFrame.offset(0, 0); + } + lastTextExtended += blockText + "$$" + blockFrame.toString() + "§§"; } - lastTextExtended += blockText + "$$" + blockFrame.toString() + "§§"; + + Log.d(TAG, "Processed text length: " + resultText.length()); + Log.d(TAG, "Number of text blocks: " + result.getTextBlocks().size()); + } catch (Exception e) { + Log.e(TAG, "Error processing OCR result", e); } - roiBitmap.recycle(); - isRunning = false; } }) .addOnFailureListener(new OnFailureListener() { @Override - public void onFailure(Exception e) { + public void onFailure(@NonNull Exception e) { + Log.e(TAG, "OCR processing failed", e); + } + }) + .addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + Log.d(TAG, "OCR task completed"); + timeoutHandler.removeCallbacksAndMessages(null); isRunning = false; + bitmapHolder.recycleBitmaps(); } }); + + } catch (Exception e) { + Log.e(TAG, "Error in image processing", e); + isRunning = false; + if (bitmapHolder != null) { + bitmapHolder.recycleBitmaps(); + } + } finally { + if (image != null) { + image.close(); + Log.d(TAG, "Image closed"); } - } catch (Exception e) { - e.printStackTrace(); - isRunning = false; } } } From b495dbe91a4c9a68e08097a934eac61b238e9789 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Thu, 5 Dec 2024 08:12:43 +0100 Subject: [PATCH 11/20] Update QZService.java --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 0e116536..071e1b6f 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -186,7 +186,7 @@ public String[] getOCR() { String[] lines = t.split("\\$\\$|\\n"); for (int i = 1; i < lines.length; i++) { - Log.d("OCRlines", i + " " + lines[i]); + writeLog("OCRlines " + i + " " + lines[i]); if (lines[i].toLowerCase().contains("incline")) { try { QZService.lastInclination = "Changed Grade " + lines[i-1].trim(); @@ -605,5 +605,5 @@ public static String pickLatestFileFromDownloadsInternal(String path) { private static void writeLog(String command) { MainActivity.writeLog(command); Log.i(LOG_TAG, command); - } + } } From 5099b18acd0755479cd4949b05642e949b958bcf Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Mon, 9 Dec 2024 09:33:03 +0100 Subject: [PATCH 12/20] sending ocr lines over udp to debug them --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 71f3b3e7..35aa57e7 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -513,9 +513,8 @@ public void sendBroadcast(String messageStr) { byte[] sendData = messageStr.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, getBroadcastAddress(), this.clientPort); socket.send(sendPacket); - writeLog("sendBroadcast " + messageStr); } catch (IOException e) { - writeLog("IOException: " + e.getMessage()); + Log.e(LOG_TAG, "IOException: " + e.getMessage()); } } InetAddress getBroadcastAddress() throws IOException { @@ -607,5 +606,6 @@ public static String pickLatestFileFromDownloadsInternal(String path) { private static void writeLog(String command) { MainActivity.writeLog(command); Log.i(LOG_TAG, command); + sendBroadcast(command); } } From 0dfbed3e5b2c8894eba9bebccbb090da19f84785 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Mon, 9 Dec 2024 09:38:36 +0100 Subject: [PATCH 13/20] fixing socket open for sendbroadcast --- .../QZService.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 35aa57e7..42ca2f7c 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -26,10 +26,10 @@ public class QZService extends Service { int startMode; // indicates how to behave if the service is killed IBinder binder; // interface for clients that bind boolean allowRebind; // indicates whether onRebind should be used - int clientPort = 8002; + static int clientPort = 8002; Handler handler = new Handler(); Runnable runnable = null; - DatagramSocket socket = null; + static DatagramSocket socket = null; byte[] lmessage = new byte[1024]; DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length); @@ -240,9 +240,6 @@ public String[] getOCR() { } } try { - socket = new DatagramSocket(); - socket.setBroadcast(true); - if(!QZService.lastSpeed.equals("")) sendBroadcast(QZService.lastSpeed); if(!QZService.lastInclination.equals("")) @@ -257,7 +254,6 @@ public String[] getOCR() { ex.printStackTrace(); return result; } - socket.close(); handler.postDelayed(runnable, 100); return result; } @@ -504,7 +500,10 @@ else if(UDPListenerService.device == UDPListenerService._device.t75s) { handler.postDelayed(runnable, 100); } - public void sendBroadcast(String messageStr) { + public static void sendBroadcast(String messageStr) { + socket = new DatagramSocket(); + socket.setBroadcast(true); + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); @@ -516,6 +515,8 @@ public void sendBroadcast(String messageStr) { } catch (IOException e) { Log.e(LOG_TAG, "IOException: " + e.getMessage()); } + + socket.close(); } InetAddress getBroadcastAddress() throws IOException { WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); From e62faa4a0ea8c2679421391f38489b38e90f3540 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Mon, 9 Dec 2024 09:41:40 +0100 Subject: [PATCH 14/20] Update QZService.java --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 42ca2f7c..b3587e90 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -518,7 +518,7 @@ public static void sendBroadcast(String messageStr) { socket.close(); } - InetAddress getBroadcastAddress() throws IOException { + static InetAddress getBroadcastAddress() throws IOException { WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = null; try { From a2c3d68aebd1668af81eb8509c0bf52f71d5f72c Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Mon, 9 Dec 2024 09:54:46 +0100 Subject: [PATCH 15/20] fixing build --- .../QZService.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index b3587e90..358af3cf 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -30,6 +30,7 @@ public class QZService extends Service { Handler handler = new Handler(); Runnable runnable = null; static DatagramSocket socket = null; + static InetAddress broadcastAddress; byte[] lmessage = new byte[1024]; DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length); @@ -58,6 +59,11 @@ public class QZService extends Service { @Override public void onCreate() { + try { + broadcastAddress = getBroadcastAddress(); + } catch (Exception e) { + Log.e(LOG_TAG, e.toString()); + } // The service is being created //Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show(); writeLog( "Service onCreate"); @@ -501,24 +507,26 @@ else if(UDPListenerService.device == UDPListenerService._device.t75s) { } public static void sendBroadcast(String messageStr) { - socket = new DatagramSocket(); - socket.setBroadcast(true); - - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); - StrictMode.setThreadPolicy(policy); try { + socket = new DatagramSocket(); + socket.setBroadcast(true); + + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); + StrictMode.setThreadPolicy(policy); byte[] sendData = messageStr.getBytes(); - DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, getBroadcastAddress(), this.clientPort); + DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcastAddress, clientPort); socket.send(sendPacket); + socket.close(); } catch (IOException e) { Log.e(LOG_TAG, "IOException: " + e.getMessage()); } - socket.close(); + } - static InetAddress getBroadcastAddress() throws IOException { + + InetAddress getBroadcastAddress() throws IOException { WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = null; try { From 62142931404f1e7b221c5d2855e59e9ba52b45d2 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Wed, 18 Dec 2024 12:23:36 +0100 Subject: [PATCH 16/20] cadence filter by ocr --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 358af3cf..0d312a1a 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -207,7 +207,8 @@ public String[] getOCR() { } if (lines[i].toLowerCase().contains("cadence")) { try { - QZService.lastCadence = "Changed RPM " + lines[i-1].trim(); + if(Integer.parseInt(lines[i-1].trim()) > 30 && Integer.parseInt(lines[i-1].trim()) < 120) + QZService.lastCadence = "Changed RPM " + lines[i-1].trim(); } catch (Exception e) { QZService.lastCadence = ""; } From 689df1bbf509212dcc848c58130c8540914a7f86 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Thu, 12 Dec 2024 08:46:55 +0100 Subject: [PATCH 17/20] debuglog setting --- .../MainActivity.java | 12 ++++++++++++ .../QZService.java | 15 +++++++++++---- app/src/main/res/layout/activity_main.xml | 6 ++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/MainActivity.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/MainActivity.java index e9b83a8a..3681f7c4 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/MainActivity.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/MainActivity.java @@ -23,6 +23,7 @@ import android.util.Log; import android.view.View; import android.widget.Button; +import android.widget.CheckBox; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; @@ -204,6 +205,16 @@ protected void onCreate(Bundle savedInstanceState) { sharedPreferences = getSharedPreferences("QZ",MODE_PRIVATE); radioGroup = findViewById(R.id.radiogroupDevice); + CheckBox debugLog = findViewById(R.id.debuglog); + + debugLog.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + SharedPreferences.Editor myEdit = sharedPreferences.edit(); + myEdit.putBoolean("debugLog", debugLog.isChecked()); + myEdit.commit(); + } + }); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override @@ -299,6 +310,7 @@ public void onCheckedChanged(RadioGroup radioGroup, int i) { }); int device = sharedPreferences.getInt("device", R.id.other); + debugLog.setChecked(sharedPreferences.getBoolean("debugLog", true)); RadioButton radioButton; radioButton = findViewById(device); if(radioButton != null) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 0d312a1a..b0be5d08 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -3,6 +3,7 @@ import android.app.Service; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.net.DhcpInfo; import android.net.wifi.WifiManager; import android.os.Handler; @@ -50,6 +51,7 @@ public class QZService extends Service { static String lastResistance = ""; String lastGear = ""; static String lastHeart = ""; + static SharedPreferences sharedPreferences; static boolean ifit_v2 = false; @@ -59,6 +61,8 @@ public class QZService extends Service { @Override public void onCreate() { + + sharedPreferences = getSharedPreferences("QZ",MODE_PRIVATE); try { broadcastAddress = getBroadcastAddress(); } catch (Exception e) { @@ -614,8 +618,11 @@ public static String pickLatestFileFromDownloadsInternal(String path) { } private static void writeLog(String command) { - MainActivity.writeLog(command); - Log.i(LOG_TAG, command); - sendBroadcast(command); - } + + if(sharedPreferences.getBoolean("debugLog", true)) { + MainActivity.writeLog(command); + Log.i(LOG_TAG, command); + sendBroadcast(command); + } + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 1ad425b2..183bcc36 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -32,6 +32,12 @@ android:focusable="true" android:longClickable="true" android:typeface="serif" /> + + Date: Wed, 18 Dec 2024 12:32:43 +0100 Subject: [PATCH 18/20] Update QZService.java --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 49b5d99a..e36a8b06 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -214,7 +214,7 @@ public String[] getOCR() { String potentialNumber = lines[i-1].trim(); // Try to parse the number to check if it's valid Double.parseDouble(potentialNumber); - if(potentialNumber > 30 && potentialNumber < 120) + if(potentialNumber > 30.0 && potentialNumber < 120.0) QZService.lastCadence = "Changed RPM " + potentialNumber; } catch (Exception e) { // If lines[i-1] isn't a number, try lines[i-2] From c0f60a63ea7b23690c4cf88c7e37f158041eac78 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Wed, 18 Dec 2024 12:35:41 +0100 Subject: [PATCH 19/20] Update QZService.java --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index e36a8b06..4d0fb5d9 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -213,8 +213,8 @@ public String[] getOCR() { try { String potentialNumber = lines[i-1].trim(); // Try to parse the number to check if it's valid - Double.parseDouble(potentialNumber); - if(potentialNumber > 30.0 && potentialNumber < 120.0) + Double dd = Double.parseDouble(potentialNumber); + if(dd > 30.0 && dd < 120.0) QZService.lastCadence = "Changed RPM " + potentialNumber; } catch (Exception e) { // If lines[i-1] isn't a number, try lines[i-2] From 808c9484729866bf05b45f80dd802f4e0c6ea7f7 Mon Sep 17 00:00:00 2001 From: Roberto Viola Date: Thu, 26 Dec 2024 15:48:21 +0100 Subject: [PATCH 20/20] Filtering strange wattage reading https://github.com/cagnulein/qdomyos-zwift/issues/2928#issuecomment-2562845999 --- .../cagnulein/qzcompanionnordictracktreadmill/QZService.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java index 4d0fb5d9..5c66cc38 100644 --- a/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java +++ b/app/src/main/java/org/cagnulein/qzcompanionnordictracktreadmill/QZService.java @@ -245,7 +245,9 @@ public String[] getOCR() { String[] numbers = numberStr.split("\\s+"); if (numbers.length > 0) { int watts = Integer.parseInt(numbers[numbers.length - 1]); - QZService.lastWattage = "Changed Watts " + watts; + if(watts > 20) { // filtering wrong reading from OCR + QZService.lastWattage = "Changed Watts " + watts; + } } } catch (Exception e) { }