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

时间:2014-12-19 05:59:48

标签: java android sockets printing bluetooth

使用mBTSocket.connect();

连接蓝牙设备时

错误消息显示为

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)

我的代码是

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

请帮帮我。

1 个答案:

答案 0 :(得分:1)

试试这里https://stackoverflow.com/a/18786701/2159780。 有一个完整的类,包含一些嵌套类。