weixin_48620148 2021-12-24 09:58 采纳率: 0%
浏览 42

c#蓝牙项目 无法获取电脑蓝牙地址咋办

搜了很多贴都需要这行代码
BluetoothRadio radio = BluetoothRadio.PrimaryRadio;

img

程序抄来的


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System.Threading;
using InTheHand.Net;

public class bule : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    BluetoothListener bluetoothListener;//蓝牙监听
    Thread listenThread;//蓝牙监听线程
    BluetoothClient bluetoothClient = new BluetoothClient();//创建新的蓝牙客户端
    bool isConnected;//是否连接标识

    /// <summary>
    /// 打开端口
    /// </summary>
    /// <returns></returns>
    public bool OpenPort()
    {
  
        //InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable;//不包含PrimaryRadui
        InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient();//创建新的蓝牙客户端连接
        Dictionary<string, BluetoothAddress> deviceAddresses = new Dictionary<string, BluetoothAddress>();//用一个键值对保存收索到的蓝牙地址
        BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
        IReadOnlyCollection<BluetoothDeviceInfo> devices = cli.DiscoverDevices();//获得发现蓝牙的地址和名字
        foreach (InTheHand.Net.Sockets.BluetoothClient. device in devices) //设备搜寻           
        {
            device.Update();
            device.Refresh();
            if (device.DeviceName == "BF10-A")
            {
                this.textBox2.Text = "设备已找到";
                break;
            }
        }

        Thread ReceiveThread = new Thread(ReceiveData);
        ReceiveThread.Start();
        return true;
    }

    public void ReceiveData()
    {
        #region

        try
        {
            Guid mGUID = Guid.Parse("00001101-0000-1000-8000-00805f9b34fb");
            bluetoothListener = new BluetoothListener(mGUID);
            bluetoothListener.Start();
            bluetoothClient = bluetoothListener.AcceptBluetoothClient();
            isConnected = true;
        }
        catch (Exception ex)
        {
            isConnected = false;
            MessageBox.Show(ex.ToString());
        }
        while (isConnected)
        {
            string receive = string.Empty;
            if (bluetoothClient == null)
            {
                break;
            }
            try
            {
                Stream peerStream = bluetoothClient.GetStream();
                byte[] buffer = new byte[6];
                peerStream.Read(buffer, 0, 6);
                receive = Encoding.UTF8.GetString(buffer).ToString();
                textBox1.Text = receive;
            }
            catch (System.Exception)
            {
            }
            Thread.Sleep(100);
        }
        #endregion
    }
}

  • 写回答

1条回答 默认 最新

  • 於黾 2021-12-24 10:05
    关注

    你是using InTheHand.Net.Bluetooth;这样引用的吗,还是自己写了个class名叫BluetoothRadio

    评论

报告相同问题?

问题事件

  • 修改了问题 12月24日
  • 创建了问题 12月24日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表