将土耳其语字符打印到Android打印机

时间:2016-03-25 21:43:35

标签: java android printing utf-8 bluetooth

我正在编写一个连接蓝牙打印机并打印支票的Android应用程序。我正在使用Sewoo LK-P20移动打印机。问题是,当我向打印机发送数字和ASCII字符时,一切都很好。但是当我发送像ÇÇŞĞÖI这样的土耳其字符时,打印机会打印出奇怪的字符。我已经尝试了以下所有

"ÜŞÇÖĞüşçöğı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");

但结果是一样的。我得到了编码字节码并搜索了当前字符utf-8字节码。所以字符“ÜÇŞĞÖ”的utf-8字节代码是“c39cc387c59ec49ec396”并且根据link 编码是正确的,但打印机打印不同的字符。在公司里,我的同事使用PDA。操作系统是PDA中的Windows,当打印到同一台打印机时,他们不会抱怨土耳其语字符编码。所以我的代码在

之下
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);

    }
}

我的UnicodeFormatter类

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);
    }

}

任何帮助将不胜感激。在此先感谢。

0 个答案:

没有答案