Android设备的蓝牙打印? [英] Bluetooth printing fron android devices ?

查看:84
本文介绍了Android设备的蓝牙打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,我需要从Android手机打印文件,这是蓝牙的支持者。



任何帮助?



提前谢谢..

解决方案

我用参考链接解决了



import java.io.OutputStream;

import java.lang.reflect.Method;



导入android.app.Activity;

导入android.app.Dialog;

导入android.bluetooth.BluetoothAdapter;

导入android.bluetooth.BluetoothDevice;

导入android.bluetooth.BluetoothSocket;

导入android.content.BroadcastReceiver;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content。 IntentFilter;

导入android.os.Bundle;

导入android.util.Log;

导入android.widget.Toast; < br $>


公共类BluetoothPrint扩展活动{



BluetoothAdapter mBTAdapter;

BluetoothSocket mBTSocket = null;

Dialog dialogProgress;

String BILL,TRANS_ID;

String PRINTER_MAC_ID;

final String ERROR_MESSAGE = 打印账单时出错。;



@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

尝试{



// BILL = getIntent()。getStringExtra(TO_PRINT );

// TRANS_ID = getIntent()。getStringExtra(TRANS_ID);



// PRINTER_MAC_ID = getIntent()。 getStringExtra(MAC_ID);

PRINTER_MAC_ID =00:1F:B7:02:8F:44;

// PRINTER_MAC_ID =00:12:F3: 0D:A3:E6;

// TRANS_ID =12345678;

BILL =\ nSale Slip No:12345678++04-08-2011 \ n;

BILL = BILL +----------- -----------------------------;

BILL = BILL +\ n \ n ;

BILL = BILL +总数量:++2.0 \ n;

BILL = BILL +总价值:+ +17625.0 \ n;

BILL = BILL +----------------------------- ------------;



mBTAdapter = BluetoothAdapter.getDefaultAdapter();

dialogProgress = new Dialog (BluetoothPrint.this);



尝试{

if(mBTAdapter.isDiscovering())

mBTAdapter。 cancelDiscovery();

else

mBTAdapter.startDiscovery();

} catch(例外e){

Log.e(Class,My Exe,e);

}

System.out.println(BT搜索状态:

+ mBTAdapter.isDiscovering());

if(mBTAdapter == null){

Toast.makeText(这个,设备没有蓝牙功能,

Toast.LENGTH_LONG)。show();

完成();

}否则{

if(!mBTAdapter.isEnabled()){

意图i =新意图(

BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(i,0);

}



//注册BroadcastReceiver

IntentFilter filter = new IntentFilter(

BluetoothDevice.ACTION_FOUND);

registerReceiver(mReceiver,filter); //不要忘记

//取消注册

// onDestroy

dialogProgress.setTitle(查找打印机......) ;

dialogProgress

.setOnDismissListener(new DialogInterface.OnDismissListener(){

public void onDismiss(DialogInterface dialog){

dialog.dismiss();

setResult(RESULT_CANCELED);

完成();

}

});

dialogProgress.show();



}



} catch(例外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(){

dialogProgress.setTitle(正在连接......);

dialogProgress.show();

}



});



mBTAdapter.cancelDiscovery();



试试{

System.out

.println(**************************#****连接 );

蓝牙设备mdevice = mBTAdapter

.getRemoteDevice(地址);

方法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());

System.out.println(BILL);





//mBTSocket.close();

setResult(RESULT_OK);

完成();

} catch(例外e){

Log.e(Class,My Exe,e);

//Toast.makeText(BluetoothPrint.this,ERROR_MESSAGE, Toast.LENGTH_SHORT)。show();

e.printStackTrace();

setResult(RESULT_CANCELED);

finish( );



}



runOnUiThread(new Runnable(){



public void run(){

try {

dialogProgress.dismiss();

} catch(异常) e){

Log.e(Class,My Exe,e);

}

}



});



}



})。开始();

}



@Override

protected void onDestroy(){

Log.i(Dest,Checking Ddest);

super.onDestroy();

尝试{

if (dialogProgress!= null)

dialogProgress.dismiss();

if(mBTAdapter!= null)

mBTA dapter.cancelDiscovery();

this.unregisterReceiver(mReceiver);

} catch(例外e){

Log.e(Class ,我的Exe,e);

}

}



