找不到类型或名称空间名称“ BluetoothAddress”

时间:2019-05-03 13:03:27

标签: c# bluetooth

我使用vs 2017和Windows 7。 我已经按照以下步骤安装了32feet.NET:Looking to write Bluetooth 'hcitool' equivelant in Windows

但是我得到了错误:

找不到类型或名称空间名称'BluetoothAddress'

找不到类型或名称空间名称'BluetoothClient'

找不到类型或名称空间名称'BluetoothSecurity'

找不到类型或名称空间名称'BluetoothDeviceInfo'

找不到类型或名称空间名称'ServiceRecord'

我已经成功安装了InTheHand.Devices.Bluetooth,并且using语句中没有警告,因此成功引用了命名空间。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using InTheHand.Devices.Bluetooth;
using InTheHand.Networking;
using InTheHand.Networking.Sockets;
using InTheHand;


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);
        var dev = new BluetoothDevice();
        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;
    }

   }
}

2 个答案:

答案 0 :(得分:0)

尝试将以下名称空间添加到您的代码中:

using InTheHand.Net;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;

答案 1 :(得分:0)

BluetoothAddress 位于 32feet.NET nuget程序包中。

您正在使用InTheHand的预览版,其中缺少 BluetoothAddress BluetoothRadio BluetoothDeviceInfo 等。 我认为您引用的是 32feet.NET 的Wiki,它是旧版nuget程序包。 如果安装 32feet.NET v3.5.0.0 ,则会找到所有缺少的绑定。

添加 32feet.NET 并进行以下更改:

var dev = new BluetoothDevice(); 
//this becomes this:
var dev = new BluetoothDeviceInfo(_searchAddress);

它将成功编译。