我正在开发一个应用程序,它将通过蓝牙将数据发送到打印机进行打印(收据热敏打印机)。我已按照此链接中的代码进行操作。
http://pastie.org/6203514 http://pastie.org/6203514这个链接也http://pastie.org/6203516 http://pastie.org/6203516
当我将数据发送到打印机时(打印机上的 LED 指示灯停止闪烁并变为标准状态,即打印机已与我的 Android 手机连接),我能够看到设备及其 MAC 地址和名称,但是当我发送它没有打印数据,也没有给出任何错误。我用谷歌搜索了很多,找到了很多代码并尝试了所有代码集,但无法打印。
请任何人都可以帮助我离开这里。我听说用 Intents 可以很容易地完成,但用 Intents 却无法得到准确的解决方案。
任何帮助,将不胜感激
提前致谢
Ganesh
最后我自己解决了这个问题,问题是我发送到打印机的标头字节是真正的罪魁祸首。实际上我正在发送 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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)