⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listenerform.cs

📁 蓝牙遥控器的代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using OpenNETCF.Net.Sockets;
using OpenNETCF.Net.Bluetooth;
using System.Net.Sockets;

namespace OpenNETCF
{
	/// <summary>
	/// Processes incoming commands
	/// </summary>
	public class RemoteListenerForm : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private OpenNETCF.Net.Sockets.BluetoothListener bl;

		//unique service identifier
		private Guid ServiceGuid = new Guid("{7A51FDC2-FDDF-4c9b-AFFC-98BCD91BF93B}");
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.Label lblVer;
		private System.Windows.Forms.NotifyIcon trayIcon;
		private System.Windows.Forms.PictureBox pictureBox1;
		private bool listening = true;

		public RemoteListenerForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(RemoteListenerForm));
			this.trayIcon = new System.Windows.Forms.NotifyIcon(this.components);
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.lblVer = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// trayIcon
			// 
			this.trayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("trayIcon.Icon")));
			this.trayIcon.Text = "Bluetooth Remote";
			this.trayIcon.Visible = true;
			this.trayIcon.Click += new System.EventHandler(this.trayIcon_Click);
			// 
			// lblVer
			// 
			this.lblVer.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblVer.Location = new System.Drawing.Point(72, 16);
			this.lblVer.Name = "lblVer";
			this.lblVer.Size = new System.Drawing.Size(232, 24);
			this.lblVer.TabIndex = 0;
			this.lblVer.Text = "OpenNETCF Bluetooth Remote v1.0";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(8, 0);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(48, 48);
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			// 
			// RemoteListenerForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(312, 46);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.lblVer);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "RemoteListenerForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "BluetoothRemote";
			this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
			this.Closing += new System.ComponentModel.CancelEventHandler(this.RemoteListenerForm_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new RemoteListenerForm());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			BluetoothRadio br = BluetoothRadio.PrimaryRadio;
			br.Mode = RadioMode.Discoverable;

			bl = new BluetoothListener(ServiceGuid);
			bl.Start();

			System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ListenLoop));
			t.Start();
		}

		private void RemoteListenerForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			listening = false;
			bl.Stop();
		}

		private void ListenLoop()
		{
			byte[] buffer = new byte[4];
			int received = 0;

			while(listening)
			{
				BluetoothClient bc;
				System.IO.Stream ns;
				try
				{
					bc = bl.AcceptBluetoothClient();
					ns = bc.GetStream();
				}
				catch
				{
					break;
				}
				

				//keep connection open
				while(listening)
				{
					try
					{
						received = ns.Read(buffer, 0, buffer.Length);
					}
					catch
					{
						break;
					}
				
					if(received > 0)
					{
						string command = "";

						Keys keycode = (Keys)BitConverter.ToInt16(buffer, 0);
						
						switch(keycode)
						{
							case Keys.Up:
								command = "{UP}";
								break;
							case Keys.Down:
								command = "{DOWN}";
								break;
							case Keys.Left:
								command = "{LEFT}";
								break;
							case Keys.Right:
								command = "{RIGHT}";
								break;
							case Keys.Enter:
								command = "{ENTER}";
								break;
							case Keys.D1:
								command = "1";
								break;
							case Keys.D2:
								command = "2";
								break;
							case Keys.D3:
								command = "3";
								break;
							case Keys.D4:
								command = "4";
								break;
							case Keys.D5:
								command = "5";
								break;
							case Keys.D6:
								command = "6";
								break;
							case Keys.D7:
								command = "7";
								break;
							case Keys.D8:
								command = "8";
								break;
							case Keys.D9:
								command = "9";
								break;
							case Keys.D0:
								command = "0";
								break;
						}
						System.Windows.Forms.SendKeys.SendWait(command);
					}
				}

				try
				{
					bc.Close();
				}
				catch
				{
				}
			}

		}

		private void trayIcon_Click(object sender, System.EventArgs e)
		{
			this.WindowState = FormWindowState.Normal;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -