How To Print Image And Some Data From An Android Device, Using Printer (print Via Bluetooth)?
Solution 1:
Try using this one....
publicclassBluetoothPrinterActivityextendsActivity {
BluetoothAdapter mBTAdapter;
BluetoothSocketmBTSocket=null;
Dialog dialogProgress;
String BILL, TRANS_ID;
StringPRINTER_MAC_ID="00:1F:B7:02:8F:44";
finalStringERROR_MESSAGE="There has been an error in printing the bill.";
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
BILL = "\nSale Slip No: 12345678" + " " + "04-08-2011\n";
BILL = BILL + "----------------------------------------";
BILL = BILL + "\n\n";
BILL = BILL + "Total Qty:" + " " + "2.0\n";
BILL = BILL + "Total Value:" + " " + "17625.0\n";
BILL = BILL + "-----------------------------------------";
mBTAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBTAdapter == null) {
Toast.makeText(this, "Device has no bluetooth capability",Toast.LENGTH_LONG).show();
finish();
} else {
if (!mBTAdapter.isEnabled()) {
Intenti=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(i, 0);
}
// Register the BroadcastReceiverIntentFilterfilter=newIntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
dialogProgress = newDialog(BluetoothPrinterActivity.this);
dialogProgress.setTitle("Finding printer...");
dialogProgress.setOnDismissListener(newDialogInterface.OnDismissListener() {
publicvoidonDismiss(DialogInterface dialog) {
dialog.dismiss();
setResult(RESULT_CANCELED);
finish();
}
});
dialogProgress.show();
}
if (mBTAdapter.isDiscovering())
mBTAdapter.cancelDiscovery();
else
mBTAdapter.startDiscovery();
System.out.println("BT Searching status :" + mBTAdapter.isDiscovering());
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
}
}
privatefinalBroadcastReceivermReceiver=newBroadcastReceiver() {
publicvoidonReceive(Context context, Intent intent) {
try {
Stringaction= intent.getAction();
// When discovery finds a deviceif (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the IntentBluetoothDevicedevice= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println("***" + device.getName() + " : "+ device.getAddress());
if (device.getAddress().equalsIgnoreCase(PRINTER_MAC_ID)) {
mBTAdapter.cancelDiscovery();
dialogProgress.dismiss();
Toast.makeText(BluetoothPrinterActivity.this,device.getName() + " Printing data",Toast.LENGTH_LONG).show();
printBillToDevice(PRINTER_MAC_ID);
Toast.makeText(BluetoothPrinterActivity.this,device.getName() + " found", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
}
}
};
@OverrideprotectedvoidonDestroy() {
super.onDestroy();
try {
if (dialogProgress != null)
dialogProgress.dismiss();
if (mBTAdapter != null)
mBTAdapter.cancelDiscovery();
this.unregisterReceiver(mReceiver);
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
}
}
@OverridepublicvoidonBackPressed() {
try {
if (mBTAdapter != null)
mBTAdapter.cancelDiscovery();
this.unregisterReceiver(mReceiver);
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
}
setResult(RESULT_CANCELED);
finish();
}
publicvoidprintBillToDevice(final String address) {
newThread(newRunnable() {
publicvoidrun() {
runOnUiThread(newRunnable() {
publicvoidrun() {
dialogProgress.setTitle("Connecting...");
dialogProgress.show();
}
});
mBTAdapter.cancelDiscovery();
try {
System.out.println("**************************#****connecting");
BluetoothDevicemdevice= mBTAdapter.getRemoteDevice(address);
Methodm= mdevice.getClass().getMethod("createRfcommSocket", newClass[] { int.class });
mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1);
mBTSocket.connect();
OutputStreamos= mBTSocket.getOutputStream();
os.flush();
os.write(BILL.getBytes());
System.out.println(BILL);
setResult(RESULT_OK);
finish();
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
e.printStackTrace();
setResult(RESULT_CANCELED);
finish();
}
runOnUiThread(newRunnable() {
publicvoidrun() {
try {
dialogProgress.dismiss();
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
}
}
});
}
}).start();
}
}
from this link Bluetooth Printer issue in android
Solution 2:
I try my best to give the answer before that you can get the solution from already asked questions
you have 3 options for printing from android app
1>SDKs/Libraries: (like starmicronics, it's limited to few devices)
2>Google Play Apps: (directly calling the intent to thirparty apps)
3>Google cloud print: (recommended. It's easy to use and integrate into an app) By this we connect any printers like Classic printers, Cloud Print printers.
for using Google print as user perspective user should activate google print service to gmail account, Google cloud print used in many places!
Setting up the google print service:
Integrating Cloud printers to App:
In Android there no option for Airprint like other platforms, but Google made awesome cloud printing option for that such that any printer can use the print option from mobile devices.
Sample codes:
Post a Comment for "How To Print Image And Some Data From An Android Device, Using Printer (print Via Bluetooth)?"