Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReOpen PR Support for nameless OKOK scales (Myria MY4836) #1088

Merged
merged 2 commits into from
Dec 4, 2024

Conversation

oliexdev
Copy link
Owner

Reopen PR #1081

@TRex22
Copy link

TRex22 commented Nov 17, 2024

So I've been investigating the Bluetooth payload... We could possibly use the manufacturer details or even look for a weight value and only display devices which respond with that data.

Would need to change the code which scans for new devices and might make pairing a little janky

@oliexdev
Copy link
Owner Author

thanks @TRex22 best think would be to have some manufactur id for filtering

@TRex22
Copy link

TRex22 commented Nov 22, 2024

So Im not 100% that all scales have that ID set. I believe the one I have is empty 🤦🏻
Maybe a simple solution is a check box option in the bluetooth settings (by default off) which enables listing devices with no names. That way People with these devices can use that behaviour and by default those scales + other devices would be hidden

@oliexdev
Copy link
Owner Author

That will only help more experience users as normally you don't know the Bluetooth address of the scale.
Maybe we can find out how the vendor app does the filtering?

@Florin9doi
Copy link
Contributor

The filtering logic is known, it just needs to be integrated with BluetoothSettingsFragment:

b.setManufacturerData((i << 8) | 0xc0, data, mask);
filters.add(b.build());
}
central.scanForPeripheralsUsingFilters(filters);

for (int i = 0; i < manufacturerSpecificData.size(); i++) {
int vendorId = manufacturerSpecificData.keyAt(i);
if ((vendorId & 0xff) == 0xc0) { // 0x00c0-->0xffc0
vendorIndex = vendorId;
break;
}
}
if (vendorIndex == -1) {
return;
}
byte[] data = manufacturerSpecificData.get(vendorIndex);
StringBuilder sb = new StringBuilder(data.length * 3);
for (byte b : data) {
sb.append(String.format("%02x ", b));
}
Timber.d("manufacturerSpecificData: [VID=%04x] %s", vendorIndex, sb.toString());
if (data[IDX_MAC_1] != (byte) ((Character.digit(mMacAddress.charAt(0), 16) << 4) + Character.digit(mMacAddress.charAt(1), 16))
|| data[IDX_MAC_2] != (byte) ((Character.digit(mMacAddress.charAt(3), 16) << 4) + Character.digit(mMacAddress.charAt(4), 16))
|| data[IDX_MAC_3] != (byte) ((Character.digit(mMacAddress.charAt(6), 16) << 4) + Character.digit(mMacAddress.charAt(7), 16))
|| data[IDX_MAC_4] != (byte) ((Character.digit(mMacAddress.charAt(9), 16) << 4) + Character.digit(mMacAddress.charAt(10), 16))
|| data[IDX_MAC_5] != (byte) ((Character.digit(mMacAddress.charAt(12), 16) << 4) + Character.digit(mMacAddress.charAt(13), 16))
|| data[IDX_MAC_6] != (byte) ((Character.digit(mMacAddress.charAt(15), 16) << 4) + Character.digit(mMacAddress.charAt(16), 16)))
return;

@oliexdev
Copy link
Owner Author

I created a new branch https://github.com/oliexdev/openScale/tree/MY4836 (sorry don't know how to pushed it into this PR) with a potential solution. I would be happy if somebody could test it as I don't own this kind of nameless scale. The basic idea is to give a nameless scale a fake name which can be used later for identification, see the commit c17fff3.

