- 浏览: 985136 次
- 性别:
- 来自: 珠海
文章分类
最新评论
-
熊猫仙:
请问,这个程序可以和BLE蓝牙设备通讯吗?
C#编程连接蓝牙设备,文件收发 -
lsy234009:
“这个电脑蓝牙不可用!”原因多半是你没打开蓝牙
C#编程连接蓝牙设备,文件收发 -
wujie_cnhn:
这个加密压缩后, linux环境下可以解压缩吗
好用java开源zip压缩解压类库Zip4j -
asdf_2012:
兄弟简单明了。顶你
C#使用Event在窗体之间传递消息和参数 -
xkb281:
简单易懂 不像有的文章 介绍一大堆没整明白是什么地下 赞一个 ...
Android网络请求框架Retrofit使用笔记
现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用USB蓝牙方便的连接手机等蓝牙设备进行通信。
操作蓝牙要使用类库InTheHand.Net.Personal
首先在项目中引用该类库;
static void Main(string[] args) { BluetoothRadio bluetoothRadio = BluetoothRadio.PrimaryRadio; if (bluetoothRadio == null) { Console.WriteLine("没有找到本机蓝牙设备!"); } else { Console.WriteLine("ClassOfDevice: " + bluetoothRadio.ClassOfDevice); Console.WriteLine("HardwareStatus: " + bluetoothRadio.HardwareStatus); Console.WriteLine("HciRevision: " + bluetoothRadio.HciRevision); Console.WriteLine("HciVersion: " + bluetoothRadio.HciVersion); Console.WriteLine("LmpSubversion: " + bluetoothRadio.LmpSubversion); Console.WriteLine("LmpVersion: " + bluetoothRadio.LmpVersion); Console.WriteLine("LocalAddress: " + bluetoothRadio.LocalAddress); Console.WriteLine("Manufacturer: " + bluetoothRadio.Manufacturer); Console.WriteLine("Mode: " + bluetoothRadio.Mode); Console.WriteLine("Name: " + bluetoothRadio.Name); Console.WriteLine("Remote:" + bluetoothRadio.Remote); Console.WriteLine("SoftwareManufacturer: " + bluetoothRadio.SoftwareManufacturer); Console.WriteLine("StackFactory: " + bluetoothRadio.StackFactory); } Console.ReadKey(); }
如果PC插入了蓝牙适配器,便会显示蓝牙相关信息:
然后我们就要利用蓝牙收发文件了:
前提是蓝牙设备(如手机)已经和PC配对了
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 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()//发送文件方法 { ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName));//创建网络请求 WebResponse response = null; try { buttonSend.Enabled = false; request.ReadFile(sendFileName);//发送文件 labelInfo.Text = "开始发送!"; response = request.GetResponse();//获取回应 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)//选择接受目录 { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择蓝牙接收文件的存放路径"; if (dialog.ShowDialog() == DialogResult.OK) { recDir = dialog.SelectedPath; labelRecDir.Text = recDir; } } 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 Form1_FormClosed(object sender, FormClosedEventArgs e) { if (sendThread != null) { sendThread.Abort(); } if (listenThread != null) { listenThread.Abort(); } if (listener != null && listener.IsListening) { listener.Stop(); } } }
程序界面:
SelectBluetoothDeviceDialog是一个InTheHand.Net.Personal提供的窗体,用于选择蓝牙设备:
从手机往电脑发送文件需要在电脑上开启监听ObexListener,才能收到文件。
核心代码:
BluetoothRadio radio = null;//蓝牙适配器 string sendFileName = null;//发送文件名 BluetoothAddress sendAddress = null;//发送目的地址 ObexListener listener = null;//监听器 string recDir = null;//接受文件存放目录 Thread listenThread, sendThread;//发送/接收线程 radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器 //关于蓝牙设备选择对话框 SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); dialog.ShowRemembered = true;//显示已经记住的蓝牙设备 dialog.ShowAuthenticated = true;//显示认证过的蓝牙设备 dialog.ShowUnknown = true;//显示位置蓝牙设备 sendAddress = dialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙地址 //发送文件操作 ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName));//创建网络请求 WebResponse response = null; request.ReadFile(sendFileName);//发送文件 response = request.GetResponse();//获取回应 response.Close(); //接收文件 radio.Mode = RadioMode.Discoverable;//设置本地蓝牙可被检测 listener = new ObexListener(ObexTransport.Bluetooth);//创建监听 listener.Start(); listener.Stop(); ObexListenerContext context = null; ObexListenerRequest request = null; context = listener.GetContext();//获取监听上下文 request = context.Request;//获取请求 string uriString = Uri.UnescapeDataString(request.RawUrl);//将uri转换成字符串 string recFileName = recDir + uriString; request.WriteFile(recFileName);//接收文件 labelRecInfo.Text = "收到文件" + uriString.TrimStart(new char[] { '/' }
end
- InTheHand.Net.Personal.zip (789.9 KB)
- 下载次数: 913
- BluetoothSend.zip (379.5 KB)
- 下载次数: 800
评论
5 楼
熊猫仙
2018-08-10
请问,这个程序可以和BLE蓝牙设备通讯吗?
4 楼
lsy234009
2017-05-07
“这个电脑蓝牙不可用!”原因多半是你没打开蓝牙
3 楼
lixiang2202
2016-04-10
你好,我想请教一个问题,在使用InTheHand库开发蓝牙文件接收的时候,手机选择多个文件,通过蓝牙发送,程序只能收到一个文件,而手机端同样显示第一个文件成功,其他文件失败。请问对于多文件接收,您有什么这方面的经验?谢谢
2 楼
昂望星空
2015-11-01
q2108907 写道
为什么,我的电脑插上蓝牙执行后,直接提示 “这个电脑蓝牙不可用!”
我的也是这种情况,是用CSR蓝牙
1 楼
q2108907
2015-02-01
为什么,我的电脑插上蓝牙执行后,直接提示 “这个电脑蓝牙不可用!”
发表评论
-
C#窗体程序实现文件拖放
2013-05-27 14:11 1904C#实现文件拖放首先要设置窗体的可拖放为True: 然 ... -
C#利用zxing.net操作二维码和条形码
2013-05-26 22:06 54593下载地址:http://zxingne ... -
C#简单读写ini文件
2013-05-11 16:48 5584INI文件其实是一种文本文件,它的构成分为三部分 Sec ... -
C#解决跨线程调用错误的简单方法
2012-12-01 12:36 4856在窗体程序中我们常把费时操作另开新线程,但是我们要知道新线程的 ... -
C#调用WinRar来解压文件
2012-10-09 09:16 1978class UseWinRar { ... -
C#使用Event在窗体之间传递消息和参数
2012-10-06 23:16 5256有时候需要在两个窗体之间传递事件或者参数,例如一个登陆窗体,要 ... -
C#使用System.Data.SQLite操作SQLite
2012-10-06 17:54 91741使用System.Data.SQLite下载地址:http:/ ... -
C#写入和读取xml文档
2012-10-04 18:46 4106写入文档: static void Main(string ... -
C#使用MySQLDriverCS连接MySQL数据库
2012-10-04 14:40 3598MySQLDriverCSS下载地址:http://sourc ... -
C#使用MySQLConnectorNet连接MySQL数据库
2012-10-04 14:00 233941.首先下载安装Connector/Net,http://ww ... -
C#中串口的操作
2012-10-02 14:08 16161.获取可用的串口名 static void Main(st ... -
C#中使用UDP通信
2012-10-01 20:00 4357UDP通信是无连接通信,客户端在发送数据前无需与服务器端建立连 ... -
C#中使用TCP通信
2012-10-01 11:12 14096TCP通信需要通信双方都 ... -
C#添加和使用资源文件
2012-09-24 21:15 2825通过向项目中添加资源文件,资源文件便可以编译到可执行文件中; ... -
C#窗体程序退出提示的实现
2012-09-23 11:20 2036在正在退出事件中添加代码: private voi ... -
C#窗体程序中操作谷歌地图
2012-09-23 11:06 7738对谷歌地图操作使用的是WebBrowser控件,通过对java ... -
C#调用浏览器打开网页
2012-09-18 15:45 20026private void lbllink_LinkCli ... -
C#播放flash动画即swf文件
2012-07-31 22:31 16731.工具箱上点鼠标右键,出现菜单,选择 "选择项.& ... -
C#中使用IrisSkin进行美化
2012-07-31 22:26 3396首先要添加外部引用 IrisSkin2.dll 在代码中创建 ... -
C#简单创建xml文件
2011-11-09 22:22 2920using System; using System. ...
相关推荐
c#代码编写连接 PC 蓝牙连接到苹果手机的蓝牙,测试连接可行,网上很多代码,很混乱,不好连接!
C#利用开源控件InTheHand.Net.Personal.dll制作的完整配对,发送及接收文件,可以连接手机也可以连接电脑。
c#代码编写连接 PC 蓝牙连接到苹果手机的蓝牙,测试连接可行,网上很多代码,很混乱,不好连接!
winform 程序调用Windows.Devices.Bluetoot API 实现windows下BLE蓝牙设备自动连接,收发数据功能。不需要使用win10的UWP开发。 实际例子用vs2022编写,可直接编译运行
C#扫描蓝牙设备demo 直接运行就可使用 含dll及代码 绝对可用!!! C#扫描蓝牙设备demo 直接运行就可使用 含dll及代码 绝对可用!!!
C#编程 文件操作 FileCopyPlan(源码)(源码)C#编程 文件操作 FileCopyPlan(源码)(源码)C#编程 文件操作 FileCopyPlan(源码)(源码)C#编程 文件操作 FileCopyPlan(源码)(源码)C#编程 文件操作 FileCopyPlan(源码)(源码)...
C#编程 文件操作 GetShortPathName(源码)(源码)C#编程 文件操作 GetShortPathName(源码)(源码)C#编程 文件操作 GetShortPathName(源码)(源码)C#编程 文件操作 GetShortPathName(源码)(源码)C#编程 文件操作 ...
C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)(源码)C#编程 文件操作 AllowDropFile(源码)...
C#编程 文件操作 WordReplace(源码)(源码)C#编程 文件操作 WordReplace(源码)(源码)C#编程 文件操作 WordReplace(源码)(源码)C#编程 文件操作 WordReplace(源码)(源码)C#编程 文件操作 WordReplace(源码)(源码)C#...
C#编程 文件操作 CreateFile(源码)(源码)C#编程 文件操作 CreateFile(源码)(源码)C#编程 文件操作 CreateFile(源码)(源码)C#编程 文件操作 CreateFile(源码)(源码)C#编程 文件操作 CreateFile(源码)(源码)C#编程 ...
C#编程 文件操作 ReadFileByLine(源码)(源码)C#编程 文件操作 ReadFileByLine(源码)(源码)C#编程 文件操作 ReadFileByLine(源码)(源码)C#编程 文件操作 ReadFileByLine(源码)(源码)C#编程 文件操作 ReadFileByLine...
C#编程 文件操作 INIFileOperate(源码)(源码)C#编程 文件操作 INIFileOperate(源码)(源码)C#编程 文件操作 INIFileOperate(源码)(源码)C#编程 文件操作 INIFileOperate(源码)(源码)C#编程 文件操作 INIFileOperate...
C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt(源码)(源码)C#编程 文件操作 MultiFormatTxt...
C# 蓝牙 文件传输 完整 DEMO
C#编程如何从文件中读取文件内容C#编程如何从文件中读取文件内容C#编程如何从文件中读取文件内容C#编程如何从文件中读取文件内容C#编程如何从文件中读取文件内容
C#编程 文件操作 RansackFile(源码)(源码)C#编程 文件操作 RansackFile(源码)(源码)C#编程 文件操作 RansackFile(源码)(源码)C#编程 文件操作 RansackFile(源码)(源码)C#编程 文件操作 RansackFile(源码)(源码)C#...
C#编程 文件操作 ClearRecycle(源码)(源码)C#编程 文件操作 ClearRecycle(源码)(源码)C#编程 文件操作 ClearRecycle(源码)(源码)C#编程 文件操作 ClearRecycle(源码)(源码)C#编程 文件操作 ClearRecycle(源码)(源码)...
C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)(源码)C#编程 文件操作 DeleteDirByDG(源码)...
C#编程 文件操作 WordToHTML(源码)(源码)C#编程 文件操作 WordToHTML(源码)(源码)C#编程 文件操作 WordToHTML(源码)(源码)C#编程 文件操作 WordToHTML(源码)(源码)C#编程 文件操作 WordToHTML(源码)(源码)C#编程 ...