Reputation: 41
I've a Bluetooth thermal printer and i am trying to send a print command using Xamarin.
I've tried the following code
BluetoothSocket socket = null;
BufferedReader inReader = null;
BufferedWriter outReader = null;
BluetoothDevice device = (from bd in BluetoothAdapter.DefaultAdapter?.BondedDevices
where bd?.Name == deviceName
select bd).FirstOrDefault();
//BluetoothDevice hxm = BluetoothAdapter.DefaultAdapter.GetRemoteDevice (bt_printer);
UUID applicationUUID = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
socket = device.CreateRfcommSocketToServiceRecord(applicationUUID);
socket.Connect();
inReader = new BufferedReader(new InputStreamReader(socket.InputStream));
outReader = new BufferedWriter(new OutputStreamWriter(socket.OutputStream));
outReader.Write("hhhh");
outReader.Flush();
Thread.Sleep(5 * 1000);
var s = inReader.Ready();
inReader.Skip(0);
//close all
inReader.Close();
socket.Close();
outReader.Close();
The screen on the printer shows 'Working' and then back to ready and nothing gets printed out. As you see i am trying to print the text 'hhhh' do i have to append anything extra for the message. The printer is an RD-G80 Radall thermal printer.
Hope you can help I've been trying for a week now.
Thanks
Upvotes: 2
Views: 3659
Reputation: 51
I used something like the code below to send the cod to a Zebra Mobile Printer on a application that i worked some time ago. Its important to point that u have to know the right code pattern to send to the printer. Usually they have a documentation about it. In the> application, i send the product code to our backend-end and it retrieves and build the code pattern that i needed to send from my mobile application to the printer.
Code below. hope this helps you:
**
private enum msgRet
{
conectionERROR,
sendERROR,
sucess
}
public int Print(string codToSend, string Print)
{
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
BluetoothSocket btSocket = null;
UUID MY_UUID = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothDevice device = null;
Stream outStream = null;
int ret = -1;
try
{
MessagingCenter.Send(this, "Print");
var MacAdress = mBluetoothAdapter.BondedDevices.ToList().Find(x => x.Name == Print).Address;
if(string.IsNullOrEmpty(MacAdress))
return ret = (int)msgRet.sendERROR;
if (!mBluetoothAdapter.IsEnabled)
{
BluetoothAdapter mBluetoothAdapter =
BluetoothAdapter.DefaultAdapter;
if(!mBluetoothAdapter.IsEnabled)
{
mBluetoothAdapter.Enable();
}
}
device = mBluetoothAdapter.GetRemoteDevice(MacAdress);
btSocket = device.CreateInsecureRfcommSocketToServiceRecord(MY_UUID);
if (!btSocket.IsConnected)
{
btSocket.Connect();
}
if (btSocket.IsConnected)
{
Thread.Sleep(200);
try
{
byte[] msgBuffer = Encoding.ASCII.GetBytes(codToSend);
outStream = btSocket.OutputStream;
outStream.Write(msgBuffer, 0, msgBuffer.Length);
}
catch (Exception e)
{
Debug.WriteLine("sendERROR" + e.Message);
ret = (int)msgRet.sendERROR;
}
}
} catch (Exception ex) {
Debug.WriteLine("conectionERROR:" + ex.Message);
ret = (int)msgRet.conectionERROR;
}
finally
{
if (outStream != null) { outStream.Close(); outStream.Dispose();}
mBluetoothAdapter.Dispose();
if (btSocket!=null) { btSocket.Close(); btSocket.Dispose(); }
}
if (ret == -1) { ret = (int)msgRet.sucess; }
return ret;
}
Upvotes: 3