首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# 32Feet.Net无法检测蓝牙设备

C# 32Feet.Net无法检测蓝牙设备
EN

Stack Overflow用户
提问于 2022-01-20 02:06:45
回答 1查看 659关注 0票数 1

我用32Feet.Net来检测蓝牙设备,但它不起作用。https://imgur.com/a/PpGHMQX

然后我使用Windows系统蓝牙功能,它可以检测到那个设备。https://imgur.com/a/bjpodlu

这是我的密码,我的密码有问题吗.请帮助我解决这个问题,非常感谢!

代码语言:javascript
运行
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 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.编码

代码语言:javascript
运行
复制
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

希望这个案子能帮助那些有需要的人。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70780018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档