int vendorIndex = -1;
for (int i = 0; i < manufacturerSpecificData.size(); i++) {
int vendorId = manufacturerSpecificData.keyAt(i);
if ((vendorId & 0xff) == 0xc0) { // 0x00c0-->0xffc0
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the 0xc0 the identifier for the scale?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ID is 0x??c0 (0x00c0, 0x01c0, 0x02c0.... ) where the MSB is used to notify the progress, it starts from 0 and increases until the measurement is stable

@Florin9doi
Copy link
Contributor

Florin9doi commented Nov 24, 2024

I created a new branch https://github.com/oliexdev/openScale/tree/MY4836 (sorry don't know how to pushed it into this PR) with a potential solution. I would be happy if somebody could test it as I don't own this kind of nameless scale. The basic idea is to give a nameless scale a fake name which can be used later for identification, see the commit c17fff3.

I had to do some changes to work:

  • return if deviceName is still null
  • Update deviceView with the new name
  • Save the new name in SharedPreferences
diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java
index 7bea7c1c..214ba506 100644
--- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java
+++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java
@@ -322,14 +322,19 @@ public class BluetoothSettingsFragment extends Fragment {
             return;
         }
 
-        BluetoothDeviceView deviceView = new BluetoothDeviceView(context);
-        deviceView.setDeviceName(formatDeviceName(bleScanResult.getDevice()));
-
         String deviceName = device.getName();
         if (deviceName == null) {
             deviceName = BluetoothFactory.convertNoNameToDeviceName(bleScanResult.getScanRecord().getManufacturerSpecificData());
+        }
+        if (deviceName == null) {
+            return;
         }
 
+        BluetoothDeviceView deviceView = new BluetoothDeviceView(context);
+        deviceView.setDeviceName(formatDeviceName(deviceName, device.getAddress()));
+        deviceView.setAlias(deviceName);
+
         BluetoothCommunication btDevice = BluetoothFactory.createDeviceDriver(context, deviceName);
         if (btDevice != null) {
             Timber.d("Found supported device %s (driver: %s)",
@@ -398,6 +403,7 @@ public class BluetoothSettingsFragment extends Fragment {
         private TextView deviceName;
         private ImageView deviceIcon;
         private String deviceAddress;
+        private String deviceAlias;
 
         public BluetoothDeviceView(Context context) {
             super(context);
@@ -439,6 +445,14 @@ public class BluetoothSettingsFragment extends Fragment {
             deviceName.setText(name);
         }
 
+        public void setAlias(String alias) {
+            deviceAlias = alias;
+        }
+
+        public String getAlias() {
+            return deviceAlias;
+        }
+
         public void setSummaryText(String text) {
             SpannableStringBuilder stringBuilder = new SpannableStringBuilder(new String());
 
@@ -485,10 +499,10 @@ public class BluetoothSettingsFragment extends Fragment {
 
             prefs.edit()
                     .putString(PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, device.getAddress())
-                    .putString(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, device.getName())
+                    .putString(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, getAlias())
                     .apply();
 
-            Timber.d("Saved Bluetooth device " + device.getName() + " with address " + device.getAddress());
+            Timber.d("Saved Bluetooth device " + getAlias() + " with address " + device.getAddress());
 
             stopBluetoothDiscovery();

oliexdev added a commit that referenced this pull request Nov 29, 2024
@oliexdev
Copy link
Owner Author

Thanks @Florin9doi I applied the patch with b232995

Does this version work now with the nameless OkOk scale?

@TRex22
Copy link

TRex22 commented Nov 29, 2024

If you'd like I can generate an APK on my side and test? ... probably only over the weekend when I find some free time.

@oliexdev
Copy link
Owner Author

@TRex22 That would be great as I don't own a nameless OkOK scale. You could also use the apk build at https://github.com/oliexdev/openScale/releases/tag/dev-build if you want

@Florin9doi
Copy link
Contributor

Thanks @Florin9doi I applied the patch with b232995

Does this version work now with the nameless OkOk scale?

It works, this is how it looks :
image

@Florin9doi
Copy link
Contributor

These 2 comments can be removed:

// TODO: verify setAdvertisingDataTypeWithData on API33+
// b.setAdvertisingDataTypeWithData(ScanRecord.DATA_TYPE_MANUFACTURER_SPECIFIC_DATA, data, mask);

I tested on a S24U with Android 14 and doesn't work.

oliexdev added a commit that referenced this pull request Dec 4, 2024
@oliexdev
Copy link
Owner Author

oliexdev commented Dec 4, 2024

thanks for testing, I will merge it

@oliexdev oliexdev merged commit 9df5066 into oliexdev:master Dec 4, 2024
@Florin9doi
Copy link
Contributor

Are you sure you merged the correct branch? I can't find in master the latest fixes from https://github.com/oliexdev/openScale/tree/MY4836

@oliexdev
Copy link
Owner Author

oliexdev commented Dec 4, 2024

you are absolutely right, I hope I pushed the right one now, could you test it maybe again?

@Florin9doi Florin9doi deleted the MY4836 branch December 4, 2024 18:48
@Florin9doi
Copy link
Contributor

I'll test later this week

@Florin9doi
Copy link
Contributor

I tested the latest dev build (https://github.com/oliexdev/openScale/releases/tag/dev-build) and it is ok.
Thank you

@oliexdev
Copy link
Owner Author

oliexdev commented Dec 7, 2024

Thanks for testing and your great PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants