I am writing an Android app which connects bluetooth printer and prints the check. I am using Sewoo LK-P20 Mobile Printer. The issue is that when i am sending numbers and ASCII characters to printer everything is fine. But when i am sending Turkish characters like ÜÇŞĞÖI the printer prints strange characters. I have tried all below
"ÜŞÇÖĞüşçöğıI".getBytes("UTF-8");
Charset.forName("UTF-8").encode("ÜŞÇÖĞüşçöğıI").array();
URLEncoder.encode("ÜŞÇÖĞüşçöğıI", "UTF-8").getBytes("UTF-8");
String str = new String("ÜŞÇÖĞüşçöğıI".getBytes("UTF-8"), "UTF-8");
String str = new String("ÜŞÇÖĞüşçöğıI".getBytes("ISO-8859-1"), "UTF-8");
"ÜŞÇÖĞüşçöğıI".toUpperCase(new Locale("tr", "TR")).getBytes("UTF-8");
But The result is the same. I got the Encoded byte codes and searched for the current character utf-8 byte code. So the utf-8 byte code for the character "ÜÇŞĞÖ" was "c39cc387c59ec49ec396" and according to the link the encoding is correct but the printer prints different characters. In the company my coworkers uses PDA. The operating system is Windows in the PDA and they do not complain about Turkish Character Encoding when the print to the same printer. So My code is below
public class BluetoothConnectionThread extends AsyncTask<String, Integer, String>{
private BluetoothSocket bluetoothSocket;
private OutputStream outputStream;
private InputStream inputStream;
private Context context;
private ServiceInterface serviceInterface;
private ProgressDialog progressDialog;
private ClassProducts classProducts;
private ArrayList<ClassProducts> productsArrayList;
public BluetoothConnectionThread(BluetoothDevice bluetoothDevice, ClassProducts classProducts, ArrayList<ClassProducts> productsArrayList, Context context, ServiceInterface serviceInterface){
this.serviceInterface = serviceInterface;
this.progressDialog = new ProgressDialog(context);
this.progressDialog.setMessage(context.getString(R.string.writting));
this.progressDialog.setCancelable(false);
this.classProducts = classProducts;
this.productsArrayList = productsArrayList;
try {
bluetoothDevice.fetchUuidsWithSdp();
UUID uuid = bluetoothDevice.getUuids()[0].getUuid();
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
inputStream = bluetoothSocket.getInputStream();
outputStream = bluetoothSocket.getOutputStream();
this.context = context;
} catch (IOException e) {
Log.e("IO Exception :",e.getMessage());
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
try{
String che = "ÜÇŞĞÖ";
String bytes="";
for(byte b: che.getBytes("UTF-8")){
bytes+=UnicodeFormatter.byteToHex(b);
}
bluetoothSocket.connect();
outputStream.write(getStringFormat().getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
bluetoothSocket.close();
return bytes;
} catch (IOException e) {
Log.e("IO Exception :",e.getMessage());
try {
bluetoothSocket.close();
return e.getMessage();
}catch (IOException e1){
Log.e("IO Exception :", e1.getMessage());
return e.getMessage();
}
}
catch (ParseException e) {
Log.e("IO Exception :",e.getMessage());
return e.getMessage();
}
}
protected String getStringFormat() throws ParseException{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("--------------------------------\r\n")
.append(context.getString(R.string.cari_hesaplar)+" : "+classProducts.getCustomerName()+"\r\n")
.append("--------------------------------\r\n")
.append(context.getString(R.string.sevkiyat_adresleri)+" : "+classProducts.getShipName()+"\r\n")
.append("--------------------------------\r\n")
.append(" "+context.getString(R.string.products)+" \r\n")
.append("--------------------------------\r\n");
for(ClassProducts classProducts: productsArrayList){
stringBuilder.append(context.getString(R.string.productName)+" : "+classProducts.getName()+"\r\n");
stringBuilder.append(context.getString(R.string.unitPrice)+" : "+classProducts.getPrice()+" \u20BA"+"\r\n");
stringBuilder.append(context.getString(R.string.unitName)+" : "+classProducts.getUnit()+"\r\n");
stringBuilder.append(context.getString(R.string.say)+" : "+classProducts.getCount()+"\r\n");
stringBuilder.append("--------------------------------\r\n");
}
Date date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.DEFAULT).parse(classProducts.getDate());
String strDate = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.DEFAULT, new Locale("tr", "TR")).format(date);
stringBuilder.append(context.getString(R.string.date)+" : "+strDate+"\r\n")
.append("--------------------------------\r\n")
.append(context.getString(R.string.totalPrice)+" : "+classProducts.getTotalPrice()+" \u20BA"+"\r\n")
.append("--------------------------------\r\n");
return stringBuilder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
if(s!=null)
serviceInterface.onPost(s);
}
}
And My UnicodeFormatter class
public class UnicodeFormatter {
static public String byteToHex(byte b) {
char hexDigit[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
return new String(array);
}
static public String charToHex(char c) {
byte hi = (byte) (c >>> 8);
byte lo = (byte) (c & 0xff);
return byteToHex(hi) + byteToHex(lo);
}
}
Any help will be appreciated. Thanks in Advance.