简体   繁体   中英

The type or namespace name 'BluetoothAddress' could not be found

I use vs 2017 and windows 7. I have installed 32feet.NET following this: Looking to write Bluetooth 'hcitool' equivelant in Windows .

But I get the errors:

The type or namespace name 'BluetoothAddress' could not be found

The type or namespace name 'BluetoothClient' could not be found

The type or namespace name 'BluetoothSecurity' could not be found

The type or namespace name 'BluetoothDeviceInfo' could not be found

The type or namespace name 'ServiceRecord' could not be found

I have successfully installed InTheHand.Devices.Bluetooth and There is no warning in the using sentence so the namespace is successfully referenced.

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;
    }

   }
}

Try to add the following namespaces to your code:

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

The BluetoothAddress is in the 32feet.NET nuget package.

You're using the preview version of InTheHand which is missing the BluetoothAddress , the BluetoothRadio , BluetoothDeviceInfo and others. I think you're referencing the wiki for the 32feet.NET , which is the legacy nuget package. If you install 32feet.NET v3.5.0.0 , you will find all your missing bindings.

Adding 32feet.NET and changing a couple of things:

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

it will compile succesfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM