G K*_*G K 13 android bluetooth thermal-printer
我正在开发一个应用程序,它将通过蓝牙将数据发送到打印机进行打印(热敏打印机用于收据).我已经按照此链接中的代码进行操作.
http://pastie.org/6203514和此链接也是http://pastie.org/6203516
当我将数据发送到打印机时,我能够看到设备及其MAC地址及其名称(打印机上的指示灯停止闪烁并变为标准,即打印机与我的Android手机连接)但是当我发送时数据不打印,也没有给出任何错误.我搜索了很多,我找到了很多代码并尝试了所有代码但无法打印.
请任何人帮助我离开这里.我听说Intent可以很容易地完成,但无法通过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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个代码为其余的:
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);
}
}
Run Code Online (Sandbox Code Playgroud)