本文简述如何在Smobiler中使用Bluetooth。
Step 1. 新建一个SmobilerForm窗体,并在窗体中加入Button和Bluetooth,布局如下
Button的点击事件代码:
/// <summary>
/// 关闭蓝牙
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Press(object sender, EventArgs e)
{
bluetooth1.Close((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
/// <summary>
/// 打开蓝牙
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Press(object sender, EventArgs e)
{
bluetooth1.Open((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
string macAdress = "";
/// <summary>
/// 搜索蓝牙设备
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Press(object sender, EventArgs e)
{
bluetooth1.Search((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
{
Toast("success" + args.Name + " " + args.MacAddress);
macAdress = args.MacAddress;
}
});
}
/// <summary>
/// 连接蓝牙
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Press(object sender, EventArgs e)
{
//第一个参数为蓝牙设备MAC地址
bluetooth1.Connect(macAdress, (abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
/// <summary>
/// 关闭蓝牙
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Press(object sender, EventArgs e)
{
bluetooth1.CloseConnect((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
/// <summary>
/// 发送蓝牙信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Press(object sender, EventArgs e)
{
string str = "690000001";
string print = "SIZE 80 mm,40 mm\r\n" + "REFERENCE 0,0\r\n" + "SPEED 4.0\r\n" + "DENSITY 8\r\n" + "SET PEEL OFF\r\n" + "SET CUTTER OFF\r\n" + "SET TEAR ON\r\n" + "DIRECTION 0\r\n" + "SHIFT 0\r\n" + "OFFSET 0 mm\r\n" + "CLS\r\n" + "BARCODE 20,20,\"128M\",160,1,0,4,12,\"" + str + "\"\r\n" + "TEXT 20,190,\"ARIAL.TTF\",0,15,15,\"" + str + "\"\n" + "PRINT 3,1\r\n";
bluetooth1.WriteData(System.Text.Encoding.UTF8.GetBytes(print), (abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
/// <summary>
///获取蓝牙状态,蓝牙是否连接成功需要GetStatus获取状态来判断
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Press(object sender, EventArgs e)
{
bluetooth1.GetStatus((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
{
Toast(args.Status + " " + args.Name + " " + args.MacAddress);
label2.Text = args.Status.ToString();
}
});
}
在设计器中选中Bluetooth添加事件
private void bluetooth1_DataReceived(object sender, Smobiler.Plugins.BluetoothDataReceivedEventArgs e)
{
//e.Data 接收到的数据; e.Deviceid 蓝牙设备id; 当连接多个蓝牙时,会多次触发该事件,可根据e.Deviceid区分蓝牙设备
MessageBox.Show(System.Text.Encoding.GetEncoding("gbk").GetString(e.Data));
}
Step 2. 运行服务端后,手机上安装官网的调试工具进行测试,效果如下
若是使用云平台上自行打包的客户端, 需要添加相应插件后再打包客户端。
补充
以上代码可用于连接经典蓝牙,BLE蓝牙请使用以下方法
方法 | 方法说明 |
---|---|
CloseConnectBle | 断开连接上的低功耗蓝牙设备 |
ConnectBle | 连接指定低功耗蓝牙 |
SearchBle | 查找并连接低功耗蓝牙 |
WriteBleData | 发送低功耗蓝牙信息 |
demo 参考 https://github.com/comsmobiler/SmobilerTutorials/blob/master/Source/Plugins/demoBluetooth.cs