public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
BluetoothClient bc =new BluetoothClient();
BluetoothDeviceInfo[] arr = new BluetoothDeviceInfo[10];
//第一參數表明搜索的最大設備數
//第二參數表示是否搜尋已配對設備
//第三參數表示是否搜尋濟助的設備
//第四個表示是否搜尋未知設備
//第五個表示搜索範圍內可被發現的設備
arr =bc.DiscoverDevices(255, true, true, true,true);
bc.Close();
}
BluetoothRadio radio = null;
string sendFileName = null;
BluetoothAddress sendAddress = null;
ObexListener listener = null;
string recDir = null;
Thread listenThread, sendThread;
private void button6_Click(object sender, EventArgs e)
{
radio = BluetoothRadio.PrimaryRadio;//獲取當前PC的藍牙適配器
CheckForIllegalCrossThreadCalls = false;//不檢查跨執行緒
if(radio==null)
{
MessageBox.Show("Bluetooth can't use !", "RROR");
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
richTextBox1.Text = recDir;
}
private void button7_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;
richTextBox1.Text = "Address:" + sendAddress.ToString() + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Device Name:" + dialog.SelectedDevice.DeviceName+"\r\n";
}
}
private void button8_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if(dialog.ShowDialog()==DialogResult.OK)
{
sendFileName = dialog.FileName;
richTextBox1.Text = Path.GetFileName(sendFileName);
sendThread = new Thread(sendFile);
sendThread.Start();
}
}
private void sendFile()
{
ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName)); //創建網路需求
WebResponse response = null;
try
{
button8.Enabled = false;
richTextBox1.Text = "";
request.ReadFile(sendFileName);
richTextBox1.Text = "start transfer"+"\r\n";
response = request.GetResponse();
richTextBox1.Text = richTextBox1.Text + "transfer End";
}
catch (System.Exception ex)
{
MessageBox.Show("transfer Failed");
richTextBox1.Text = richTextBox1.Text + "transfer failed";
}
finally
{
if(response!=null)
{
response.Close();
button8.Enabled = true;
}
}
}
private void button9_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "選擇藍芽接收文件的存放路徑";
if(dialog.ShowDialog()==DialogResult.OK)
{
recDir = dialog.SelectedPath;
richTextBox1.Text = dialog.SelectedPath;
richTextBox1.Text = recDir;
if((listener==null) ||(!listener.IsListening))
{
radio.Mode = RadioMode.Discoverable;//設置本地藍芽可被檢測
listener = new ObexListener(ObexTransport.Bluetooth);
listener.Start();
if(listener.IsListening)
{
richTextBox1.Text = "Start listener";
listenThread = new Thread(receiveFile);
listenThread.Start();
}
}
else
{
listener.Stop();
richTextBox1.Text = "Stop Listener";
}
}
}
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);
richTextBox1.Text = "收到文件" + uriString.TrimStart(new char[] { '/' });
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(sendThread!=null)
{
sendThread.Abort();
}
if (listener != null && listener.IsListening)
{
listener.Stop();
}
if (listenThread!=null)
{
listenThread.Abort();
}
}
private void button5_Click(object sender, EventArgs e)
{
BluetoothRadio bluetoothRadio = BluetoothRadio.PrimaryRadio;
if(bluetoothRadio==null)
{
MessageBox.Show("沒找到本機藍芽裝置");
}
else
{
richTextBox1.Text = "Class of Device:" + bluetoothRadio.ClassOfDevice + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Hardware Status:" + bluetoothRadio.HardwareStatus + "\r\n";
richTextBox1.Text = richTextBox1.Text + "HCiRevision:" + bluetoothRadio.HciRevision+"\r\n";
richTextBox1.Text = richTextBox1.Text + "HCiVersion:" + bluetoothRadio.HciVersion+"\r\n";
richTextBox1.Text = richTextBox1.Text + "LmpSubversion:" + bluetoothRadio.LmpSubversion + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Local address:" + bluetoothRadio.LocalAddress + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Manufacturer:" + bluetoothRadio.Manufacturer + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Mode:" + bluetoothRadio.Mode+"\r\n";
richTextBox1.Text = richTextBox1.Text + "Name:" + bluetoothRadio.Name + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Remote:" + bluetoothRadio.Name + "\r\n";
richTextBox1.Text = richTextBox1.Text + "SoftwareManfacturer:" + bluetoothRadio.SoftwareManufacturer + "\r\n";
richTextBox1.Text = richTextBox1.Text + "StackFactory" + bluetoothRadio.StackFactory + "\r\n";
}
}
}
兩台PC使用rfcomm通訊
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System.IO;
namespace bluetooth_rfcomm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
BluetoothDeviceInfo device = null;
richTextBox1.Text = "";
foreach(BluetoothDeviceInfo d in devices)
{
richTextBox1.Text = richTextBox1.Text + " " + d.DeviceName + "\n";
if (d.DeviceName=="NB-13")
{
device = d;
break;
}
}
if(device!=null)
{
richTextBox1.Text = richTextBox1.Text + "connect start\n";
client.Connect(device.DeviceAddress, BluetoothService.SerialPort);
Stream perStream = client.GetStream();
byte[] buffer = new byte[200];
perStream.Read(buffer, 0, 40);
string data = System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, 40);
richTextBox1.Text = richTextBox1.Text + "DATA:"+data+"\n";
perStream.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text ="\n";
BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
if (myRadio == null)
{
richTextBox1.Text = "No radio HW or unsupported SW stack";
return;
}
richTextBox1.Text = richTextBox1.Text+"Radio,address:" + myRadio.LocalAddress.ToString() + "\n";
richTextBox1.Text = richTextBox1.Text + "Mode:" + myRadio.Mode.ToString() + "\n";
richTextBox1.Text = richTextBox1.Text + "Name: " + myRadio.Name + " LmpSubversion:" + myRadio.LmpSubversion + "\n";
richTextBox1.Text = richTextBox1.Text + "ClassofDevice:" + myRadio.ClassOfDevice.ToString() + " device:" + myRadio.ClassOfDevice.Device.ToString() + " Service:" + myRadio.ClassOfDevice.Service.ToString() + "\n";
myRadio.Mode = RadioMode.Discoverable;
richTextBox1.Text = richTextBox1.Text + "Radio Mode now:" + myRadio.Mode.ToString();
richTextBox1.Text = richTextBox1.Text + "\n";
Application.DoEvents();
BluetoothListener listener = new BluetoothListener(BluetoothService.SerialPort);
listener.Start();
richTextBox1.Text = richTextBox1.Text + "service startd:\n";
BluetoothClient client = listener.AcceptBluetoothClient();
richTextBox1.Text = richTextBox1 + "Got a request\n";
Stream peerStream=client.GetStream();
string dataToSend = "Hello from service";
byte[] dataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(dataToSend);
peerStream.Write(dataBuffer, 0, dataBuffer.Length);
}
}
}
不錯的文章:
How to find Bluetooth MAC Address in Windows
https://macaddresschanger.com/how-to-find-bluetooth-mac-address-windows
連接方式介紹
http://32feet.codeplex.com/documentation
32feet.net 蓝牙虚拟串口编程http://32feet.codeplex.com/documentation
https://tisyang.github.io/2016/05/bluetooth-serialport-with-32feet-net-library/
Windows Moible, Wince 使用.NET Compact Framework进行蓝牙(Bluetooth)开发 之 32feet.NET
http://www.cnblogs.com/procoder/archive/2009/05/14/1456243.html
C#编程连接蓝牙设备,文件收发
http://hzy3774.iteye.com/blog/1735163
C#编程连接蓝牙设备,文件收发
http://hzy3774.iteye.com/blog/1735163
沒有留言:
張貼留言