Skip to content

Instantly share code, notes, and snippets.

@Alvarocda
Created June 5, 2020 17:26
Show Gist options
  • Save Alvarocda/ed6a97e7e4eb98cc1db9e4ed6aa39d75 to your computer and use it in GitHub Desktop.
Save Alvarocda/ed6a97e7e4eb98cc1db9e4ed6aa39d75 to your computer and use it in GitHub Desktop.
private BluetoothSocket BSocketPadrao { get; set; }
public void AbreConexao()
{
//Identifica a impressora definada como padrão no configurador do aplicativo
System.String ImpPadrao = Application.Current.Properties["ImpPadrao"].ToString();
BluetoothAdapter adaptador = BluetoothAdapter.DefaultAdapter;
BluetoothDevice impressora = (from bd in adaptador?.BondedDevices
where bd.Name.Contains(ImpPadrao)
select bd).FirstOrDefault();
impressora = adaptador.GetRemoteDevice(impressora.Address);
BluetoothSocket Socket = null;
//Todas as impressoras do modelo MPT-II (Leopardo A8 e a que é usada na Positiva são MPT-II)
//Basicamente são a mesma impressora, porem são distribuidos por empresas diferentes, o nome original
//delas são MPT-II
Socket = impressora?.
CreateRfcommSocketToServiceRecord(
UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
BSocketPadrao = Socket;
//Abre a conexão com a impressora
Socket.Connect();
}
//Imprime o texto passado no parametro
public async Task PrintText(string text)
{
BluetoothSocket Socket = BSocketPadrao;
//Transforma o texto da classificão em um Array de bit para que possa ser impresso
byte[] buffer = Encoding.UTF8.GetBytes(text);
//Justifica todo o texto impresso na esquerda
string ajustaEsquerda = ComandosImpressora.ESCQ + "a" + (char)0;
byte[] ajustEsquerdaBytes = Encoding.UTF8.GetBytes(ajustaEsquerda);
await Socket.OutputStream.WriteAsync(ajustEsquerdaBytes, 0, ajustEsquerdaBytes.Length);
byte[] fonte = { ComandosImpressora.B_ESC, 0x21, ComandosImpressora.FONT_32PX };
await Socket.OutputStream.WriteAsync(fonte, 0, fonte.Length);
//Envia o texto da classificação em bits para a impressora
await Socket.OutputStream.WriteAsync(buffer, 0, buffer.Length);
}
//Lista todos os dispositivos bluetooth pareados com o celular
public IList<string> GetDeviceList()
{
using (BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter)
{
var btdevice = bluetoothAdapter?.BondedDevices.Select(i => i.Name).ToList();
return btdevice;
}
}
public class ComandosImpressora
{
public static byte FONT_24PX = (byte)0x01; //24
public static byte FONT_32PX = (byte)0x00;
public static byte FONT_24PX_UNDERLINE = (byte)0x81;
public static byte FONT_32PX_UNDERLINE = (byte)0x80;
public static byte FONT_48PX_HEIGHT = (byte)0x12;
public static byte FONT_48PX_HEIGHT_UNDERLINE = (byte)0x92;
public static byte FONT_48PX_WIDTH = (byte)0x21;
public static byte FONT_48PX_WIDTH_UNDERLINE = (byte)0xA1;
public static byte FONT_64PX_HEIGHT = (byte)0x10;
public static byte FONT_64PX_HEIGHT_UNDERLINE = (byte)0x90;
public static byte FONT_64PX_WIDTH = (byte)0x20;
public static byte FONT_64PX_WIDTH_UNDERLINE = (byte)0xA0;
public static byte FONT_64PX = (byte)0x30;
public static byte FONT_64PX_UNDERLINE = (byte)0xB0;
public static byte FONT_48PX = (byte)0x31;
public static byte FONT_48PX_UNDERLINE = (byte)0xB1;
public static byte Align_LEFT = (byte)0x30;
public static byte Align_CENTER = (byte)0x31;
public static byte Align_RIGHT = (byte)0x32;
public static int Leopard_Alinha_Esquerda = 0x00;
public static int Leopard_Alinha_Centro = 0x01;
public static int Leopard_Alinha_Right = 0x02;
public static byte LANGUAGE_ENGLISH = 0;
public static byte LANGUAGE_FRANCE = 1;
public static byte LANGUAGE_GERMANY = 2;
public static byte LANGUAGE_UK = 3;
public static byte LANGUAGE_DENMARK1 = 4;
public static byte LANGUAGE_SWEDEN = 5;
public static byte LANGUAGE_ITALY = 6;
public static byte LANGUAGE_SPAIN1 = 7;
public static byte LANGUAGE_NORWAY = 8;
public static byte LANGUAGE_DENMARK2 = 9;
public static byte LANGUAGE_SPAIN2 = 10;
public static byte LANGUAGE_PORTUGUESE = 11;
public static byte LANGUAGE_CHINESE = 48;
public static int TYPE_IMAGE = 2;
public static int TYPE_IDLE_TIME = 3;
public static int TYPE_POWEROFF_TIME = 4;
public static int TYPE_ALIGN_LEFT = 5;
public static int TYPE_ALIGN_CENTER = 6;
public static int TYPE_ALIGN_RIGHT = 7;
public static int TYPE_ALIGN_FONTMULTIPLE = 8;
// add decode and encode
public static byte B_SOF = (byte)0xc0;
public static byte B_EOF = (byte)0xc1;
public static byte B_TRANSFER = (byte)0x7D;
public static byte B_XOR = (byte)0x20;
public static byte B_ESC = (byte)0x1B;
//Comandos da Leopardo A8
public static byte HT = 0x9;
public static byte LF = 0x0A;
public static byte CR = 0x0D;
public static byte ESC = 0x1B;
public static byte DLE = 0x10;
public static byte GS = 0x1D;
public static byte FS = 0x1C;
public static byte STX = 0x02;
public static byte US = 0x1F;
public static byte CAN = 0x18;
public static byte CLR = 0x0C;
public static byte EOT = 0x04;
public static byte ESC_CHAR = 0x1B;
public static byte[] LINE_FEED = new byte[] { 0x0A };
public static byte[] CUT_PAPER = new byte[] { GS, 0x56, 0x00 };
public static byte[] INIT_PRINTER = new byte[] { ESC_CHAR, 0x40 };
public static byte[] SET_LINE_SPACE_24 = new byte[] { ESC_CHAR, 0x33, 24 };
public static byte[] SET_LINE_SPACE_30 = new byte[] { ESC_CHAR, 0x33, 30 };
public static byte[] INIT = { 27, 64 };
public static byte[] FEED_LINE = { 10 };
public static byte[] SELECT_FONT_A = { 27, 33, 0 };
public static byte[] HEADER_FONT = { 27, 33, 32 };
public static int ALIGN_LEFT = 0;
public static int ALIGN_CENTER = 1;
public static int ALIGN_RIGHT = 2;
public static int BARCODE_UPCA = 65;
public static int BARCODE_UPCE = 66;
public static int BARCODE_EAN13 = 67;
public static int BARCODE_EAN8 = 68;
public static int BARCODE_CODE39 = 69;
public static int BARCODE_ITF = 70;
public static int BARCODE_CODABAR = 71;
public static int BARCODE_CODE93 = 72;
public static int BARCODE_CODE128 = 73;
public static int BARCODE_PDF417 = 74;
public static int BARCODE_CODE128AUTO = 75;
public static int BARCODE_EAN128 = 76;
public static int HRI_NONE = 0;
public static int HRI_ABOVE = 1;
public static int HRI_BELOW = 2;
public static int HRI_BOTH = 3;
public static int PAGE_LEFT = 0;
public static int PAGE_BOTTOM = 1;
public static int PAGE_RIGHT = 2;
public static int PAGE_TOP = 3;
public static int FILL_WHITE = 0;
public static int FILL_BLACK = 1;
public static int FILL_INVERTED = 2;
public static int SETTINGS_BLUETOOTH = 5;
//Convert ASCII/Decimal
public static string ESCQ = Convert.ToString((char)27);
public static string GSQ = Convert.ToString((char)29);
public static string center = ESCQ + "a" + (char)1; //align center
public static string left = ESCQ + "a" + (char)0; //align left
public static string bold_on = ESCQ + "E" + (char)1; //turn on bold mode
public static string bold_off = ESCQ + "E" + (char)0; //turn off bold mode
public static string cut = ESCQ + "d" + (char)1 + GS + "V" + (char)66; //add 1 extra line before partial cut
public static string initp = ESCQ + (char)64; //initialize printer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment