using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;using InTheHand.Windows.Forms;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;using System.Threading;
using System.Runtime.InteropServices;using System.Net;
using System.IO;namespace bluetooth
{
public partial class Form1 : Form
{
BluetoothRadio radio = null;//蓝牙适配器
string sendFileName = null;//发送文件名
BluetoothAddress sendAddress = null;//发送目的地址
ObexListener listener = null;//监听器
string recDir = null;//接受文件存放目录 BluetoothClient bluetoothClient;
BluetoothListener bluetoothListener;
Thread listenThread;
bool isConnected;
NetworkStream peerStream; public Form1()
{
InitializeComponent();
radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器
CheckForIllegalCrossThreadCalls = false;//不检查跨线程调用
if (radio == null)//检查该电脑蓝牙是否可用
{
MessageBox.Show("这个电脑蓝牙不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// labelRecDir1.Text = recDir;
} private void button1_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;//获取选择的远程蓝牙地址
label1.Text = "地址:" + sendAddress.ToString() + "设备名:" + dialog.SelectedDevice.DeviceName;
listenThread = new Thread(ReceiveData);
listenThread.Start();
}
}
private void ReceiveData()
{
try
{
// textBox1.Text = sendAddress.ToString();
Guid mGUID = Guid.Parse("e0cbf06c-cd8b-4647-bb8a-263b43f0f974"); // radio.LocalAddress sendAddress.ToString()
bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();
label3.Text = bluetoothListener.ServiceName.ToString();
bluetoothClient = bluetoothListener.AcceptBluetoothClient();
label2.Text = "3";
isConnected = true;
}
catch (Exception ex)
{
label2.Text = ex.Message.ToString();
isConnected = false;
}
while (isConnected)
{
string receive = string.Empty;
if (bluetoothClient == null)
{
break;
}
try
{
peerStream = bluetoothClient.GetStream();
byte[] buffer = new byte[6];
peerStream.Read(buffer, 0, 6);
receive = Encoding.UTF8.GetString(buffer).ToString();
}
catch (System.Exception)
{
}
Thread.Sleep(100);
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (listenThread != null)
{
listenThread.Abort();
}
if (listener != null && listener.IsListening)
{
listener.Stop();
}
}
}
}
蓝牙
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;using InTheHand.Windows.Forms;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;using System.Threading;
using System.Runtime.InteropServices;using System.Net;
using System.IO;namespace bluetooth
{
public partial class Form1 : Form
{
BluetoothRadio radio = null;//蓝牙适配器
string sendFileName = null;//发送文件名
BluetoothAddress sendAddress = null;//发送目的地址
ObexListener listener = null;//监听器
string recDir = null;//接受文件存放目录 BluetoothClient bluetoothClient;
BluetoothListener bluetoothListener;
Thread listenThread;
bool isConnected;
NetworkStream peerStream; public Form1()
{
InitializeComponent();
radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器
CheckForIllegalCrossThreadCalls = false;//不检查跨线程调用
if (radio == null)//检查该电脑蓝牙是否可用
{
MessageBox.Show("这个电脑蓝牙不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// labelRecDir1.Text = recDir;
} private void button1_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;//获取选择的远程蓝牙地址
label1.Text = "地址:" + sendAddress.ToString() + "设备名:" + dialog.SelectedDevice.DeviceName;
listenThread = new Thread(ReceiveData);
listenThread.Start();
}
}
private void ReceiveData()
{
try
{
// textBox1.Text = sendAddress.ToString();
Guid mGUID = Guid.Parse("e0cbf06c-cd8b-4647-bb8a-263b43f0f974"); // radio.LocalAddress sendAddress.ToString()
bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();
label3.Text = bluetoothListener.ServiceName.ToString();
bluetoothClient = bluetoothListener.AcceptBluetoothClient();
label2.Text = "3";
isConnected = true;
}
catch (Exception ex)
{
label2.Text = ex.Message.ToString();
isConnected = false;
}
while (isConnected)
{
string receive = string.Empty;
if (bluetoothClient == null)
{
break;
}
try
{
peerStream = bluetoothClient.GetStream();
byte[] buffer = new byte[6];
peerStream.Read(buffer, 0, 6);
receive = Encoding.UTF8.GetString(buffer).ToString();
}
catch (System.Exception)
{
}
Thread.Sleep(100);
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (listenThread != null)
{
listenThread.Abort();
}
if (listener != null && listener.IsListening)
{
listener.Stop();
}
}
}
}
蓝牙
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;using InTheHand.Windows.Forms;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;using System.Threading;
using System.Runtime.InteropServices;using System.Net;
using System.IO;namespace bluetooth
{
public partial class Form1 : Form
{
BluetoothRadio radio = null;//蓝牙适配器
string sendFileName = null;//发送文件名
BluetoothAddress sendAddress = null;//发送目的地址
ObexListener listener = null;//监听器
string recDir = null;//接受文件存放目录 BluetoothClient bluetoothClient;
BluetoothListener bluetoothListener;
Thread listenThread;
bool isConnected;
NetworkStream peerStream; public Form1()
{
InitializeComponent();
radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器
CheckForIllegalCrossThreadCalls = false;//不检查跨线程调用
if (radio == null)//检查该电脑蓝牙是否可用
{
MessageBox.Show("这个电脑蓝牙不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// labelRecDir1.Text = recDir;
} private void button1_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;//获取选择的远程蓝牙地址
label1.Text = "地址:" + sendAddress.ToString() + "设备名:" + dialog.SelectedDevice.DeviceName;
listenThread = new Thread(ReceiveData);
listenThread.Start();
}
}
private void ReceiveData()
{
try
{
// textBox1.Text = sendAddress.ToString();
Guid mGUID = Guid.Parse("e0cbf06c-cd8b-4647-bb8a-263b43f0f974"); // radio.LocalAddress sendAddress.ToString()
bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();
//在这里程序就不动了
label3.Text = bluetoothListener.ServiceName.ToString();
bluetoothClient = bluetoothListener.AcceptBluetoothClient();
label2.Text = "3";
isConnected = true;
}
catch (Exception ex)
{
label2.Text = ex.Message.ToString();
isConnected = false;
}
while (isConnected)
{
string receive = string.Empty;
if (bluetoothClient == null)
{
break;
}
try
{
peerStream = bluetoothClient.GetStream();
byte[] buffer = new byte[6];
peerStream.Read(buffer, 0, 6);
receive = Encoding.UTF8.GetString(buffer).ToString();
}
catch (System.Exception)
{
}
Thread.Sleep(100);
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (listenThread != null)
{
listenThread.Abort();
}
if (listener != null && listener.IsListening)
{
listener.Stop();
}
}
}
}