public void Connect() { BluetoothDevice device = mBluetoothAdapter.GetRemoteDevice(address); System.Console.WriteLine("Conexion en curso" + device); mBluetoothAdapter.CancelDiscovery(); try { btSocket = device.CreateRfcommSocketToServiceRecord(MY_UUID); btSocket.Connect(); System.Console.WriteLine("Conexion Correcta"); } catch (System.Exception e) { Console.WriteLine (e.Message); try { btSocket.Close(); } catch (System.Exception) { System.Console.WriteLine("Imposible Conectar"); } System.Console.WriteLine("Socket Creado"); } beginListenForData(); dataToSend = new Java.Lang.String("e"); writeData(dataToSend); }
public async Task<bool> Init(bool simulatormode = false) { running = true; //initialize _data data = new Dictionary<string, string> {{"vin", DefValue}}; //VIN piDs = ObdShare.ObdUtil.GetPIDs(); foreach (var v in piDs.Values) { data.Add(v, DefValue); } this.simulatormode = simulatormode; if (simulatormode) { return true; } bluetoothAdapter = BluetoothAdapter.DefaultAdapter; if (bluetoothAdapter == null) { System.Diagnostics.Debug.WriteLine("Bluetooth is not available"); return false; } try { var ba = bluetoothAdapter.BondedDevices; foreach (var bd in ba) { if (bd.Name.ToLower().Contains("obd")) bluetoothDevice = bd; } if (bluetoothDevice == null) { return false; } bluetoothSocket = bluetoothDevice.CreateRfcommSocketToServiceRecord(SppUuid); await bluetoothSocket.ConnectAsync(); connected = true; } catch (Java.IO.IOException) { // Close the socket try { connected = false; bluetoothSocket.Close(); } catch (Java.IO.IOException) { } catch (Exception) { } return false; } catch (Exception) { } if (connected) { reader = bluetoothSocket.InputStream; writer = bluetoothSocket.OutputStream; string s; s = await SendAndReceive("ATZ\r"); s = await SendAndReceive("ATE0\r"); s = await SendAndReceive("ATL1\r"); s = await SendAndReceive("ATSP00\r"); PollObd(); return true; } else return false; }
public string PrintInvSumm(BluetoothSocket mmSocket,BluetoothDevice mmDevice,DateTime printDate1,DateTime printDate2) { msg = ""; Stream mmOutputStream; try { UUID uuid = UUID.FromString ("00001101-0000-1000-8000-00805F9B34FB"); mmSocket = mmDevice.CreateInsecureRfcommSocketToServiceRecord (uuid); if (mmSocket == null) { msg = "Error creating sockect"; return msg; } if (mmDevice.BondState == Bond.Bonded) { mmSocket.Connect (); Thread.Sleep (300); mmOutputStream = mmSocket.OutputStream; byte[] charfont; if (apara.PaperSize == "58mm") { charfont = new Byte[] { 27, 33, 1 }; //Char font 9x17 mmOutputStream.Write (charfont, 0, charfont.Length); } if (apara.PaperSize == "80mm") { charfont = new Byte[] { 27, 33, 0 }; //Char font 12x24 mmOutputStream.Write (charfont, 0, charfont.Length); } string text = GetInvoiceSumm (printDate1, printDate2); //byte[] cc = ASCIIEncoding.ASCII.GetBytes(text); byte[] cc = Encoding.GetEncoding ("GB18030").GetBytes (text); int rem; int result = Math.DivRem (cc.Length, 2048, out rem); int pos = 0; for (int line = 0; line < result; line++) { IsStreamCanWrite (mmOutputStream); mmOutputStream.Write (cc, pos, 2048); pos += 2048; } if (rem > 0) mmOutputStream.Write (cc, pos, rem); //mmOutputStream.Write (cc, 0, cc.Length); Thread.Sleep (3000); mmOutputStream.Flush (); mmOutputStream.Close (); mmSocket.Close (); } } catch (Exception ex) { msg = ex.Message; } return msg; }
//Bluetooth Printer -Mini Bluetooth Printer public void BluetoothMiniTest(BluetoothSocket mmSocket,BluetoothDevice mmDevice) { msg = ""; Stream mmOutputStream; //TextView txtv = FindViewById<TextView> (Resource.Id.textView2); try { UUID uuid = UUID.FromString ("00001101-0000-1000-8000-00805F9B34FB"); mmSocket = mmDevice.CreateInsecureRfcommSocketToServiceRecord (uuid); if (mmSocket == null) { msg = "Error creating sockect"; return; } if (mmDevice.BondState == Bond.Bonded) { TrytoConnect(mmSocket); Thread.Sleep (300); mmOutputStream = mmSocket.OutputStream; byte[] charfont; charfont = new Byte[] { 27, 64 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); if (apara.PaperSize=="58mm") { charfont = new Byte[] { 27, 33, 1 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); } if (apara.PaperSize=="80mm") { charfont = new Byte[] { 27, 33, 0 }; //Char font 12x24 mmOutputStream.Write(charfont, 0, charfont.Length); } charfont = new Byte[] { 28, 38 }; mmOutputStream.Write(charfont, 0, charfont.Length); string test = ""; PrintCompHeader (ref test); test = test+"\n\n\n\n\n"; byte[] cc = Encoding.GetEncoding("GB18030").GetBytes(test); mmOutputStream.Write (cc,0, cc.Length); Thread.Sleep (300); charfont = new Byte[] { 28, 46 }; mmOutputStream.Write(charfont, 0, charfont.Length); mmOutputStream.Close (); mmSocket.Close (); msg ="Printing...."; } else { msg= "Device not connected"; } } catch (Exception ex) { msg = ex.Message; } }
public void BluetoothMiniCN(BluetoothSocket mmSocket,BluetoothDevice mmDevice,CNNote inv,CNNoteDtls[] list,int noofcopy ) { msg = ""; Stream mmOutputStream; try { UUID uuid = UUID.FromString ("00001101-0000-1000-8000-00805F9B34FB"); mmSocket = mmDevice.CreateInsecureRfcommSocketToServiceRecord (uuid); if (mmSocket == null) { msg = "Error creating sockect"; return; } if (mmDevice.BondState == Bond.Bonded) { TrytoConnect(mmSocket); Thread.Sleep (300); mmOutputStream = mmSocket.OutputStream; byte[] charfont; charfont = new Byte[] { 27, 64 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); if (apara.PaperSize=="58mm") { charfont = new Byte[] { 27, 33, 1 }; //Char font 9x17 //charfont = new Byte[] { 27, 77, 1 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); } if (apara.PaperSize=="80mm") { charfont = new Byte[] { 27, 33, 0 }; //Char font 12x24 //charfont = new Byte[] { 27, 77, 1 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); } charfont = new Byte[] { 28, 38 }; mmOutputStream.Write(charfont, 0, charfont.Length); //charfont = new Byte[] { 28, 67,0,48 }; //mmOutputStream.Write(charfont, 0, charfont.Length); string test = ""; double invTtlAmt =0; double invTtlTax =0; bool IsfoundInvoice =PrintCNInvoice(inv,ref test,ref invTtlAmt,ref invTtlTax ); if (!IsfoundInvoice){ PrintCompHeader (ref test); PrintCustomer (ref test,inv.custcode); } PrintCNHeader (ref test,inv); string dline=""; double ttlAmt =0; double ttltax =0; int count =0; foreach(CNNoteDtls itm in list) { count+=1; dline =dline+PrintCNDetail (itm,count); ttlAmt = ttlAmt+ itm.netamount; ttltax = ttltax+itm.tax; } test += dline; PrintTotal (ref test,ttlAmt,ttltax); PrintCNTaxSumm(ref test,list ); PrintFooter (ref test); if (IsfoundInvoice) { test += "\nTHANK YOU\n\n"; PrintTotal (ref test,ttlAmt, ttltax, invTtlAmt,invTtlTax); test += "n\n\n\n\n\n\n\n"; }else test += "\nTHANK YOUn\n\n\n\n\n\n\n"; //byte[] cc =ASCIIEncoding.ASCII.GetBytes(test); byte[] cc = Encoding.GetEncoding("GB18030").GetBytes(test); int bLen= cc.Length; for (int i=0; i<noofcopy;i++) { int rem; int result =Math.DivRem(cc.Length, 2048, out rem); int pos =0; for(int line= 0;line<result;line++) { IsStreamCanWrite (mmOutputStream); mmOutputStream.Write (cc, pos, 2048); pos += 2048; } if (rem >0) mmOutputStream.Write (cc, pos, rem); //mmOutputStream.BeginWrite(cc,0,cc.Length, //mmOutputStream.Write (cc, 0, cc.Length); Thread.Sleep (3000); //mmOutputStream.Flush (); } Thread.Sleep (300); mmOutputStream.Close (); //mmInputStream.Close(); mmSocket.Close (); msg ="Printing...."; } else { //txtv.Text = "Device not connected"; msg= "Device not connected"; } } catch (Exception ex) { msg = ex.Message; } // return msg; }
//Bluetooth Printer -Mini Bluetooth Printer public void BluetoothMini(BluetoothSocket mmSocket,BluetoothDevice mmDevice,Invoice inv,InvoiceDtls[] list,int noofcopy) { msg = ""; Stream mmOutputStream; //TextView txtv = FindViewById<TextView> (Resource.Id.textView2); try { UUID uuid = UUID.FromString ("00001101-0000-1000-8000-00805F9B34FB"); mmSocket = mmDevice.CreateInsecureRfcommSocketToServiceRecord (uuid); if (mmSocket == null) { //txtv.Text = "Error creating sockect"; msg = "Error creating sockect"; //errmsg.Append(msg); return; } //txtv.Text = mmDevice.BondState.ToString (); if (mmDevice.BondState == Bond.Bonded) { //errmsg.Append("Start to connect\n"); //mmSocket.Connect (); TrytoConnect(mmSocket); Thread.Sleep (300); //mmInputStream = mmSocket.InputStream; //errmsg.Append("connected\n"); mmOutputStream = mmSocket.OutputStream; byte[] charfont; charfont = new Byte[] { 27, 64 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); if (apara.PaperSize=="58mm") { charfont = new Byte[] { 27, 33, 1 }; //Char font 9x17 //charfont = new Byte[] { 27, 77, 1 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); } if (apara.PaperSize=="80mm") { charfont = new Byte[] { 27, 33, 0 }; //Char font 12x24 //charfont = new Byte[] { 27, 77, 1 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); } charfont = new Byte[] { 28, 38 }; mmOutputStream.Write(charfont, 0, charfont.Length); //charfont = new Byte[] { 28, 67,0,48 }; // mmOutputStream.Write(charfont, 0, charfont.Length); //charfont = new Byte[] { 27, 82, 15 }; //Char font 9x17 // mmOutputStream.Write(charfont, 0, charfont.Length); string test = ""; //errmsg.Append("Set printer\n"); PrintInvoice (inv, list, ref test); //var encoding = Encoding.GetEncoding(936); //byte[] source = Encoding.Unicode.GetBytes(text); //byte[] converted = Encoding.Convert(Encoding.Unicode, encoding, source); //errmsg.Append("Start Endcoidng\n"); byte[] cc = Encoding.GetEncoding("GB18030").GetBytes(test); for (int i=0; i<noofcopy;i++) { int rem; int result =Math.DivRem(cc.Length, 2048, out rem); int pos =0; for(int line= 0;line<result;line++) { IsStreamCanWrite (mmOutputStream); mmOutputStream.Write (cc, pos, 2048); pos += 2048; } if (rem >0) mmOutputStream.Write (cc, pos, rem); //mmOutputStream.BeginWrite(cc,0,cc.Length, //mmOutputStream.Write (cc, 0, cc.Length); Thread.Sleep (3000); //mmOutputStream.Flush (); } Thread.Sleep (300); charfont = new Byte[] { 28, 46 }; mmOutputStream.Write(charfont, 0, charfont.Length); mmOutputStream.Close (); //mmInputStream.Close(); mmSocket.Close (); msg ="Printing...."; } else { //txtv.Text = "Device not connected"; msg= "Device not connected"; //errmsg.Append(msg); } } catch (Exception ex) { msg = ex.Message; } }
//Evento de conexion al Bluetooth public void Connect() { //asignamos el sensor bluetooth con el que vamos a trabajar mBluetoothAdapter = BluetoothAdapter.DefaultAdapter; //Verificamos que este habilitado if (!mBluetoothAdapter.Enable()) { Toast.MakeText(this, "Bluetooth Desactivado", ToastLength.Short).Show(); } //verificamos que no sea nulo el sensor if (mBluetoothAdapter == null) { Toast.MakeText(this, "Bluetooth No Existe o esta Ocupado", ToastLength.Short) .Show(); } //Iniciamos la conexion con el arduino BluetoothDevice device = mBluetoothAdapter.GetRemoteDevice(address); System.Console.WriteLine("Conexion en curso" + device); //Indicamos al adaptador que ya no sea visible mBluetoothAdapter.CancelDiscovery(); try { //Inicamos el socket de comunicacion con el arduino btSocket = device.CreateRfcommSocketToServiceRecord(MY_UUID); //Conectamos el socket btSocket.Connect(); System.Console.WriteLine("Conexion Correcta"); } catch (System.Exception e) { //en caso de generarnos error cerramos el socket Console.WriteLine(e.Message); try { btSocket.Close(); } catch (System.Exception) { System.Console.WriteLine("Imposible Conectar"); } System.Console.WriteLine("Socket Creado"); } //Una vez conectados al bluetooth mandamos llamar el metodo que generara el hilo //que recibira los datos del arduino beginListenForData(); //NOTA envio la letra e ya que el sketch esta configurado para funcionar cuando //recibe esta letra. //dataToSend = new Java.Lang.String("Hola"); //writeData(dataToSend); }
private bool PrintToDevice(string text,int noofcopy) { bool isPrinted = false; msg = ""; Stream mmOutputStream=null; try { UUID uuid = UUID.FromString ("00001101-0000-1000-8000-00805F9B34FB"); mmSocket = mmDevice.CreateInsecureRfcommSocketToServiceRecord (uuid); if (mmSocket == null) { msg = callingActivity.Resources.GetString(Resource.String.msg_bluetoothnofound); return isPrinted; } if (mmDevice.BondState == Bond.Bonded) { TrytoConnect(mmSocket); Thread.Sleep (300); mmOutputStream = mmSocket.OutputStream; byte[] charfont; if (iSPrintCompLogo) PrintLogo(mmOutputStream); charfont = new Byte[] { 27, 64 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); if (apara.PaperSize=="58mm") { charfont = new Byte[] { 27, 33, 1 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); } if (apara.PaperSize=="80mm") { charfont = new Byte[] { 27, 33, 0 }; //Char font 12x24 mmOutputStream.Write(charfont, 0, charfont.Length); } charfont = new Byte[] { 28, 38 }; mmOutputStream.Write(charfont, 0, charfont.Length); byte[] cc = Encoding.GetEncoding("GB18030").GetBytes(text); for (int i=0; i<noofcopy;i++) { int rem; int result =Math.DivRem(cc.Length, 2048, out rem); int pos =0; for(int line= 0;line<result;line++) { IsStreamCanWrite (mmOutputStream); mmOutputStream.Write (cc, pos, 2048); pos += 2048; } if (rem >0) mmOutputStream.Write (cc, pos, rem); Thread.Sleep (3000); } Thread.Sleep (300); charfont = new Byte[] { 28, 46 }; mmOutputStream.Write(charfont, 0, charfont.Length); mmOutputStream.Close (); mmSocket.Close (); isPrinted =true; } else { //txtv.Text = "Device not connected"; msg= callingActivity.Resources.GetString(Resource.String.msg_bluetoothnofound); //errmsg.Append(msg); } } catch (Exception ex) { msg = ex.Message; } finally { if (mmOutputStream != null) { mmOutputStream.Close (); mmOutputStream = null; } if (mmSocket != null) { mmSocket.Close (); mmSocket = null; } if (mmDevice != null) { mmDevice.Dispose (); mmDevice = null; } } return isPrinted; }
public void Connect() { BluetoothDevice device = mBluetoothAdapter.GetRemoteDevice(address); System.Console.WriteLine("Conexion en curso" + device); mBluetoothAdapter.CancelDiscovery(); try { btSocket = device.CreateRfcommSocketToServiceRecord(MY_UUID); btSocket.Connect(); System.Console.WriteLine("Conexion Correcta"); } catch (System.Exception e) { Console.WriteLine(e.Message); try { btSocket.Close(); } catch (System.Exception) { System.Console.WriteLine("Imposible Conectar"); } System.Console.WriteLine("Socket Creado"); } beginListenForData(); var ac = FindViewById<Button>(Resource.Id.ac); var kapa = FindViewById<Button>(Resource.Id.kapa); dataToSend = new Java.Lang.String("1"); ac.Click += delegate { dataToSend = new Java.Lang.String("1"); writeData(dataToSend); }; kapa.Click += delegate { dataToSend = new Java.Lang.String("2"); writeData(dataToSend); }; writeData(dataToSend); }
public void BluetoothPrint(BluetoothSocket mmSocket,BluetoothDevice mmDevice,string text,int noofcopy) { msg = ""; Stream mmOutputStream; //TextView txtv = FindViewById<TextView> (Resource.Id.textView2); try { UUID uuid = UUID.FromString ("00001101-0000-1000-8000-00805F9B34FB"); mmSocket = mmDevice.CreateInsecureRfcommSocketToServiceRecord (uuid); if (mmSocket == null) { msg = "Error creating sockect"; return; } if (mmDevice.BondState == Bond.Bonded) { TrytoConnect(mmSocket); Thread.Sleep (300); mmOutputStream = mmSocket.OutputStream; byte[] charfont; charfont = new Byte[] { 27, 64 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); if (apara.PaperSize=="58mm") { charfont = new Byte[] { 27, 33, 1 }; //Char font 9x17 mmOutputStream.Write(charfont, 0, charfont.Length); } if (apara.PaperSize=="80mm") { charfont = new Byte[] { 27, 33, 0 }; //Char font 12x24 mmOutputStream.Write(charfont, 0, charfont.Length); } charfont = new Byte[] { 28, 38 }; mmOutputStream.Write(charfont, 0, charfont.Length); // charfont = new Byte[] { 28, 67,0,48 }; // mmOutputStream.Write(charfont, 0, charfont.Length); byte[] cc = Encoding.GetEncoding("GB18030").GetBytes(text); for (int i=0; i<noofcopy;i++) { int rem; int result =Math.DivRem(cc.Length, 2048, out rem); int pos =0; for(int line= 0;line<result;line++) { IsStreamCanWrite (mmOutputStream); mmOutputStream.Write (cc, pos, 2048); pos += 2048; } if (rem >0) mmOutputStream.Write (cc, pos, rem); Thread.Sleep (3000); } Thread.Sleep (300); charfont = new Byte[] { 28, 46 }; mmOutputStream.Write(charfont, 0, charfont.Length); mmOutputStream.Close (); mmSocket.Close (); msg ="Printing...."; } else { msg= "Device not connected"; } } catch (Exception ex) { msg = ex.Message; } }