📄 frmmotherserver.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using InTheHand.Net;
using System.Threading;
using System.Data.SqlServerCe;
namespace Surance.MotherDevice
{
public partial class FrmMotherServer : Form
{
private BluetoothClient btClient;
private bool isSearched = false;
private bool isStart = false;
private BluetoothDeviceInfo[] btInfos = null;
private Guid childService = new Guid("{7A51FDC2-FDDF-4c9b-AFFC-11BF291BF93A}");
private Guid motherService = new Guid("{7A51FDC2-FDDF-4c9b-AFFC-11BF291BF93B}");
private BluetoothListener listener;
const int MAX_CHILDREN = 6;
public FrmMotherServer()
{
InitializeComponent();
}
private void FrmMotherServer_Load(object sender, EventArgs e)
{
SetBluetoothDiscoverable();
SetMenuStatus();
Thread t=new Thread(new ThreadStart(StartListen));
t.Start();
}
void StartListen()
{
listener = new BluetoothListener(motherService);
listener.Start();
}
#region Private Method
private void SetBluetoothDiscoverable()
{
BluetoothRadio br = BluetoothRadio.PrimaryRadio;
br.Mode = RadioMode.Discoverable;
btClient = new BluetoothClient();
}
private void SetMenuStatus()
{
this.menuItemSearch.Enabled = !isStart;
this.menuItemStartMonitor.Enabled = isSearched;
this.menuItemEndMonitor.Enabled = !isStart;
this.menuItemExit.Enabled = !isStart;
}
private void BindData()
{
Cursor.Current = Cursors.WaitCursor;
btInfos = btClient.DiscoverDevices(MAX_CHILDREN);
foreach (BluetoothDeviceInfo currentInfo in btInfos)
{
DataRow dr = this.dataSetChildren.Tables[0].NewRow();
dr[0] = currentInfo.DeviceName;
dr[1] = "Conneted";
dr[2] = currentInfo.DeviceAddress;
this.dataSetChildren.Tables[0].Rows.Add(dr);
}
Cursor.Current = Cursors.Default;
//DataRow dr = this.dataSetChildren.Tables[0].NewRow();
//dr[0] = "jacy";
//dr[1] = "Connected";
//dr[2] = "000000000000000";
//this.dataSetChildren.Tables[0].Rows.Add(dr);
//DataRow dr1 = this.dataSetChildren.Tables[0].NewRow();
//dr1[0] = "Lily";
//dr1[1] = "Connected";
//dr1[2] = "000000000000001";
//this.dataSetChildren.Tables[0].Rows.Add(dr1);
//DataRow dr2 = this.dataSetChildren.Tables[0].NewRow();
//dr2[0] = "Birds";
//dr2[1] = "Connected";
//dr2[2] = "000000000000002";
//this.dataSetChildren.Tables[0].Rows.Add(dr2);
//DataRow dr3 = this.dataSetChildren.Tables[0].NewRow();
//dr3[0] = "Lucy";
//dr3[1] = "Connected";
//dr3[2] = "000000000000003";
//this.dataSetChildren.Tables[0].Rows.Add(dr3);
}
private void ListenLoop()
{
foreach (BluetoothDeviceInfo oldCurrentInfo in btInfos)
{
btClient.Connect(oldCurrentInfo.DeviceAddress, childService);
if (!btClient.Connected)
{
MessageBox.Show(oldCurrentInfo.DeviceName + "Disconnected!");
}
}
}
#endregion
private void menuItemSearch_Click(object sender, EventArgs e)
{
BindData();
if (this.btInfos.Length > 0)
{
isSearched = true;
}
SetMenuStatus();
}
private void menuItemStartMonitor_Click(object sender, EventArgs e)
{
isStart = true;
if (isStart)
{
this.labelMsg.Text = "status:start monitor!";
Thread t = new Thread(new ThreadStart(ListenLoop));
t.Start();
}
this.SetMenuStatus();
}
private void menuItemExit_Click(object sender, EventArgs e)
{
listener.Stop();
Application.Exit();
}
private void menuItemEndMonitor_Click(object sender, EventArgs e)
{
isStart = false;
MessageBox.Show("Monitor have stoped!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -