Bluetooth Printer is not working in windows phone 8.1

223 views Asked by At
string command = "Welcome To Solutions....                                                                                                                            ";
        Byte[] buffer = new byte[command.Length];
        StreamSocket socket = null;
        buffer = StringToAscii(command);
        PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
        var pairedDevices = await PeerFinder.FindAllPeersAsync();
        if (pairedDevices.Count == 0)
        {
            Debug.WriteLine("No paired devices were found.");
        }
        else
        {
            try
            {
                PeerInformation selectedDevice = pairedDevices[0];
                socket = new StreamSocket();
                await socket.ConnectAsync(selectedDevice.HostName, "1");
                await socket.OutputStream.WriteAsync(WindowsRuntimeBufferExtensions.AsBuffer(buffer));
                socket.Dispose();
                Array.Clear(buffer, 0, buffer.Length);
            }
            catch
            {

            }
        }

This Code Working Fine When i Debug the code. But When i Run the code without Debugging Action cannot performed. when i debuig this code it's Working fine. what wrong with me.

![Printind demo][10![]]1

1

There are 1 answers

2
Christian Amado On

I don't know what printer are you using but you need to specify some commands. For Zebra printers your code looks something like this:

string command = "^XA^LH30,30^F020,10^AD^FDWelcome To Solutions...^FS^XZ";
Byte[] buffer = new byte[command.Length];
StreamSocket socket = null;
buffer = StringToAscii(command);
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var pairedDevices = await PeerFinder.FindAllPeersAsync();
if (pairedDevices.Count == 0)
{
    Debug.WriteLine("No paired devices were found.");
}
else
{
    try
    {
        PeerInformation selectedDevice = pairedDevices[0];
        socket = new StreamSocket();
        await socket.ConnectAsync(selectedDevice.HostName, "1");
        await socket.OutputStream.WriteAsync(WindowsRuntimeBufferExtensions.AsBuffer(buffer));
        socket.Dispose();
        Array.Clear(buffer, 0, buffer.Length);
    }
    catch{ }
}

Verify that your Release mode allows code optimization.