我正在开发一个应用程序,它将通过蓝牙发送数据到打印机打印(一个热敏打印机的收据)。我已经按照这个链接中的代码来做。
http:/pastie.org6203514 而这个链接也 http:/pastie.org6203516
我能够看到设备和它的MAC地址和它的名字,当我发送数据到打印机(打印机上的LED灯停止闪烁,并成为标准,即打印机与我的Android手机连接),但当我发送数据,它是不打印,也没有给出任何错误也。我已经google了很多,我发现很多代码,并尝试了所有的代码集,但无法打印。
请任何一个人可以帮助我离开这里。我听说与Intents它可以很容易做到,但不能得到确切的解决方案与Intents。
任何帮助将被感激 谢谢在前进
格涅沙
最后我自己解决了这个问题,问题是我发送的头字节才是真正的罪魁祸首。实际上我发送的是170,1(其中170是打印机必须接收的第一个字节,第二个字节是打印机ID,我指的是打印机控制卡设计者给出的一些com端口)。实际我必须发送170,2,其中2是打印机ID,这样才能给出正确的打印,对于每个打印机来说,根据他们的控制卡发送数据是很常见的。
非常感谢朋友们,这是我的代码,你可以使用这些代码为所有类型的打印机(POS-热敏打印机)。
public void IntentPrint(String txtvalue)
{
byte[] buffer = txtvalue.getBytes();
byte[] PrintHeader = { (byte) 0xAA, 0x55,2,0 };
PrintHeader[3]=(byte) buffer.length;
InitPrinter();
if(PrintHeader.length>128)
{
value+="\nValue is more than 128 size\n";
txtLogin.setText(value);
}
else
{
try
{
for(int i=0;i<=PrintHeader.length-1;i++)
{
mmOutputStream.write(PrintHeader[i]);
}
for(int i=0;i<=buffer.length-1;i++)
{
mmOutputStream.write(buffer[i]);
}
mmOutputStream.close();
mmSocket.close();
}
catch(Exception ex)
{
value+=ex.toString()+ "\n" +"Excep IntentPrint \n";
txtLogin.setText(value);
}
}
}
剩下的就用这个代码了。
public void InitPrinter()
{
try
{
if(!bluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("Your Device Name")) //Note, you will need to change this to match the name of your device
{
mmDevice = device;
break;
}
}
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
//Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
//mmSocket = (BluetoothSocket) m.invoke(mmDevice, uuid);
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
bluetoothAdapter.cancelDiscovery();
if(mmDevice.getBondState()==2)
{
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
}
else
{
value+="Device not connected";
txtLogin.setText(value);
}
}
else
{
value+="No Devices found";
txtLogin.setText(value);
return;
}
}
catch(Exception ex)
{
value+=ex.toString()+ "\n" +" InitPrinter \n";
txtLogin.setText(value);
}
}
你的目的是为了一个特定的打印协议吗?(针对特定的打印机?)
如果不是 并且可以在任何连接的打印机上进行通用打印,你可以使用这段代码。
在你想打印一个特定文件的地方写下这个代码。
Intent intent = Tools.getSendToPrinterIntent(
DisplayActivity.this, mPdfAsPictures,
mPrintCurrentIndex);
// notify the activity on return (will need to ask the user for
// approvel)
startActivityForResult(intent, ACTIVITY_PRINT);
这是辅助方法
public static Intent getSendToPrinterIntent(Context ctx, String[] fileFullPaths, int indexToPrint){
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
// This type means we can send either JPEG, or PNG
sendIntent.setType("image/*");
ArrayList<Uri> uris = new ArrayList<Uri>();
File fileIn = new File(fileFullPaths[indexToPrint]);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
return sendIntent;
}
最后,你会收到答案的:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ACTIVITY_PRINT) {
switch (resultCode) {
case Activity.RESULT_CANCELED:
Log.d(TAG(), "onActivityResult, resultCode = CANCELED");
break;
case Activity.RESULT_FIRST_USER:
Log.d(TAG(), "onActivityResult, resultCode = FIRST_USER");
break;
case Activity.RESULT_OK:
Log.d(TAG(), "onActivityResult, resultCode = OK");
break;
}
}
};
祝您好运!