2014-12-19 161 views
0

当我使用连接到蓝牙设备显示为mBTSocket.connect();如何连接Buletooth插座蓝牙打印机打印数据

错误信息

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

请帮帮我。

回答