简体   繁体   English

如何连接蓝牙插座以在蓝牙打印机中打印数据

[英]How to connect Buletooth socket to print data in bluetooth printer

When i connect to Bluetooth device using mBTSocket.connect(); 当我使用mBTSocket.connect();连接到蓝牙设备时mBTSocket.connect();

error message shown as 错误消息显示为

12-19 11:29:05.328: E/Class(6762): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
12-19 11:29:05.328: E/Class(6762):  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
12-19 11:29:05.328: E/Class(6762):  at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:516)
12-19 11:29:05.328: E/Class(6762):  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
12-19 11:29:05.328: E/Class(6762):  at com.airtimeonline.Sales$12.run(Sales.java:1219)
12-19 11:29:05.328: E/Class(6762):  at java.lang.Thread.run(Thread.java:841)

My Code is 我的代码是

public void printing()
{
    try {
        BILL ="";
            Calendar c = Calendar.getInstance();
             SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
             String currentdate = df.format(c.getTime());
             if(reprintchkbx.isChecked())
             {


                 BILL = BILL + " E RECHARGE"+ " " +currentdate; 
                 BILL = BILL + "\n                                ";
                 BILL = BILL + "\n--------------------------------";
                 BILL = BILL + "\nNetWork      :" + " " + networkprint;
                 BILL = BILL + "\nDenomination :" + " " + denominationprint;
                 BILL = BILL + "\nPin No       :" + " " + pinnoprint;
                 BILL = BILL + "\nSerial No    :" + " " + serialnoprint;
                 BILL = BILL + "\n--------------------------------";
                 BILL = BILL + "\n                                ";
                 BILL = BILL + "\n      www.prepaidzone.co.za"; 
                 BILL = BILL + "\n                                \n\n\n";

             }
             else if(reprintchkbxall.isChecked())
             {


                 for(int i=0; i< Networkprintout.length; i++)
                 {    
                 BILL = BILL + " E RECHARGE"+ " " +currentdate; 
                 BILL = BILL + "\n                                ";
                 BILL = BILL + "\n--------------------------------";
                 BILL = BILL + "\nNetWork      :" + " " + Networkprintout[i];
                 BILL = BILL + "\nDenomination :" + " " + Denominationprintout[i];
                 BILL = BILL + "\nPin No       :" + " " + Pinnoprintout[i];
                 BILL = BILL + "\nSerial No    :" + " " + serialnoprintout[i];
                 BILL = BILL + "\n--------------------------------";
                 BILL = BILL + "\n                                ";
                 BILL = BILL + "\n      www.prepaidzone.co.za"; 
                 BILL = BILL + "\n                                \n\n\n";
                 }

             }
             else
             {
                    for(int i=0; i< Integer.parseInt(Quantity.toString()); i++)
                     {    
                     BILL = BILL + " E RECHARGE"+ " " +currentdate; 
                     BILL = BILL + "\n                                ";
                     BILL = BILL + "\n--------------------------------";
                     BILL = BILL + "\nNetWork      :" + " " + Networkprintout[i];
                     BILL = BILL + "\nDenomination :" + " " + Denominationprintout[i];
                     BILL = BILL + "\nPin No       :" + " " + Pinnoprintout[i];
                     BILL = BILL + "\nSerial No    :" + " " + serialnoprintout[i];
                     BILL = BILL + "\n--------------------------------";
                     BILL = BILL + "\n                                ";
                     BILL = BILL + "\n      www.prepaidzone.co.za"; 
                     BILL = BILL + "\n                                \n\n\n";
                     }
             }

        mBTAdapter = BluetoothAdapter.getDefaultAdapter();
        dialogProgress = new Dialog(Sales.this);

        try {
        if (mBTAdapter.isDiscovering())
        mBTAdapter.cancelDiscovery();
        else
        mBTAdapter.startDiscovery();
        } catch (Exception e) {
        Log.e("Class ", "My Exe ", e);
        }
        System.out.println("BT Searching status :"
        + mBTAdapter.isDiscovering());
        if (mBTAdapter == null) {
        Toast.makeText(Sales.this, "Device has no bluetooth capability",
        Toast.LENGTH_LONG).show();
        finish();
        } else {
        if (!mBTAdapter.isEnabled()) {
        Intent i = new Intent(
        BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(i, 0);
        }

        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(
        BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter); // Don't forget to
        // unregister during
        // onDestroy
        dialogProgress
        .setOnDismissListener(new DialogInterface.OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
        dialog.dismiss();
        setResult(RESULT_CANCELED);
        finish();
        }
        });


        }

        } catch (Exception e) {
        Log.e("Class ", "My Exe ", e);
        }
}

public void printBillToDevice(final String address) {
    new Thread(new Runnable() {

    public void run() {
    runOnUiThread(new Runnable() {

    public void run() {
    }

    });

    mBTAdapter.cancelDiscovery();

    try {
    BluetoothDevice mdevice = mBTAdapter
    .getRemoteDevice(address);
    Method m = mdevice.getClass().getMethod(
    "createRfcommSocket", new Class[] { int.class });
    mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);
    mBTSocket.connect();
    OutputStream os = mBTSocket.getOutputStream();
    os.flush();
    os.write(BILL.getBytes());
    mBTSocket.close();
/*  finish();*/
    } catch (Exception e) {

        Log.e("Class ", "My Exe ", e);
        e.printStackTrace();
        setResult(RESULT_CANCELED);
        finish();

    }

    runOnUiThread(new Runnable() {

    public void run() {
    try {
    dialogProgress.dismiss();
    } catch (Exception e) {
    Log.e("Class ", "My Exe ", e);
    }
    }

    });

    }

    }).start();
}

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {

    try {
    String action = intent.getAction();
    // When discovery finds a device
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
    // Get the BluetoothDevice object from the Intent
    BluetoothDevice device = intent
    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    if (device.getName().equalsIgnoreCase(PRINTER_NAME))    
    {
    mBTAdapter.cancelDiscovery();
    dialogProgress.dismiss();
    Toast.makeText(Sales.this,device.getName() + " Printing Voucher",Toast.LENGTH_LONG).show();
    printBillToDevice(device.getAddress());
    btn_print.setEnabled(true);
    printalert();
    }
    }
    } catch (Exception e) {
    Log.e("Class ", "My Exe ", e);
    }
    }
    };

Please help me. 请帮我。

Try look here https://stackoverflow.com/a/18786701/2159780 . 尝试在这里查看https://stackoverflow.com/a/18786701/2159780 There is a complete class, containing some nested classes. 有一个完整的类,其中包含一些嵌套类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM