Even Realities G1: Support non-latin charaters in notifications (#5562)

This properly encodes the notification json as UTF-8 instead of ASCII so
that other characters are correctly sent.

Also fix a null pointer in logging in the BLEQueue
This commit is contained in:
jrthomas270
2025-11-22 23:19:15 +01:00
committed by José Rebelo
parent c9d2e52ac1
commit d8386d5e4f
2 changed files with 4 additions and 11 deletions
@@ -414,7 +414,8 @@ public final class BtLEQueue implements Thread.UncaughtExceptionHandler {
}
if (forceDisconnect) {
LOG.warn("unhealthy disconnect {} {}", mBluetoothGatt.getDevice().getAddress(),
BluetoothDevice device = mBluetoothGatt.getDevice();
LOG.warn("unhealthy disconnect {} {}", device == null ? "<UNKNOWN>" : device.getAddress(),
BleNamesResolver.getStatusString(status));
} else if (mBluetoothGatt != null) {
// try to reconnect immediately
@@ -913,7 +913,6 @@ public class G1Communications {
}
private static byte[] generatePayload(NotificationSpec notificationSpec) {
try {
JSONObject notificationJson = new JSONObject();
notificationJson.put("msg_id", notificationSpec.getId());
@@ -933,14 +932,7 @@ public class G1Communications {
JSONObject json = new JSONObject();
json.put("ncs_notification", notificationJson);
// Need to allocate one larger in order to null terminate.
String jsonString = json.toString();
byte[] bytes = new byte[jsonString.length() + 1];
System.arraycopy(jsonString.getBytes(StandardCharsets.US_ASCII),
0, bytes, 0, jsonString.length());
bytes[jsonString.length()] = 0;
return bytes;
return json.toString().getBytes(StandardCharsets.UTF_8);
} catch (JSONException e) {
throw new RuntimeException(e);
}
@@ -1033,7 +1025,7 @@ public class G1Communications {
}
public static String getMessage(byte[] payload) {
return new String(payload, 1, payload.length-2, StandardCharsets.US_ASCII);
return new String(payload, 1, payload.length-2, StandardCharsets.UTF_8);
}
}