程式碼 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using InTheHand.Net;
using InTheHand.Net.Sockets;
using System.IO;
using System.Threading;
using InTheHand.Net.Bluetooth;
using System.Net.Sockets;
using InTheHand.Windows.Forms;
//using System.Diagnostics;
using InTheHand.Net.Sockets;
using System.IO;
using System.Threading;
using InTheHand.Net.Bluetooth;
using System.Net.Sockets;
using InTheHand.Windows.Forms;
//using System.Diagnostics;
using Brecham.Obex;
using Brecham.Obex.Objects;
using System.Net;
using Brecham.Obex.Objects;
using System.Net;
//using DragAndDropFileControlLibrary;//托放作品
namespace Ex1
{
public partial class Form1 : Form
{
//BluetoothClient client;
//Stack<ListViewItem[]> previousItemsStack = new Stack<ListViewItem[]>();
//ObexClientSession session;
//string localAds;
//BluetoothAddress mac = null;//地址
//BluetoothEndPoint localEndpoint = null;//送出端
//BluetoothClient localClient = null;//接收端
//BluetoothComponent localComponent = null;
//BluetoothAddress mac = null;//地址
//BluetoothEndPoint localEndpoint = null;//送出端
//BluetoothClient localClient = null;//接收端
//BluetoothComponent localComponent = null;
BluetoothRadio radio = null;//蓝牙适配器
string sendFileName = null;//发送文件名
BluetoothAddress sendAddress = null;//发送目的地址
ObexListener listener = null;//监听器
string recDir = null;//接受文件存放目录
Thread listenThread, sendThread;//发送/接收线程
public Form1()
{
string sendFileName = null;//发送文件名
BluetoothAddress sendAddress = null;//发送目的地址
ObexListener listener = null;//监听器
string recDir = null;//接受文件存放目录
Thread listenThread, sendThread;//发送/接收线程
public Form1()
{
radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器
}
private void Connect()
{
{
using (SelectBluetoothDeviceDialog bldialog = new SelectBluetoothDeviceDialog())// SelectBluetoothDeviceDialog是一个InTheHand.Net.Personal提供的窗体,用于选择蓝牙设备
{
{
if (bldialog.ShowDialog() == DialogResult.OK)
{
sendAddress = bldialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙地址
label2.Text = "地址:" + sendAddress.ToString() + " 設備名:" + bldialog.SelectedDevice.DeviceName;
}
}
}
{
sendAddress = bldialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙地址
label2.Text = "地址:" + sendAddress.ToString() + " 設備名:" + bldialog.SelectedDevice.DeviceName;
}
}
}
private void sendFile()//发送文件方法
{
ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName));//创建网络请求
WebResponse response = null;
try
{
button發送.Enabled = false;
request.ReadFile(sendFileName);//发送文件
label發送狀態.Text = "開始發送!";
response = request.GetResponse();//获取回应
label發送狀態.Text = "發送完成!";
}
catch (System.Exception ex)
{
MessageBox.Show("發送失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
label發送狀態.Text = "發送失敗!";
}
finally
{
if (response != null)
{
response.Close();
button發送.Enabled = true;
}
}
}
{
ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName));//创建网络请求
WebResponse response = null;
try
{
button發送.Enabled = false;
request.ReadFile(sendFileName);//发送文件
label發送狀態.Text = "開始發送!";
response = request.GetResponse();//获取回应
label發送狀態.Text = "發送完成!";
}
catch (System.Exception ex)
{
MessageBox.Show("發送失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
label發送狀態.Text = "發送失敗!";
}
finally
{
if (response != null)
{
response.Close();
button發送.Enabled = true;
}
}
}
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 = "停止";
label1.Text = "開始監聽";
listenThread = new Thread(receiveFile);//开启监听线程
listenThread.Start();
}
}
else
{
listener.Stop();
buttonListen.Text = "監聽";
label1.Text = "停止監聽";
}
}
{
if (listener == null || !listener.IsListening)
{
radio.Mode = RadioMode.Discoverable;//设置本地蓝牙可被检测
listener = new ObexListener(ObexTransport.Bluetooth);//创建监听
listener.Start();
if (listener.IsListening)
{
buttonListen.Text = "停止";
label1.Text = "開始監聽";
listenThread = new Thread(receiveFile);//开启监听线程
listenThread.Start();
}
}
else
{
listener.Stop();
buttonListen.Text = "監聽";
label1.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);//接收文件
}
}
{
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);//接收文件
}
}
private void button1_Click(object sender, EventArgs e)//連接藍芽
{
Connect();
}
{
Connect();
}
private void button選擇文件_Click(object sender, EventArgs e)
{
OpenFileDialog opp=new OpenFileDialog();
if (opp.ShowDialog() == DialogResult.OK)
{
sendFileName = opp.FileName;
label發送文件名.Text = "文件:" + sendFileName;
}
}
{
OpenFileDialog opp=new OpenFileDialog();
if (opp.ShowDialog() == DialogResult.OK)
{
sendFileName = opp.FileName;
label發送文件名.Text = "文件:" + sendFileName;
}
}
private void button發送_Click(object sender, EventArgs e)
{
sendFile();
}
{
sendFile();
}
private void button選擇目錄_Click(object sender, EventArgs e)
{
FolderBrowserDialog opd = new FolderBrowserDialog();
opd.Description = "请选择蓝牙接收文件的存放路径";
if (opd.ShowDialog() == DialogResult.OK)
{
recDir = opd.SelectedPath;
label接收目錄.Text = recDir;
}
}
{
FolderBrowserDialog opd = new FolderBrowserDialog();
opd.Description = "请选择蓝牙接收文件的存放路径";
if (opd.ShowDialog() == DialogResult.OK)
{
recDir = opd.SelectedPath;
label接收目錄.Text = recDir;
}
}
}
}
之前玩藍芽器材的時候做練習的
痾這好像是度娘的資源哈哈
總之程式設計就是一種練習找資料的事情厄
沒有留言:
張貼留言