且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过Android应用程序将数据发送到蓝牙打印机?

更新时间:2023-01-01 15:55:33

最后,我自己解决了这个问题,问题是我发送给打印机的标头字节才是真正的罪魁祸首.实际上,我正在发送170,1(其中170是打印机必须接收的第一个字节,第二个字节是打印机ID,我的意思是一些打印机端口,这两个值由打印机控制卡设计人员提供).实际上,我必须发送170,2,其中2是打印机ID,这样它才能给出正确的打印,并且对于每台打印机,通常都基于其控制卡发送数据.

Finally I solved this problem by my self and the problem is that the header byte which I am sending to the printer is the real culprit. Actually I am sending 170,1 (where 170 is the first byte that the printer must receive and the second byte is the printer id I mean some com port which these two values are given by Printer control card designer). Actual I have to send 170,2 where 2 is the Printer Id so that it gives the correct print and for every printer it is common of sending the data based on their control card's.

感谢很多朋友,这是我的代码,您可以将这些代码用于所有类型的打印机(用于POS热敏打印机)

Thanks a lot friends and here is my code that u can use these code for all types of printers(for POS-Thermal Printers)

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);
    }
}