LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#WinForm實現(xiàn)藍牙連接文件傳輸

admin
2025年2月26日 7:39 本文熱度 659

一、效果展示

二、VS2022
三、電腦開啟藍牙功能
手機接收到藍牙連接
手機接收到藍牙文件
四、核心代碼
引用
    using InTheHand.Net;using InTheHand.Net.Sockets;using InTheHand.Net.Bluetooth;
    建立連接
      private BluetoothClient btClient;private NetworkStream btStream;private BluetoothDeviceInfo selectedDevice;private readonly Guid serviceUUID = new Guid("00001132-0000-1000-8000-00805f9b34fb"); // 標準SPP服務(wù)
      // 掃描藍牙設(shè)備private void btnScan_Click(object sender, EventArgs e){ lstDevices.Items.Clear(); btClient = new BluetoothClient(); var devices = btClient.DiscoverDevices();
      foreach (var device in devices) { lstDevices.Items.Add($"{device.DeviceName} [{device.DeviceAddress}]"); }}
      // 設(shè)備選擇private void lstDevices_SelectedIndexChanged(object sender, EventArgs e){ var selected = btClient.DiscoverDevices()[lstDevices.SelectedIndex]; selectedDevice = selected; richTextBox1.AppendText($"已選擇:{selected.DeviceName}\n");}
      // 建立連接private void btnConnect_Click(object sender, EventArgs e){ try { btClient.Connect(selectedDevice.DeviceAddress, serviceUUID); btStream = btClient.GetStream(); StartListening(); richTextBox1.AppendText("連接成功\n"); } catch (Exception ex) { MessageBox.Show($"連接失敗:{ex.Message}"); }}
      接收數(shù)據(jù)
        // 異步數(shù)據(jù)接收private void StartListening(){	new Thread(() =>	{		byte[] buffer = new byte[1024];		while (btClient?.Connected == true)		{			try			{				int bytesRead = btStream.Read(buffer, 0, buffer.Length);				if (bytesRead > 0)				{					string received = Encoding.UTF8.GetString(buffer, 0, bytesRead);					Invoke((MethodInvoker)delegate { richTextBox1.AppendText($"[接收] {received}\n"); });				}			}			catch { break; }		}	}).Start();}
        發(fā)送數(shù)據(jù)
          // 發(fā)送數(shù)據(jù)private void btnSend_Click(object sender, EventArgs e){	if (btClient?.Connected != truereturn;	// 讀取文件并發(fā)送	byte[] buffer = new byte[1024]; // 緩沖區(qū)大小可以根據(jù)需要調(diào)整	using (FileStream fs = new FileStream(txtSend.Text, FileMode.Open, FileAccess.Read))	{		int bytesRead;		while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)		{			btClient.GetStream().Write(buffer, 0, bytesRead);		}	}	richTextBox1.AppendText($"[發(fā)送] {txtSend.Text}\n");	txtSend.Clear();}
          藍牙Guid


          閱讀原文:原文鏈接


          該文章在 2025/2/26 18:30:29 編輯過
          關(guān)鍵字查詢
          相關(guān)文章
          正在查詢...
          點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
          點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
          點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
          點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
          Copyright 2010-2025 ClickSun All Rights Reserved

          黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
          精品成年人在线观看 | 亚洲中日韩欧美高清在线 | 日韩一区二区在线播放 | 亚洲一区二区三区四区精品视频在线观看 | 中文字幕免费的日本精品视频 | 羞羞视频在线观看网页 |