我是新手,C#才起步,就先拿著其他大神的程序練練手,跪請大神幫忙看一下是怎麼回事。
代碼是:using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Windows.Forms;
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace _12._2._2
{
public partial class Form1 : Form
{
BluetoothRadio radio = null;//藍牙適配器
string sendFileName = null;//發送文件名
BluetoothAddress sendAddress = null;//發送目的地址
ObexListener listener = null;//監聽器
string recDir = null;//接受文件存放目錄
Thread listenThread, sendThread;//發送/接收線程
public Form1()
{
InitializeComponent();
radio = BluetoothRadio.PrimaryRadio;//獲取當前PC的藍牙適配器
CheckForIllegalCrossThreadCalls = false;//不檢查跨線程調用
if (radio == null)//檢查該電腦藍牙是否可用
{
MessageBox.Show("這個電腦藍牙不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
labelRecDir.Text = recDir;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonSelectBluetooth_Click(object sender, EventArgs e)
{
SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog();
dialog.ShowRemembered = true;//顯示已經記住的藍牙設備
dialog.ShowAuthenticated = true;//顯示認證過的藍牙設備
dialog.ShowUnknown = true;//顯示位置藍牙設備
if (dialog.ShowDialog() == DialogResult.OK)
{
sendAddress = dialog.SelectedDevice.DeviceAddress;//獲取選擇的遠程藍牙地址
labelAddress.Text = "地址:" + sendAddress.ToString() + " 設備名:" + dialog.SelectedDevice.DeviceName;
}
}
private void buttonSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
sendFileName = dialog.FileName;//設置文件名
labelPath.Text =Path.GetFileName(sendFileName);
}
}
private void buttonSend_Click(object sender, EventArgs e)
{
sendThread = new Thread(sendFile);//開啟發送文件線程
sendThread.Start();
}
private void sendFile()//發送文件方法
{
Cursor.Current = Cursors.WaitCursor;
System.Uri uri = new Uri("obex://" + dialog.SelectedDevice.DeviceAddress.ToString() + "/" + System.IO.Path.GetFileName(dialog.FileName));
ObexWebResponse response = null;
try
{
ObexWebRequest request = new ObexWebRequest(uri);
buttonSend.Enabled = false;
request.ReadFile(sendFileName);//發送文件
labelInfo.Text = "開始發送!";
response = request.GetResponse() as ObexWebResponse;//獲取回應
labelInfo.Text = "發送完成!";
}
catch (System.Exception ex)
{
MessageBox.Show("發送失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
labelInfo.Text = "發送失敗!";
}
finally
{
if (response != null)
{
response.Close();
buttonSend.Enabled = true;
}
}
}
private void buttonselectRecDir_Click(object sender, EventArgs e)//選擇接受目錄
{
}
private void buttonListen_Click(object sender, EventArgs e)
{
if (listener == null || !listener.IsListening)
{
radio.Mode = RadioMode.Discoverable;//設置本地藍牙可被檢測
listener = new ObexListener(ObexTransport.Bluetooth);//創建監聽
listener.Start();
if (listener.IsListening)
{
buttonListen.Text = "停止";
labelRecInfo.Text = "開始監聽";
listenThread = new Thread(receiveFile);//開啟監聽線程
listenThread.Start();
}
}
else
{
listener.Stop();
buttonListen.Text = "監聽";
labelRecInfo.Text = "停止監聽";
}
}
private void receiveFile()//收文件方法
{
ObexListenerContext context = null;
ObexListenerRequest request = null;
while (listener.IsListening)
{
context = listener.GetContext();//獲取監聽上下文
if (context == null)
{
break;
}
request = context.Request;//獲取請求
string uriString = Uri.UnescapeDataString(request.RawUrl);//將uri轉換成字符串
string recFileName = recDir + uriString;
request.WriteFile(recFileName);//接收文件
labelRecInfo.Text = "收到文件" + uriString.TrimStart(new char[] { '/' });
}
}
private void labelRecDir_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (sendThread != null)
{
sendThread.Abort();
}
if (listenThread != null)
{
listenThread.Abort();
}
if (listener != null && listener.IsListening)
{
listener.Stop();
}
}
}
}
http://www.tuicool.com/articles/iYjYn2