资源简介
Xamarin蓝牙打印,已经封装好了代码,直接移植过去使用,可自行扩展,按照使用例子直接调用打印。注意:需要打开设备的蓝牙。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Bluetooth;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Util;
namespace MshSelf.Helper
{
///
/// 蓝牙打印帮助类
///
public class BluetoothUtil
{
private static UUID PRINTER_UUID = UUID.FromString(“00001101-0000-1000-8000-00805F9B34FB“);
private static string Innerprinter_Address = “00:11:22:33:44:55“;
private static BluetoothSocket bluetoothSocket;
private static BluetoothAdapter getBTAdapter()
{
return BluetoothAdapter.DefaultAdapter;
}
private static BluetoothDevice getDevice(BluetoothAdapter bluetoothAdapter)
{
BluetoothDevice innerprinter_device = null;
List devices = bluetoothAdapter.BondedDevices.ToList();
foreach (BluetoothDevice device in devices)
{
if (device.Address.Equals(Innerprinter_Address))
{
innerprinter_device = device;
break;
}
}
return innerprinter_device;
}
private static BluetoothSocket getSocket(BluetoothDevice device)
{
BluetoothSocket socket = null;
socket = device.CreateRfcommSocketToServiceRecord(PRINTER_UUID);
socket.Connect();
return socket;
}
/**
* 连接蓝牙
*
* @param context
* @return
*/
public static bool connectBlueTooth(Context context)
{
if (bluetoothSocket == null)
{
if (getBTAdapter() == null)
{
return false;
}
if (!getBTAdapter().IsEnabled)
{
return false;
}
BluetoothDevice device;
if ((device = getDevice(getBTAdapter())) == null)
{
return false;
}
try
{
bluetoothSocket = getSocket(device);
}
catch (Exception e)
{
return false;
}
}
return true;
}
/**
* 断开蓝牙
*/
public static void disconnectBlueTooth(Context context)
{
if (bluetoothSocket != null)
{
try
{
Stream mOut = bluetoothSocket.OutputStream;
mOut.Close();
bluetoothSocket.Close();
bluetoothSocket = null;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3881 2018-07-18 19:12 Xamarin蓝牙打印例子\BluetoothUtil.cs
文件 7029 2018-07-18 19:12 Xamarin蓝牙打印例子\BytesUtil.cs
文件 18072 2018-07-18 19:12 Xamarin蓝牙打印例子\ESCUtil.cs
文件 11690 2018-07-18 19:12 Xamarin蓝牙打印例子\PrinterHelper.cs
文件 98 2018-08-13 12:49 Xamarin蓝牙打印例子\使用例子.txt
目录 0 2018-08-13 12:48 Xamarin蓝牙打印例子
----------- --------- ---------- ----- ----
40770 6
评论
共有 条评论