how to send data to printer ?

MaxNunezMaxNunez CRMember ✭✭

I have tried follow

public void sendData ()

    {

        try {



            System.Text.UnicodeEncoding uniencoding = new System.Text.UnicodeEncoding();

            byte[] result = uniencoding.GetBytes("0");

            mmOutputStream.Write(result,0,result.Length);

            } catch (Java.IO.IOException e) 

        {

            e.PrintStackTrace ();

        }

    }               

but I don't know that is doing bad

Posts

  • JasonAwbreyJasonAwbrey USInsider, University, Developer Group Leader mod

    It would be helpful if you provided some context? Are you trying to do this on iOS, Android, or OS X? What kind of printer, and how is it attached (directly, via the Network, Bluetooth, etc?).

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    And why are trying to use Java code in Xamarin?

  • MaxNunezMaxNunez CRMember ✭✭

    The context of this code is the follow:

    I'm trying to do this on Android.The printer is Printek Mt2 and it is attached by bluetooth.

    I have the code to connect with printer but it isn't issue it is that I can't send data, or my question is How to send data to printer? need I to use ascii code? And it is true how to send it? because how you can see I try use write() but it doesn't work.

  • MaxNunezMaxNunez CRMember ✭✭

    @DaveHunt I want not use java code but I saw java example and I wanted to do a code conversion but It is complicated because some libraries doesn't work the same way that on Java of course I understand it and I want to receive suggestions about my issue. Thanks

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    What is happening that is not supposed to be happening? Or what is not happening that is supposed to be happening? Do you get an error? Are you sure the stream was successfully opened to the printer? We can't help you if you just post code and ask "What is wrong?" We need a clear description of the problem along with all relevant code (the code that establishes the connection as well as the code that attempts to write to the connection and the code that calls that code).

    Also, I assume your output stream is from the Mono for Android Java.IO.OutputStreamWriter class? Or did you just paste some actual Java code that you want us to translate for you?

  • MaxNunezMaxNunez CRMember ✭✭

    Well,I have not any errors, my specific problem is that I'm not sure if I'm passing data correctly ...

    so this is my code to connect with the printer is based on Android API

                BluetoothAdapter mBluetoothAdapter;
    
        BluetoothSocket mmSocket;
    
        BluetoothDevice mmDevice;
    
    
    
        System.IO.Stream mmOutputStream;
    
        System.IO.Stream mmInputStream;
    

    void OpenBT()

        {
    
         try
    
            {
    
                var uuid = Java.Util.UUID.FromString("00001101-0000-1000-8000-00805f9b34fb");
    
                mmSocket = mmDevice.CreateRfcommSocketToServiceRecord(uuid);
    
                mmSocket.Connect();
    
                mmOutputStream = mmSocket.OutputStream;
    
                mmInputStream = mmSocket.InputStream;
    
                myLabel.Text = "Bluetooth Opened";
    
    
    
            }
    
            catch (Exception e)
    
            {
    
            }
    
        }
    

    and this is to find the device

    void FindBT() {

        try {
    
                mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
    
    
    
                if (mBluetoothAdapter == null) {
    
                    myLabel.Text="No bluetooth adapter available";
    
                }
    
    
    
                if (!mBluetoothAdapter.IsEnabled) {
    
                    Intent enableBluetooth = new Intent(
    
                        BluetoothAdapter.ActionRequestEnable);
    
                    StartActivityForResult(enableBluetooth, 0);
    
                }
    
    
    
                var pairedDevices = mBluetoothAdapter.BondedDevices;
    
                if (pairedDevices.Count > 0) {
    
                    foreach (BluetoothDevice device in pairedDevices) {
    
    
    
                        // MP300 is the name of the bluetooth printer device
    
                        if (device.Name.Equals("Printek Mt2")) {
    
                            mmDevice = device;
    
                            myLabel.Text="Bluetooth Device Found";
    
                            break;
    
                        }
    
                    }
    
                }
    
            } catch (NullPointerException e) {
    
                e.PrintStackTrace();
    
            } catch (Exception e) {
    
                e.PrintStackTrace();
    
            }
    
        }
    

    and those are called from the On create in the main activity.

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    my specific problem is that I'm not sure if I'm passing data correctly

    Is it working? Then you are passing data correctly.
    Is it not working? Then it is possible you are not passing data correctly.

    If you are having an actual problem, then you need to describe the problem so we can help you.

    If you are just wanting to know if the code you are posting is correct, you can figure that out for yourself - try it in your project.

  • MaxNunezMaxNunez CRMember ✭✭

    this is not my point. I know that this code is correct but I have problems to send data to printer and my question is if Is there other way to send data in xamarin. Thanks. I guess that my streams aren't saving the data.

  • MaxNunezMaxNunez CRMember ✭✭
    edited August 2013

    @DaveHunt Thanks for your help, but my issue is solved.

  • Paul_MorenoPaul_Moreno USMember

    @MaxNunez what was your solution to the problem you are experiencing? Could you help me I have the same problem?

Sign In or Register to comment.