📄 bluetooth_manager.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using InTheHand.IO;
using InTheHand.Net;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;
using System.Threading ;
namespace Bluetooth_Manager
{
public class Bluetooth_Manager : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
InTheHand.Net.BluetoothAddress[] address_array = new BluetoothAddress [1000];
private Thread search_thread ;
private System.Windows.Forms.ListBox listBox_devices;
private System.Windows.Forms.Button button_sendfile;
private System.Windows.Forms.Button button_mac;
private System.Windows.Forms.Button button_find_devices;
private Thread send_thread ;
public Bluetooth_Manager()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Bluetooth_Manager));
this.button_sendfile = new System.Windows.Forms.Button();
this.button_mac = new System.Windows.Forms.Button();
this.button_find_devices = new System.Windows.Forms.Button();
this.listBox_devices = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button_sendfile
//
this.button_sendfile.BackColor = System.Drawing.Color.PapayaWhip;
this.button_sendfile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button_sendfile.Location = new System.Drawing.Point(264, 352);
this.button_sendfile.Name = "button_sendfile";
this.button_sendfile.Size = new System.Drawing.Size(176, 40);
this.button_sendfile.TabIndex = 10;
this.button_sendfile.Text = "Send file via OBEX";
this.button_sendfile.Click += new System.EventHandler(this.button_sendfile_Click);
//
// button_mac
//
this.button_mac.BackColor = System.Drawing.Color.PapayaWhip;
this.button_mac.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button_mac.Location = new System.Drawing.Point(264, 232);
this.button_mac.Name = "button_mac";
this.button_mac.Size = new System.Drawing.Size(176, 40);
this.button_mac.TabIndex = 7;
this.button_mac.Text = "Show MAC";
this.button_mac.Click += new System.EventHandler(this.button_mac_Click);
//
// button_find_devices
//
this.button_find_devices.BackColor = System.Drawing.Color.PapayaWhip;
this.button_find_devices.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button_find_devices.Location = new System.Drawing.Point(264, 120);
this.button_find_devices.Name = "button_find_devices";
this.button_find_devices.Size = new System.Drawing.Size(176, 40);
this.button_find_devices.TabIndex = 4;
this.button_find_devices.Text = "Find Devices";
this.button_find_devices.Click += new System.EventHandler(this.button_find_devices_Click);
//
// listBox_devices
//
this.listBox_devices.Location = new System.Drawing.Point(40, 32);
this.listBox_devices.Name = "listBox_devices";
this.listBox_devices.Size = new System.Drawing.Size(176, 420);
this.listBox_devices.TabIndex = 12;
//
// Bluetooth_Manager
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(528, 485);
this.Controls.Add(this.listBox_devices);
this.Controls.Add(this.button_find_devices);
this.Controls.Add(this.button_mac);
this.Controls.Add(this.button_sendfile);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Bluetooth_Manager";
this.Text = "Bluetooth Manager";
this.Load += new System.EventHandler(this.Bluetooth_Manager_Load);
this.ResumeLayout(false);
}
#endregion
private void Bluetooth_Manager_Load(object sender, System.EventArgs e)
{
}
[STAThread]
static void Main()
{
Application.Run(new Bluetooth_Manager());
}
private void search ()
{
this.listBox_devices.Items.Clear();
InTheHand.Net.Sockets.BluetoothClient bc = new InTheHand.Net.Sockets.BluetoothClient ();
InTheHand.Net.Sockets.BluetoothDeviceInfo[] array = bc.DiscoverDevices();
for(int i = 0 ; i<array.Length;i++)
{
this.address_array[i] = array[i].DeviceAddress;
this.listBox_devices.Items.Add(array[i].DeviceName);
}
this.button_find_devices.Enabled=true;
}
private void button_find_devices_Click(object sender, System.EventArgs e)
{
this.button_find_devices.Enabled=false;
this.search_thread = new Thread (new ThreadStart(search));
this.search_thread.Start();
}
private void button_mac_Click(object sender, System.EventArgs e)
{
if(this.listBox_devices.SelectedIndex == -1)
{
MessageBox.Show("Please select a device.");
return ;
}
int index = this.listBox_devices.SelectedIndex ;
string mac = this.address_array[index].ToString();
string nap = this.address_array[index].Nap.ToString();
string sap = this.address_array[index].Sap.ToString();
MessageBox.Show(mac+" " + nap + " " + sap);
}
private void button_sendfile_Click(object sender, System.EventArgs e)
{
if(this.listBox_devices.SelectedIndex == -1)
{
MessageBox.Show("Please select a device.");
return ;
}
this.send_thread = new Thread ( new ThreadStart(sendfile));
this.button_sendfile.Enabled = false ;
this.send_thread.Start();
}
private void sendfile ()
{
int index = this.listBox_devices.SelectedIndex ;
InTheHand.Net.BluetoothAddress address = this.address_array[index];
System.Uri uri = new Uri("obex://" + address.ToString() + "/" + "sample.txt");
ObexWebRequest request = new ObexWebRequest(uri);
request.ReadFile("sample.txt");/////
ObexWebResponse response = (ObexWebResponse)request.GetResponse();
response.Close();
this.button_sendfile.Enabled = true ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -