HPlus: Fix Unicode handling

This commit is contained in:
João Paulo Barraca 2017-05-22 23:14:05 +01:00
parent 845869e25e
commit 759b9c81a3
2 changed files with 5 additions and 1 deletions

View File

@ -289,6 +289,6 @@ public class HPlusCoordinator extends AbstractDeviceCoordinator {
} }
public static boolean getUnicodeSupport(String address){ public static boolean getUnicodeSupport(String address){
return (prefs.getBoolean(HPlusConstants.PREF_HPLUS_UNICODE, false)); return (prefs.getBoolean(HPlusConstants.PREF_HPLUS_UNICODE + "_" + address, false));
} }
} }

View File

@ -808,6 +808,8 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
private byte[] encodeStringToDevice(String s) { private byte[] encodeStringToDevice(String s) {
List<Byte> outBytes = new ArrayList<Byte>(); List<Byte> outBytes = new ArrayList<Byte>();
Boolean unicode = HPlusCoordinator.getUnicodeSupport(this.gbDevice.getAddress());
LOG.info("Encode String: Unicode=" + unicode);
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i); Character c = s.charAt(i);
@ -818,12 +820,14 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
} else { } else {
try { try {
if(HPlusCoordinator.getUnicodeSupport(this.gbDevice.getAddress())) if(HPlusCoordinator.getUnicodeSupport(this.gbDevice.getAddress()))
if(unicode)
cs = c.toString().getBytes("Unicode"); cs = c.toString().getBytes("Unicode");
else else
cs = c.toString().getBytes("GB2312"); cs = c.toString().getBytes("GB2312");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
//Fallback. Result string may be strange, but better than nothing //Fallback. Result string may be strange, but better than nothing
cs = c.toString().getBytes(); cs = c.toString().getBytes();
LOG.error("Could not convert String to Bytes: " + e.getMessage());
} }
} }
for (int j = 0; j < cs.length; j++) for (int j = 0; j < cs.length; j++)