我正在做一个跨平台应用程序,我需要通过蓝牙打印机打印一组数据。为此,我已经通过互联网找到了一种从 android 打印的方法。这是我实现的代码:
mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
if (mBluetoothAdapter == null)
{
await MyFavHelper.InformUser("No bluetooth adapter found", "Bluetooth");
return;
}
else
{
await MyFavHelper.InformUser("Bluetooth found", "Success");
}
if (!mBluetoothAdapter.IsEnabled)
{
mBluetoothAdapter.Enable();
}
else
{
await MyFavHelper.InformUser("Bluetooth Enabled", "Success");
}
ICollection<BluetoothDevice> pairedDevices = mBluetoothAdapter.BondedDevices;
if (pairedDevices.Count > 0)
{
foreach (BluetoothDevice device in pairedDevices)
{
if (device.Name.Contains("TSP"))
{
mmDevice = device;
break;
}
}
}
else
{
await MyFavHelper.InformUser("Paired Devices not found", "Bluetooth");
return;
}
ParcelUuid uuid = mmDevice.GetUuids().ElementAt(0);
if (mmDevice == null)
{
await MyFavHelper.InformUser("No Device Found", "Sorry");
}
mmsSocket = mmDevice.CreateInsecureRfcommSocketToServiceRecord(uuid.Uuid);
mmsSocket.Connect();
if (mmsSocket.IsConnected)
{
await MyFavHelper.InformUser("Socket Connected Successfully", "Success");
}
else
{
await MyFavHelper.InformUser("Socket Not Connected Successfully", "Sorry");
}
var datastream = mmsSocket.OutputStream;
byte[] byteArray = Encoding.ASCII.GetBytes("Sample Text");
datastream.Write(byteArray, 0, byteArray.Length);
我成功到“套接字连接成功”。但我无法从打印机打印数据。我正在使用型号为“TSP 100 III BI”的star micronics 打印机。
如果我遗漏任何东西,谁能给我一个建议。
这对我有很大帮助。在此先感谢。