我用32Feet.Net来检测蓝牙设备,但它不起作用。https://imgur.com/a/PpGHMQX
然后我使用Windows系统蓝牙功能,它可以检测到那个设备。https://imgur.com/a/bjpodlu
这是我的密码,我的密码有问题吗.请帮助我解决这个问题,非常感谢!
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BluetoothSearchTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool RUN = false;
private void btnSearch_Click(object sender, EventArgs e)
{
if (!RUN)
{
RUN = true;
Thread t = new Thread(Search);
t.Start();
}
else
{
RUN = false;
}
}
private void Search()
{
BluetoothClient bc = new BluetoothClient();
BluetoothDeviceInfo[] bdi_list = null;
ButtonText("Stop");
AppendText("Start Searching" + "\r\n");
while (RUN)
{
bdi_list = bc.DiscoverDevicesInRange();
if (bdi_list.Length != 0)
{
foreach (BluetoothDeviceInfo bdi in bdi_list)
{
AppendText(bdi.DeviceName + "\r\n");
AppendText(bdi.DeviceAddress + "\r\n");
AppendText("-----------------" + "\r\n");
}
}
}
ButtonText("Search");
AppendText("Stop Searching" + "\r\n");
}
private void ButtonText(string text)
{
if (this.InvokeRequired == true)
{
this.BeginInvoke(new MethodInvoker(delegate { ButtonText(text); }));
return;
}
btnSearch.Text = text;
Application.DoEvents();
}
private void AppendText(string content)
{
if (this.InvokeRequired == true)
{
this.BeginInvoke(new MethodInvoker(delegate { AppendText(content); }));
return;
}
rtbContent.AppendText(content);
rtbContent.ScrollToCaret();
Application.DoEvents();
}
}
}
发布于 2022-01-21 08:56:25
谢谢Risto的暗示!这是一个BLE装置,我的答案如下,
开发环境: VS 2017
Windows应用程序:.NET框架4.6.1
步骤1.在安装某些软件包之前,我们需要设置包管理格式,
In Tools -> Options -> Nuget Package Manager -> General -> Package Management to PackageReference
步骤2.通过InTheHand.BluetoothLE包管理器控制台安装NuGet & UwpDesktop
PM> Install-Package InTheHand.BluetoothLE -Version 4.0.22
PM>安装- UwpDesktop -Version 10.0.14393.3包
步骤3.编码
using InTheHand.Bluetooth;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BluetoothSearchTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool RUN = false;
static string MSG = "";
private void btnSearch_Click(object sender, EventArgs e)
{
if (!RUN)
{
RUN = true;
Thread t = new Thread(Search);
t.Start();
}
else
{
RUN = false;
}
}
private void Search()
{
ButtonText("Stop");
AppendText("Start Searching" + "\r\n");
while (RUN)
{
var discoveryTask = TestDeviceDiscovery();
discoveryTask.Wait();
AppendText(MSG);
}
ButtonText("Search");
AppendText("Stop Searching" + "\r\n");
}
private void ButtonText(string text)
{
if (this.InvokeRequired == true)
{
this.BeginInvoke(new MethodInvoker(delegate { ButtonText(text); }));
return;
}
btnSearch.Text = text;
Application.DoEvents();
}
private void AppendText(string content)
{
if (this.InvokeRequired == true)
{
this.BeginInvoke(new MethodInvoker(delegate { AppendText(content); }));
return;
}
rtbContent.AppendText(content);
rtbContent.ScrollToCaret();
Application.DoEvents();
}
private static async Task<bool> TestDeviceDiscovery()
{
var discoveredDevices = await Bluetooth.ScanForDevicesAsync();
MSG = "";
foreach (BluetoothDevice bd in discoveredDevices)
{
MSG += bd.Name + "\r\n" + bd.Id + "\r\n" + "-----------------" + "\r\n";
}
return discoveredDevices?.Count > 0;
}
}
}
完了!这是结果!https://imgur.com/rkPSCam
希望这个案子能帮助那些有需要的人。
https://stackoverflow.com/questions/70780018
复制相似问题