📄 frmchildren.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 InTheHand.Windows.Forms;
namespace Surance.ChildrenDevice
{
enum mStatus
{
connected,stopSearcing,disconnected
}
public partial class FrmChildren : Form
{
private Guid childService = new Guid("{7A51FDC2-FDDF-4c9b-AFFC-11BF291BF93A}");
private Guid motherService = new Guid("{7A51FDC2-FDDF-4c9b-AFFC-11BF291BF93B}");
private BluetoothDeviceInfo[] btInfos;
private BluetoothDeviceInfo mother;
private bool isStart;
private bool isSearched;
private BluetoothListener listener;
const int MAX_MOTHER = 3;
public FrmChildren()
{
InitializeComponent();
}
private void FrmChildren_Load(object sender, EventArgs e)
{
SetBluetoothDiscoverable();
SetStatus(mStatus.stopSearcing);
BindData();
if (btInfos.Length > 0)
isSearched = true;
SetMenuStatus();
Thread t = new Thread(new ThreadStart(StartListen));
t.Start();
}
void StartListen()
{
listener = new BluetoothListener(childService);
listener.Start();
}
private void SetBluetoothDiscoverable()
{
BluetoothRadio br = BluetoothRadio.PrimaryRadio;
br.Mode = RadioMode.Discoverable;
}
private void SetStatus(mStatus status)
{
switch (status)
{
case mStatus.connected:lbSate.Text="Mother is around you!";break;
case mStatus.disconnected:lbSate.Text="Mother is out of range";break;
case mStatus.stopSearcing:lbSate.Text="press 'Start' to work";break;
}
}
private void SetMenuStatus()
{
this.menuItemStart.Enabled = isSearched;
this.comboBoxServerList.Enabled = !isStart;
}
private void BindData()
{
Cursor.Current = Cursors.WaitCursor;
btInfos = new BluetoothClient().DiscoverDevices(MAX_MOTHER);
this.comboBoxServerList.DataSource = btInfos;
this.comboBoxServerList.DisplayMember = "DeviceName";
// this.comboBoxServerList.ValueMember = "DeviceAddress";
Cursor.Current = Cursors.Default;
//DataRow dr = this.dataSetMother.Tables[0].NewRow();
//dr[0] = 1;
//dr[1] = "00000000000000000";
//dr[2] = "234322-34234-3242-42-324234-4";
//dr[3] = "White Mother";
//this.dataSetMother.Tables[0].Rows.Add(dr);
//DataRow dr1 = this.dataSetMother.Tables[0].NewRow();
//dr1[0] = 2;
//dr1[1] = "00000000000000000";
//dr1[2] = "234322-34234-3242-42-324234-4";
//dr1[3] = "Benjamin Mother";
//this.dataSetMother.Tables[0].Rows.Add(dr1);
}
private void DiscoverLoop()
{
while (isStart)
{
BluetoothClient c = new BluetoothClient();
c.Connect(new BluetoothEndPoint(mother.DeviceAddress, motherService));
if(c.Connected)
SetStatus(mStatus.connected);
else
SetStatus(mStatus.disconnected);
}
}
private void menuItemStart_Click(object sender, EventArgs e)
{
isStart = true;
Thread t = new Thread(new ThreadStart(DiscoverLoop));
t.Start();
}
private void menuItemExit_Click(object sender, EventArgs e)
{
listener.Stop();
Application.Exit();
}
private void comboBoxServerList_SelectedIndexChanged(object sender, EventArgs e)
{
mother = (BluetoothDeviceInfo)this.comboBoxServerList.SelectedItem;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -