预览
代码
手机端效果
demoBluetooth.cs
demoBluetooth.Designer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smobiler.Core;
using Smobiler.Core.Controls;
namespace Smobiler.Tutorials.Plugins
{
partial class demoBluetooth : Smobiler.Core.Controls.MobileForm
{
public demoBluetooth() : base()
{
//This call is required by the SmobilerForm.
InitializeComponent();
}
private void title1_ImagePress(object sender, EventArgs e)
{
this.Close();
}
private void btnopen_Press(object sender, EventArgs e)
{
bluetooth1.Open((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
private void btnClose_Press(object sender, EventArgs e)
{
bluetooth1.Close((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
private void btnSearch_Press(object sender, EventArgs e)
{
bluetooth1.Search((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
{
Toast("success" + args.Name + " " + args.MacAddress);
MessageBox.Show(args.Name + " " + args.MacAddress);
}
});
}
private void btnConnect_Press(object sender, EventArgs e)
{
bluetooth1.Connect("00:0D:18:00:19:98", (abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
private void btnCloseConnect_Press(object sender, EventArgs e)
{
bluetooth1.CloseConnect((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
private void btnWriteMessage_Press(object sender, EventArgs e)
{
//发送的TSPL指令
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";
string print1 = "SIZE 50 mm,80 mm\r\n" + "GAP 2 mm,0 mm\r\n" + "TEXT 30,0,\"TST24.BF2 \",0,1,1,\"" + "Smobiler" + "\n" + "TEXT 30,60,\"ROMAN.TTF \",0,1,1,\"" + "110mm×300m" + "\r\n" + "TEXT 30,120,\"TST24.BF2 \",0,1,1,\"" + "石磨" + "\r\n" + "TEXT 30,180,\"ROMAN.TTF \",0,1,1,\"" + "上海石磨信息科技有限公司" + "\r\n" + "TEXT 30,240,\"ROMAN.TTF \",0,1,1,\"" + "Smobiler" + "\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" + "QRCODE 350,0,H,4,A,0,M6,S7,\"" + str + "\"\r\n" + "TEXT 350,240,\"TSS24.BF2 \",0,1,1,\"" + "页数 1" + "\n" + "PRINT 1,1\r\n";
bluetooth1.WriteData(System.Text.Encoding.GetEncoding("gbk").GetBytes(print1), (abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
private void bluetooth1_DataReceived(object sender, Smobiler.Plugins.BluetoothDataReceivedEventArgs e)
{
Toast(e.Data.ToString());
MessageBox.Show(System.Text.Encoding.GetEncoding("gbk").GetString(e.Data));
}
private void demoBluetooth_Load(object sender, EventArgs e)
{
timer1.Start();
btnGetStatus_Press(null, null);
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = bluetooth1.BluetoothStatus.ToString();
}
private void btnGetStatus_Press(object sender, EventArgs e)
{
bluetooth1.GetStatus((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
{
Toast(args.Status + " " + args.Name + " " + args.MacAddress);
MessageBox.Show(args.Status + " " + args.Name + " " + args.MacAddress);
}
});
}
private void btnSearchBle_Press(object sender, EventArgs e)
{
//参数uuidService:连接时的service uuid,需要蓝牙供应商提供
//参数uuidTransmitCharUuid:连接时发送内容的uuid,需要蓝牙供应商提供
//参数uuidReceiveCharUuid:连接时接收内容的uuid,如果与发送id相同,请填写相同id.需要蓝牙供应商提供
bluetooth1.SearchBle("", "", "", (abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
{
Toast("success" + args.Name + " " + args.MacAddress);
MessageBox.Show(args.Name + " " + args.MacAddress);
}
});
}
private void btnConnectBle_Press(object sender, EventArgs e)
{
//macAddress:连接蓝牙的mac地址,如"00:0D:18:00:19:98"
//uuidService:连接时的service uuid,需要蓝牙供应商提供
//uuidTransmitCharUuid:连接时发送内容的uuid,需要蓝牙供应商提供
//uuidReceiveCharUuid:连接时接收内容的uuid,如果与发送id相同,请填写相同id.需要蓝牙供应商提供
bluetooth1.ConnectBle("", "", "", "", (abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
private void btnCloseConnectBle_Press(object sender, EventArgs e)
{
bluetooth1.CloseConnectBle((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
private void btnWriteBleMessage_Press(object sender, EventArgs e)
{
//发送的TSPL指令
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";
string print1 = "SIZE 50 mm,80 mm\r\n" + "GAP 2 mm,0 mm\r\n" + "TEXT 30,0,\"TST24.BF2 \",0,1,1,\"" + "Smobiler" + "\n" + "TEXT 30,60,\"ROMAN.TTF \",0,1,1,\"" + "110mm×300m" + "\r\n" + "TEXT 30,120,\"TST24.BF2 \",0,1,1,\"" + "石磨" + "\r\n" + "TEXT 30,180,\"ROMAN.TTF \",0,1,1,\"" + "上海石磨信息科技有限公司" + "\r\n" + "TEXT 30,240,\"ROMAN.TTF \",0,1,1,\"" + "Smobiler" + "\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" + "QRCODE 350,0,H,4,A,0,M6,S7,\"" + str + "\"\r\n" + "TEXT 350,240,\"TSS24.BF2 \",0,1,1,\"" + "页数 1" + "\n" + "PRINT 1,1\r\n";
//BluetoothBleResponse.WRITE_TYPE_DEFAULT 表示不需要外网设备的回应
bluetooth1.WriteBleData(System.Text.Encoding.GetEncoding("gbk").GetBytes(print1), Smobiler.Plugins.BluetoothBleResponse.WRITE_TYPE_DEFAULT, (abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
Toast("success");
});
}
private void btnGetBleStatus_Press(object sender, EventArgs e)
{
bluetooth1.GetBleStatus((abj, args) =>
{
if (args.isError == true)
Toast(args.error);
else
{
Toast(args.Status + " " + args.Name + " " + args.MacAddress);
MessageBox.Show(args.Status + " " + args.Name + " " + args.MacAddress);
}
});
}
}
}
using System;
using Smobiler.Core;
namespace Smobiler.Tutorials.Plugins
{
partial class demoBluetooth : Smobiler.Core.Controls.MobileForm
{
#region "SmobilerForm generated code "
//SmobilerForm overrides dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
//NOTE: The following procedure is required by the SmobilerForm
//It can be modified using the SmobilerForm.
//Do not modify it using the code editor.
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.title1 = new Smobiler.Core.Controls.Title();
this.btnopen = new Smobiler.Core.Controls.Button();
this.btnClose = new Smobiler.Core.Controls.Button();
this.btnSearch = new Smobiler.Core.Controls.Button();
this.btnConnect = new Smobiler.Core.Controls.Button();
this.btnCloseConnect = new Smobiler.Core.Controls.Button();
this.btnWriteMessage = new Smobiler.Core.Controls.Button();
this.label1 = new Smobiler.Core.Controls.Label();
this.btnGetStatus = new Smobiler.Core.Controls.Button();
this.label2 = new Smobiler.Core.Controls.Label();
this.bluetooth1 = new Smobiler.Plugins.Bluetooth();
this.timer1 = new Smobiler.Core.Controls.Timer();
this.btnSearchBle = new Smobiler.Core.Controls.Button();
this.btnConnectBle = new Smobiler.Core.Controls.Button();
this.btnCloseConnectBle = new Smobiler.Core.Controls.Button();
this.btnWriteBleMessage = new Smobiler.Core.Controls.Button();
this.btnGetBleStatus = new Smobiler.Core.Controls.Button();
this.label3 = new Smobiler.Core.Controls.Label();
//
// title1
//
this.title1.ImageType = Smobiler.Core.Controls.ImageEx.ImageStyle.FontIcon;
this.title1.Name = "title1";
this.title1.ResourceID = "angle-left";
this.title1.Size = new System.Drawing.Size(300, 30);
this.title1.Text = "Bluetooth";
this.title1.ImagePress += new System.EventHandler(this.title1_ImagePress);
//
// btnopen
//
this.btnopen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(192)))), ((int)(((byte)(80)))));
this.btnopen.Location = new System.Drawing.Point(0, 48);
this.btnopen.Name = "btnopen";
this.btnopen.Size = new System.Drawing.Size(144, 38);
this.btnopen.Text = "Open";
this.btnopen.Press += new System.EventHandler(this.btnopen_Press);
//
// btnClose
//
this.btnClose.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(185)))), ((int)(((byte)(229)))), ((int)(((byte)(100)))));
this.btnClose.Location = new System.Drawing.Point(0, 102);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(144, 38);
this.btnClose.Text = "Close";
this.btnClose.Press += new System.EventHandler(this.btnClose_Press);
//
// btnSearch
//
this.btnSearch.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(67)))), ((int)(((byte)(80)))));
this.btnSearch.Location = new System.Drawing.Point(0, 166);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(144, 38);
this.btnSearch.Text = "Search";
this.btnSearch.Press += new System.EventHandler(this.btnSearch_Press);
//
// btnConnect
//
this.btnConnect.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(67)))), ((int)(((byte)(80)))));
this.btnConnect.Location = new System.Drawing.Point(0, 216);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(144, 38);
this.btnConnect.Text = "Connect";
this.btnConnect.Press += new System.EventHandler(this.btnConnect_Press);
//
// btnCloseConnect
//
this.btnCloseConnect.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(67)))), ((int)(((byte)(80)))));
this.btnCloseConnect.Location = new System.Drawing.Point(0, 280);
this.btnCloseConnect.Name = "btnCloseConnect";
this.btnCloseConnect.Size = new System.Drawing.Size(144, 38);
this.btnCloseConnect.Text = "CloseConnect";
this.btnCloseConnect.Press += new System.EventHandler(this.btnCloseConnect_Press);
//
// btnWriteMessage
//
this.btnWriteMessage.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(67)))), ((int)(((byte)(80)))));
this.btnWriteMessage.Location = new System.Drawing.Point(0, 332);
this.btnWriteMessage.Name = "btnWriteMessage";
this.btnWriteMessage.Size = new System.Drawing.Size(144, 38);
this.btnWriteMessage.Text = "WriteMessage";
this.btnWriteMessage.Press += new System.EventHandler(this.btnWriteMessage_Press);
//
// label1
//
this.label1.ForeColor = System.Drawing.Color.Maroon;
this.label1.Location = new System.Drawing.Point(132, 433);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(142, 35);
//
// btnGetStatus
//
this.btnGetStatus.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(67)))), ((int)(((byte)(80)))));
this.btnGetStatus.Location = new System.Drawing.Point(0, 387);
this.btnGetStatus.Name = "btnGetStatus";
this.btnGetStatus.Size = new System.Drawing.Size(144, 36);
this.btnGetStatus.Text = "GetStatus";
this.btnGetStatus.Press += new System.EventHandler(this.btnGetStatus_Press);
//
// label2
//
this.label2.FontSize = 14F;
this.label2.Location = new System.Drawing.Point(6, 433);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(120, 35);
this.label2.Text = "连接状态:";
//
// bluetooth1
//
this.bluetooth1.Name = "bluetooth1";
this.bluetooth1.DataReceived += new Smobiler.Plugins.BluetoothDataReceivedEventHandler(this.bluetooth1_DataReceived);
//
// timer1
//
this.timer1.Name = "timer1";
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// btnSearchBle
//
this.btnSearchBle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(76)))), ((int)(((byte)(176)))), ((int)(((byte)(250)))));
this.btnSearchBle.Location = new System.Drawing.Point(156, 166);
this.btnSearchBle.Name = "btnSearchBle";
this.btnSearchBle.Size = new System.Drawing.Size(144, 38);
this.btnSearchBle.Text = "SearchBle";
this.btnSearchBle.Press += new System.EventHandler(this.btnSearchBle_Press);
//
// btnConnectBle
//
this.btnConnectBle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(76)))), ((int)(((byte)(176)))), ((int)(((byte)(250)))));
this.btnConnectBle.Location = new System.Drawing.Point(156, 216);
this.btnConnectBle.Name = "btnConnectBle";
this.btnConnectBle.Size = new System.Drawing.Size(144, 38);
this.btnConnectBle.Text = "ConnectBle";
this.btnConnectBle.Press += new System.EventHandler(this.btnConnectBle_Press);
//
// btnCloseConnectBle
//
this.btnCloseConnectBle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(76)))), ((int)(((byte)(176)))), ((int)(((byte)(250)))));
this.btnCloseConnectBle.Location = new System.Drawing.Point(156, 280);
this.btnCloseConnectBle.Name = "btnCloseConnectBle";
this.btnCloseConnectBle.Size = new System.Drawing.Size(144, 38);
this.btnCloseConnectBle.Text = "CloseConnectBle";
this.btnCloseConnectBle.Press += new System.EventHandler(this.btnCloseConnectBle_Press);
//
// btnWriteBleMessage
//
this.btnWriteBleMessage.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(76)))), ((int)(((byte)(176)))), ((int)(((byte)(250)))));
this.btnWriteBleMessage.Location = new System.Drawing.Point(156, 332);
this.btnWriteBleMessage.Name = "btnWriteBleMessage";
this.btnWriteBleMessage.Size = new System.Drawing.Size(144, 38);
this.btnWriteBleMessage.Text = "WriteBleMessage";
this.btnWriteBleMessage.Press += new System.EventHandler(this.btnWriteBleMessage_Press);
//
// btnGetBleStatus
//
this.btnGetBleStatus.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(76)))), ((int)(((byte)(176)))), ((int)(((byte)(250)))));
this.btnGetBleStatus.Location = new System.Drawing.Point(156, 387);
this.btnGetBleStatus.Name = "btnGetBleStatus";
this.btnGetBleStatus.Size = new System.Drawing.Size(144, 36);
this.btnGetBleStatus.Text = "GetBleStatus";
this.btnGetBleStatus.Press += new System.EventHandler(this.btnGetBleStatus_Press);
//
// label3
//
this.label3.FontSize = 14F;
this.label3.Location = new System.Drawing.Point(164, 48);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(126, 92);
this.label3.Text = "使用Ble蓝牙通讯需填写相应的uuid\r\n连接返回true不等于连接上,仍需要GetStatus判断连接状态";
//
// demoBluetooth
//
this.Components.AddRange(new Smobiler.Core.Controls.MobileComponent[] {
this.bluetooth1,
this.timer1});
this.Controls.AddRange(new Smobiler.Core.Controls.MobileControl[] {
this.title1,
this.btnopen,
this.btnClose,
this.btnSearch,
this.btnConnect,
this.btnCloseConnect,
this.btnWriteMessage,
this.label1,
this.btnGetStatus,
this.label2,
this.btnSearchBle,
this.btnConnectBle,
this.btnCloseConnectBle,
this.btnWriteBleMessage,
this.btnGetBleStatus,
this.label3});
this.Load += new System.EventHandler(this.demoBluetooth_Load);
this.Name = "demoBluetooth";
}
#endregion
private Core.Controls.Title title1;
private Core.Controls.Button btnopen;
private Core.Controls.Button btnClose;
private Core.Controls.Button btnSearch;
private Core.Controls.Button btnConnect;
private Core.Controls.Button btnCloseConnect;
private Core.Controls.Button btnWriteMessage;
private Core.Controls.Label label1;
private Core.Controls.Button btnGetStatus;
private Core.Controls.Label label2;
private Smobiler.Plugins.Bluetooth bluetooth1;
private Core.Controls.Timer timer1;
private Core.Controls.Button btnSearchBle;
private Core.Controls.Button btnConnectBle;
private Core.Controls.Button btnCloseConnectBle;
private Core.Controls.Button btnWriteBleMessage;
private Core.Controls.Button btnGetBleStatus;
private Core.Controls.Label label3;
}
}