先下载InTheHand.Net.Personal.dll并在C中引用

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.Threading;
using System.Windows.Forms;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using InTheHand.Windows.Forms;
using System.Net;
using System.IO;
using System.IO.Ports;namespace WindowsFormsApp1
{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);textBox1.Text = recDir;}BluetoothClient Blueclient = new BluetoothClient();Dictionary<string, BluetoothAddress> deviceAddresses = new Dictionary<string, BluetoothAddress>();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 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 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 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 textBox1_TextChanged(object sender, EventArgs e){}private void labelRecDir_Click(object sender, EventArgs e){}SerialPort BluetoothConnection = new SerialPort();int length = 20;string BlueToothReceivedData = "";private void button3_Click(object sender, EventArgs e){string[] Ports = SerialPort.GetPortNames();for(int i = 0; i < Ports.Length; i++){string name = Ports[i];comboBox.Items.Add(name);//显示在消息框里面}}private void send_Click(object sender, EventArgs e){BluetoothClient cli = new BluetoothClient();BluetoothEndPoint ep = null;try{// [注意2]:要注意MAC地址中字节的对应关系,直接来看顺序是相反的,例如// 如下对应的MAC地址为——12:34:56:78:9a:bcep = new BluetoothEndPoint(sendAddress, BluetoothService.SerialPort);cli.Connect(ep); // 连接蓝牙if (cli.Connected){MessageBox.Show("端口1已打开");Stream peerStream = cli.GetStream();peerStream.WriteByte(0xBB); // 发送开门指令}else {MessageBox.Show("端口1没打开");}}catch {Console.WriteLine("123123");Console.ReadLine();}finally{if (cli != null){// [注意3]:要延迟一定时间(例如1000毫秒)//避免因连接后又迅速断开而导致蓝牙进入异常(傻逼)状态Thread.Sleep(1000);cli.Close();}}///byte[] head = new byte[8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };//随便写的一组数据,里面的数据无意try{BluetoothConnection.PortName = comboBox.Text;BluetoothConnection.BaudRate = 9600;BluetoothConnection.Open();//打开蓝牙串口if (BluetoothConnection.IsOpen) { BlueToothReceivedData = " d1_on";MessageBox.Show("端口已打开");BlueToothReceivedData = " d1_on";//BluetoothConnection.Write(head, 0, head.Length);BluetoothConnection.Write("12323");Thread.Sleep(1000);MessageBox.Show("完成");}//BluetoothConnection.WriteLine(BlueToothReceivedData);//byte[] head = new byte[8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };//随便写的一组数据,里面的数据无意义BluetoothConnection.Write(head, 0, head.Length);}catch{label1.Text = "发送失败"; ;}}private void get_Click(object sender, EventArgs e){byte[] data = new byte[length];try{BluetoothConnection.Read(data, 0, length);}catch{label1.Text = " 接受失败"; }for (int i = 0; i < length; i++){BlueToothReceivedData += string.Format("data[{0}] = {1}\r\n", i, data[i]);//"+="表示接收数据事件发生时,触发"+="后面的语句}}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();}}}
}

C#蓝牙链接+传输文件相关推荐

  1. win10不能上传文件到服务器,Win10系统下蓝牙无法传输文件怎么解决

    在win10系统中,自带有蓝牙功能,可以方便用户们进行数据传输等,但是有些用户却反映说遇到蓝牙无法传输文件的情况,该怎么解决这样的情况呢?本文就给大家讲解一下Win10系统下蓝牙无法传输文件的具体解决 ...

  2. Android 11.0 蓝牙去掉传输文件的功能

    1.概述 在11.0的系统产品开发中,对于蓝牙的管控也是常有的功能,比如禁止连接蓝牙,禁止蓝牙传输文件等功能,最近有产品功能需求,要求禁止蓝牙传输文件,这就要从蓝牙文件传输流程分析,然后禁用传输功能就 ...

  3. Android 10.0 蓝牙去掉传输文件的功能

    1.概述 在10.0的系统产品开发中,对于蓝牙的管控功能也是特别多的,在针对蓝牙的定制化功能中,由于产品需要 要求在蓝牙文件传输过程中,进行限制就是不让蓝牙传输文件,所以要求在开始传输的时候 ,屏蔽掉 ...

  4. android蓝牙传文件,安卓手机怎样使用蓝牙连接传输文件

    首先要打开手机的蓝牙功能,平常大家不用蓝牙的时候一般处于关闭状态,需要的时候才开启,这样比较省电不是~~ 步骤一:打开蓝牙 打开蓝牙于两种方法,一种是通过快捷方式,比如桌面的蓝牙开关等以及状态栏的下拉 ...

  5. Android 9.0 蓝牙去掉传输文件的功能

    1.概述 在9.0的系统rom定制化产品开发中,在原生系统中蓝牙这块的功能也是非常重要的,所以在对蓝牙功能开发过程中,对功能的定制要求也多,在蓝牙的开发需求中,功能要求 也是越来越多的,产品需要要求在 ...

  6. Windows Mobile 与 PC之间的通过蓝牙(Bluetooth) 传输文件的开发

    背景 之前也写过一些Windows Mobile和Wince下Bluetooth开发的文章如下. .NET Compact Framework下的Bluetooth开发 之 Windows Embed ...

  7. 计算机如何用蓝牙实现文件传输,Win10系统电脑通过蓝牙进行传输文件操作步骤...

    Win10系统如何使用蓝牙传输文件?相信用过win10系统的用户都知道,win10系统当中的蓝牙功能可以用来连接蓝牙耳机,蓝夜鼠标等设备.但其实,win10系统当中的蓝牙也可以用来传输文件,只需要用蓝 ...

  8. win10如何修复蓝牙服务器,修复win10系统下蓝牙无法正常传输文件的解决方法

    一般win10系统都内置蓝牙功能,它可以连接各种无线设备,或者传输文件之类的功能.最近有一些小伙伴使用win10操作系统的时候遇到了一个问题,就是偶尔无法利用蓝牙正常传输文件,这让用户感到很头疼.于是 ...

  9. 蓝牙OPP协议文件传输格式修改

    android蓝牙在传输文件时对文件格式有限制,默认情况下,并不是所有文件都能够传输和共享. 在packages/apps/Bluetooth下面的AndroidManifest.xml中       ...

最新文章

  1. Linux下三个密码生成工具
  2. 我们需要什么样的数据架构?
  3. python3.6小程序_python3.6小程序
  4. c++ map iterator 获取key_前K个高频的元素衍生之Map的Value与Key排序
  5. 伪元素控制网页表单样式
  6. 迄今为止用到的Eclipse快捷键,最常用的--[欢迎补充]
  7. LeetCode 13. Roman to Integer
  8. [2020-ECCV]PIPAL-a Large-Scale Image Quality Assessment Dataset for Perceptual Image Restoration论文简析
  9. hashmap hash冲突怎么解决_HashMap原理及冲突之简谈
  10. 终于有人把Elasticsearch原理讲明白了!
  11. springboot tomcat启动
  12. 从下列选项中选择正确的java表达式,java笔试常见的选择题
  13. 第 4 周 关于 startActivityForResult
  14. 控制台打印vue实例
  15. 学生计算机重启删除文件,Delete.On.Reboot(重启时删除无用文件工具)
  16. VMware虚拟机磁盘扩容
  17. ubuntu如何打拼音
  18. 计算机网络:第四章 网络层
  19. 洛谷P5385 [Cnoi2019]须臾幻境
  20. win10设置窗口背景

热门文章

  1. easypermission坑_Android 权限管理(原生、EasyPermissions、RxPermissions)-阿里云开发者社区...
  2. 现代企业管理笔记——管理理论新进展
  3. Eclipse软件使用教程
  4. 项目:基于yolov5的舰船检测+pycharm+机器学习+图像检测
  5. 2023年4月Kali MSF命令模块最详细模块与利用解释整合
  6. 【转】Kbps、KB、Mbps单位换算
  7. 汇编程序语言 输入数字n和n个数字并对n个数字排序(带注释)
  8. jupyter notebook多行注释方法
  9. laravel8 改变分页样式
  10. 有利可图网_如何发布自己的流行且有利可图的开源书