首页 > 解决方案 > 未找到支持的蓝牙协议栈

问题描述

我在VS2017中使用windows 7和C#,.net4.6.1。我使用蓝牙 CSR 4.0 加密狗和 csr hormony 蓝牙堆栈作为第三方蓝牙驱动程序。

我跟着这个写了一个hcitool程序: 想在Windows中写蓝牙'hcitool'等价物

但是当我尝试运行程序来连接 BLE 设备时。此行发生错误
var dev = new BluetoothDeviceInfo(_searchAddress); 错误是:找不到支持的蓝牙协议栈。

我已经仔细检查了,我传递的参数是正确的(信息+设备地址)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using InTheHand.Net.Bluetooth;
using InTheHand.Net;
using InTheHand.Net.Sockets;
using System.Diagnostics;
using System.Net.Sockets;

namespace hcitool
{
partial class Program
{
    static bool infoRatherThanName;
    static BluetoothAddress _searchAddress;

    static int Main(string[] args)
    {
        if (args.Length < 1) {
            Console.WriteLine("Please specify command.");
            return 2;
        }
        var cmd = args[0];
        switch (cmd) {
            case "name":
                infoRatherThanName = false;
                break;
            case "info":
                infoRatherThanName = true;
                break;
            //-
            case "dev":
                return ShowRadios();
            //case "auth":
            //    return CauseAuth(GETADDRESS());
            default:
                throw new NotImplementedException("Command: '" + cmd + "'");
        }
        if (args.Length < 2) {
            Console.WriteLine("Please specify device address.");
            return 2;
        }
        var addrS = args[1];
        _searchAddress = BluetoothAddress.Parse(addrS);
        //
        var dev = new BluetoothDeviceInfo(_searchAddress);
        bool isInRange = GetCanConnectTo(dev);
        if (isInRange) {
            PrintDevice(dev);
        } else {
            Console.WriteLine("Can't see that device.");
        }
        //
        Console.WriteLine("simple");
        return Simple();
        //return Fancier();
    }

    //----
    private static int ShowRadios()
    {
        BluetoothRadio[] list;
        try {
            list = BluetoothRadio.AllRadios;
        } catch (Exception) {
            return 1;
        }
        Debug.Assert(list.Length != 0, "Expect zero radios case to raise an error.");
        foreach (var curR in list) {
            Console.WriteLine("* {0} '{1}'", curR.LocalAddress, curR.Name);
            Console.WriteLine("{0}", curR.SoftwareManufacturer);
            Console.WriteLine("{0}", curR.Manufacturer);
            Console.WriteLine("{0}", curR.Mode);
        }//for
        return 0;
    }

    private static int CauseAuth(BluetoothAddress addr)
    {
        BluetoothSecurity.PairRequest(addr, null);
        return 0;
    }

    //----
    static int Simple()
    {
        BluetoothDeviceInfo[] devices;
        BluetoothDeviceInfo foundDev = null;
        var cli = new BluetoothClient();
        // Fast: Remembered/Authenticated
        devices = cli.DiscoverDevices(255, true, true, false, false);
        SimpleCheckDevice(devices, ref foundDev);
        if (foundDev == null) {
            // Slow: Inquiry
            cli.DiscoverDevices(255, false, false, true, false);
            SimpleCheckDevice(devices, ref foundDev);
        }
        //
        if (foundDev != null) {
            return 0;
        } else {
            return 1;
        }
    }

    private static void SimpleCheckDevice(IEnumerable<BluetoothDeviceInfo> devices,
        ref BluetoothDeviceInfo foundDev)
    {
        foreach (var cur in devices) {
            if (cur.DeviceAddress == _searchAddress) {
                foundDev = cur;
                PrintDevice(cur);
            }
        }//for
    }

    private static void PrintDevice(BluetoothDeviceInfo cur)
    {
        Console.WriteLine("* Found device: '{0}' ", cur.DeviceName);
        if (infoRatherThanName) {
            try {
                var vs = cur.GetVersions();
                Console.WriteLine(vs.Manufacturer);
                Console.WriteLine(vs.LmpVersion);
                Console.WriteLine(vs.LmpSubversion);
                Console.WriteLine(vs.LmpSupportedFeatures);
            } catch (Exception ex) {
                Console.WriteLine("Failed to get remote device versions info: "
                    + ex.Message);
            }
        }
    }

    //----
    private static bool GetCanConnectTo(BluetoothDeviceInfo device)
    {
        bool inRange;
        Guid fakeUuid = new Guid("{F13F471D-47CB-41d6-9609-BAD0690BF891}");
        try {
            ServiceRecord[] records = device.GetServiceRecords(fakeUuid);
            Debug.Assert(records.Length == 0, "Why are we getting any records?? len: " + records.Length);
            inRange = true;
        } catch (SocketException) {
            inRange = false;
        }
        return inRange;
    }

}
}

标签: c#bluetoothbluetooth-lowenergy

解决方案


推荐阅读