📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.IO;
using Microsoft.WindowsMobile.SharedSource.Bluetooth;
namespace BluetoothTest
{
public partial class Form1 : Form
{
private MsgWindow MsgWin;
BluetoothRadio btr;
[DllImport("BthHelper")]
private static extern bool PopulateBtDevList(IntPtr hWnd);
public Form1()
{
btr = new BluetoothRadio();
#region Service code
InitializeComponent();
MsgWin = new MsgWindow(this);
myServiceGUID = new Guid("{BE6D2F78-137F-49b3-A7BF-8FB00740D050}");
bts = new BluetoothService(myServiceGUID);
bts.Start();
#endregion
}
private void RefreshData()
{
Cursor.Current = Cursors.WaitCursor;
// Get the radio state
RadioState.Text = btr.BluetoothRadioMode.ToString();
// Enumerate paired devices
PairedDevices.Items.Clear();
foreach (BluetoothDevice btd in btr.PairedDevices)
{
PairedDevices.Items.Add(btd.Name);
}
// Refresh In Range list
PopulateBtDevList(MsgWin.Hwnd);
Cursor.Current = Cursors.Default;
}
private void ToggleRadio_Click(object sender, EventArgs e)
{
// Change the current Radio state and update the display
switch (btr.BluetoothRadioMode)
{
case BluetoothRadioMode.Discoverable:
btr.BluetoothRadioMode = BluetoothRadioMode.Off;
break;
case BluetoothRadioMode.Off:
btr.BluetoothRadioMode = BluetoothRadioMode.On;
break;
case BluetoothRadioMode.On:
btr.BluetoothRadioMode = BluetoothRadioMode.Discoverable;
break;
}
this.RadioState.Text = btr.BluetoothRadioMode.ToString();
}
unsafe internal void RespondToMessage(int p, int p_2)
{
StringBuilder sb = new StringBuilder();
char* StringPtr = (char*)p_2;
while (*StringPtr!='\0')
{
sb.Append(*StringPtr);
StringPtr++;
}
InRange.Items.Add(sb.ToString());
}
#region Plumbing code
private void menuItem2_Click(object sender, EventArgs e)
{
RefreshData();
}
private void menuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (bts.ConnectionPending)
{
NetworkStream ns = bts.AcceptConnection();
StreamReader sr = new StreamReader(ns);
String Message = sr.ReadLine();
MessageBox.Show(Message, "Recieved Message");
ns.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
RefreshData();
timer1.Interval = 1000;
timer1.Enabled = true;
}
private BluetoothService bts;
private Guid myServiceGUID;
#endregion
}
public class MsgWindow : MessageWindow
{
// Assign integers to messages.
// Note that custom Window messages start at WM_USER = 0x400.
public const int WM_USER = 0x0400;
// Create an instance of the form.
private Form1 msgform;
// Save a reference to the form so it can
// be notified when messages are received.
public MsgWindow(Form1 msgform)
{
this.msgform = msgform;
}
// Override the default WndProc behavior to examine messages.
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
// If message is of interest, invoke the method on the form that
// functions as a callback to perform actions in response to the message.
case WM_USER+101:
this.msgform.RespondToMessage((int)msg.WParam, (int)msg.LParam);
break;
}
// Call the base WndProc method
// to process any messages not handled.
base.WndProc(ref msg);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -