下面是工作示例:public void bluetoothSendData(String text){
bluetooth_adapter.cancelDiscovery();
if (socket_connected) {
try {
OutputStream o_stream = socket.getOutputStream();
o_stream.write(decodeText(text,"CP1251"));
Log.i("emi","Data was sended.");
} catch (IOException e) {
bluetoothCloseConnection();
Log.i("emi","Send data error:" + e);
}
} else {
Log.i("emi","Bluetooth device not connected.");
}
}
private byte[] decodeText(String text, String encoding) throws CharacterCodingException, UnsupportedEncodingException{
Charset charset = Charset.forName(encoding);
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(text));
CharBuffer cbuf = decoder.decode(bbuf);
String s = cbuf.toString();
return s.getBytes(encoding);
}
我如何 understend,这个examle将在字体中工作,从操作系统加载的编码 CP1251.