• 大小: 478KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-02
  • 语言: C#
  • 标签: C#  蓝牙  

资源简介

C# 蓝牙 文件传输 完整 DEMO

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Net;

using InTheHand.Net.Bluetooth;
using InTheHand.Net;
using InTheHand.Windows.Forms;

namespace myBlueCS
{
    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(“发送失败!“ “提示“ MessageBoxBut

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     515584  2017-06-22 18:19  myBlueCS\myBlueCS\bin\Debug\InTheHand.Net.Personal.dll

     文件    1351168  2017-06-22 18:19  myBlueCS\myBlueCS\bin\Debug\InTheHand.Net.Personal.pdb

     文件      13824  2017-06-23 08:48  myBlueCS\myBlueCS\bin\Debug\myBlueCS.exe

     文件      26112  2017-06-23 08:48  myBlueCS\myBlueCS\bin\Debug\myBlueCS.pdb

     文件      21464  2017-06-23 08:39  myBlueCS\myBlueCS\bin\Debug\myBlueCS.vshost.exe

     文件        490  2012-06-06 02:06  myBlueCS\myBlueCS\bin\Debug\myBlueCS.vshost.exe.manifest

     文件       6242  2017-06-23 08:48  myBlueCS\myBlueCS\Form1.cs

     文件       9533  2017-06-23 08:48  myBlueCS\myBlueCS\Form1.Designer.cs

     文件       5817  2017-06-23 08:48  myBlueCS\myBlueCS\Form1.resx

     文件       3812  2017-06-22 18:39  myBlueCS\myBlueCS\myBlueCS.csproj

     文件       1229  2017-06-22 18:20  myBlueCS\myBlueCS\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6137  2017-06-22 18:21  myBlueCS\myBlueCS\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        705  2017-06-23 08:48  myBlueCS\myBlueCS\obj\Debug\myBlueCS.csproj.FileListAbsolute.txt

     文件        847  2017-06-23 08:48  myBlueCS\myBlueCS\obj\Debug\myBlueCS.csproj.GenerateResource.Cache

     文件      13824  2017-06-23 08:48  myBlueCS\myBlueCS\obj\Debug\myBlueCS.exe

     文件        180  2017-06-23 08:48  myBlueCS\myBlueCS\obj\Debug\myBlueCS.Form1.resources

     文件      26112  2017-06-23 08:48  myBlueCS\myBlueCS\obj\Debug\myBlueCS.pdb

     文件        180  2017-06-23 08:48  myBlueCS\myBlueCS\obj\Debug\myBlueCS.Properties.Resources.resources

     文件        469  2017-06-22 17:54  myBlueCS\myBlueCS\Program.cs

     文件       1354  2017-06-22 17:54  myBlueCS\myBlueCS\Properties\AssemblyInfo.cs

     文件       2868  2017-06-22 17:54  myBlueCS\myBlueCS\Properties\Resources.Designer.cs

     文件       5612  2017-06-22 17:54  myBlueCS\myBlueCS\Properties\Resources.resx

     文件       1095  2017-06-22 17:54  myBlueCS\myBlueCS\Properties\Settings.Designer.cs

     文件        249  2017-06-22 17:54  myBlueCS\myBlueCS\Properties\Settings.settings

     文件        914  2017-06-22 17:54  myBlueCS\myBlueCS.sln

    ..A..H.     38400  2017-06-23 08:48  myBlueCS\myBlueCS.v11.suo

     目录          0  2017-06-22 17:54  myBlueCS\myBlueCS\obj\Debug\TempPE

     目录          0  2017-06-23 08:48  myBlueCS\myBlueCS\bin\Debug

     目录          0  2017-06-23 08:48  myBlueCS\myBlueCS\obj\Debug

     目录          0  2017-06-22 17:55  myBlueCS\myBlueCS\bin

............此处省略7个文件信息

评论

共有 条评论