私人最终BroadcastReceiver mReceiver = new BroadcastReceiver(){

public void onReceive(Context context,Intent intent){



try {

String action = intent.getAction();

//当发现找到设备时

if(BluetoothDevice.ACTION_FOUND.equals(action)){

//从意图中获取BluetoothDevice对象

蓝牙设备=意图

.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

System.out。 println(***+ device.getName()+:

+ device.g etAddress());



if(device.getAddress()。equalsIgnoreCase(PRINTER_MAC_ID)){

mBTAdapter.cancelDiscovery(); < br $>
dialogProgress.dismiss();

Toast.makeText(BluetoothPrint.this,

device.getName()+打印数据,

Toast.LENGTH_LONG)。show();

printBillToDevice(PRINTER_MAC_ID);

Toast.makeText(BluetoothPrint.this,

device.getName()+found,Toast.LENGTH_LONG)

.show();

}

}

} catch(例外e){

Log.e(Class,My Exe,e);

//Toast.makeText( BluetoothPrint.this,ERROR_MESSAGE, Toast.LENGTH_SHORT)。show();



}

}

};



@Override

public void onBackPressed(){

try {

if(mBTAdapter!= null)

mBTAdapter.cancelDiscovery();

this.unregisterReceiver(mReceiver);

} catch(例外e){

Log.e(Class,My Exe,e);

}

setResult(RESULT_CANCELED);

完成();

}



}



来自此链接

http://stackoverflow.com/questions/7145787/bluetooth-printer -issue-in-android [ ^ ]

<集团kquote> 我用参考链接解决了







来自此链接

http://stackoverflow.com / questions / 7145787 / bluetooth-printer-issue-in-android [ ^ ]


请告诉我哪个蓝牙打印机支持此程序。因为我是android新手,请帮助我。


I am making one application from where i need to print a document from android phone , which is supporter by Bluetooth.

Any helps??

Thanks in advance ..

解决方案

I solved it with reference link

import java.io.OutputStream;
import java.lang.reflect.Method;

import android.app.Activity;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class BluetoothPrint extends Activity {

BluetoothAdapter mBTAdapter;
BluetoothSocket mBTSocket = null;
Dialog dialogProgress;
String BILL, TRANS_ID;
String PRINTER_MAC_ID;
final String ERROR_MESSAGE = "There has been an error in printing the bill.";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {

// BILL = getIntent().getStringExtra("TO_PRINT");
// TRANS_ID = getIntent().getStringExtra("TRANS_ID");

// PRINTER_MAC_ID = getIntent().getStringExtra("MAC_ID");
PRINTER_MAC_ID = "00:1F:B7:02:8F:44";
//PRINTER_MAC_ID = "00:12:F3:0D:A3:E6";
// TRANS_ID="12345678";
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();
dialogProgress = new Dialog(BluetoothPrint.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(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.setTitle("Finding printer...");
dialogProgress
.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
setResult(RESULT_CANCELED);
finish();
}
});
dialogProgress.show();

}

} 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() {
dialogProgress.setTitle("Connecting...");
dialogProgress.show();
}

});

mBTAdapter.cancelDiscovery();

try {
System.out
.println("**************************#****connecting");
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());
System.out.println(BILL);


//mBTSocket.close();
setResult(RESULT_OK);
finish();
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
//Toast.makeText(BluetoothPrint.this, ERROR_MESSAGE, Toast.LENGTH_SHORT).show();
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();
}

@Override
protected void onDestroy() {
Log.i("Dest ", "Checking Ddest");
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);
}
}

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);
System.out.println("***" + device.getName() + " : "
+ device.getAddress());

if (device.getAddress().equalsIgnoreCase(PRINTER_MAC_ID)) {
mBTAdapter.cancelDiscovery();
dialogProgress.dismiss();
Toast.makeText(BluetoothPrint.this,
device.getName() + " Printing data",
Toast.LENGTH_LONG).show();
printBillToDevice(PRINTER_MAC_ID);
Toast.makeText(BluetoothPrint.this,
device.getName() + " found", Toast.LENGTH_LONG)
.show();
}
}
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
//Toast.makeText(BluetoothPrint.this, ERROR_MESSAGE, Toast.LENGTH_SHORT).show();

}
}
};

@Override
public void onBackPressed() {
try {
if (mBTAdapter != null)
mBTAdapter.cancelDiscovery();
this.unregisterReceiver(mReceiver);
} catch (Exception e) {
Log.e("Class ", "My Exe ", e);
}
setResult(RESULT_CANCELED);
finish();
}

}

from this link
http://stackoverflow.com/questions/7145787/bluetooth-printer-issue-in-android[^]


I solved it with reference link



from this link
http://stackoverflow.com/questions/7145787/bluetooth-printer-issue-in-android[^]


please tell me which Bluetooth printer support this program. Because i am new in android, please help me.


这篇关于Android设备的蓝牙打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