蓝牙名称显示问题:
如果你检查蓝牙适配器 setName(),你会得到那个
https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#setName
使用 UTF-8 编码的有效蓝牙名称最长为 248 字节,
虽然很多远程设备只能显示前 40 个字符,
有些可能仅限于 20 个。
Android 支持的版本:
如果您检查链接https://stackoverflow.com/a/7989085/2293534,您将获得android支持的版本列表。
-----------------------------------------------------------------------------------------------------
| DEC Korean | Korean EUC | ISO-2022-KR | KSC5601/cp949 | UCS-2/UTF-16 | UCS-4 | UTF-8 |
-----------------------------------------------------------------------------------------------------
DEC Korean | - | Y | N | Y | Y | Y | Y |
-----------------------------------------------------------------------------------------------------
Korean EUC | Y | - | Y | N | N | N | N |
-----------------------------------------------------------------------------------------------------
ISO-2022-KR | N | Y | - | Y | N | N | N |
-----------------------------------------------------------------------------------------------------
KSC5601/cp949| Y | N | Y | - | Y | Y | Y |
-----------------------------------------------------------------------------------------------------
UCS-2/UTF-16| Y | N | N | Y | - | Y | Y |
-----------------------------------------------------------------------------------------------------
UCS-4 | Y | N | N | Y | Y | - | Y |
-----------------------------------------------------------------------------------------------------
UTF-8 | Y | N | N | Y | Y | Y | - |
-----------------------------------------------------------------------------------------------------
对于解决方案,
解决方案#1:
Michael 为转换提供了一个很好的例子。更多内容可以查看https://stackoverflow.com/a/40070761/2293534
当你调用 getBytes() 时,你得到的是字符串的原始字节
在系统的本机字符编码下编码(可能或
可能不是 UTF-8)。然后,您将这些字节视为
以 UTF-8 编码,它们可能不是。
更可靠的方法是将 ko_KR-euc 文件读入
Java 字符串。然后,使用 UTF-8 编码写出 Java 字符串。
InputStream in = ...
Reader reader = new InputStreamReader(in, "ko_KR-euc"); // you can use specific korean locale here
StringBuilder sb = new StringBuilder();
int read;
while ((read = reader.read()) != -1){
sb.append((char)read);
}
reader.close();
String string = sb.toString();
OutputStream out = ...
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.write(string);
writer.close();
注意:当然,您应该使用正确的编码名称
解决方案#2:
使用StringUtils,你可以做到
https://stackoverflow.com/a/30170431/2293534
解决方案#3:
您可以使用 Apache Commons IO 进行转换。这里给出了一个很好的例子:http://www.utdallas.edu/~lmorenoc/research/icse2015/commons-io-2.4/examples/toString_49.html
1 String resource;
2 //getClass().getResourceAsStream(resource) -> the <code>InputStream</code> to read from
3 //"UTF-8" -> the encoding to use, null means platform default
4 IOUtils.toString(getClass().getResourceAsStream(resource),"UTF-8");
资源链接:
- Korean Codesets and Codeset Conversion
- Korean Localization
- Changing the Default Locale
- Byte Encodings and Strings