wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了希望在Windows中编写蓝牙’hcitool’equivelant大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我在 Linux中使用了Bluez蓝牙堆栈,它带有一个方便的实用程序’hcitool’.希望在Windows中使用相同或相同的功能构建类似的东西.具体来说,’hcitool name< MAC>‘,显示指定的设备是否在范围内. 任何指导将不胜感激. 我有使用C/C++的Visual studio 2010的Windows SDK v7 谢谢. 使用我的 32feet.NET库,如下所示. 编辑3月
我在 Linux中使用了Bluez蓝牙堆栈,它带有一个便的实用程序’hcitool’.希望在Windows中使用相同或相同的功能构建类似的东西.具体来说,’hcitool name< MAC>‘,显示指定的设备是否在范围内.
任何指导将不胜感激.

我有使用C/C++的Visual studio 2010的Windows SDK v7

谢谢.

解决方法

使用我的 32feet.NET库,如下所示.

编辑3月3日:我现在添加代码,通过地址直接查找设备,而不是使用设备发现;所以这是一个简单的’新的BluetoothDeviceInfo(…)’.

看看是否找到了你想要的设备.这要求远程设备仅处于“可连接”模式,而前者要求它处于“可发现”模式. (顺便说一句,我已经将发现代码留在了原地.)

编辑3月8日:现在进行连接(使用SDP API)检查设备是否在范围内(并且处于可连接模式).

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.@R_618_8313@;
                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,false,falsE);
            SimplecheckDevice(devices,ref foundDev);
            if (foundDev == null) {
                // Slow: Inquiry
                cli.DiscoverDevices(255,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 (infoRatherThan@R_618_8313@ {
                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;
        }

    }
}

大佬总结

以上是大佬教程为你收集整理的希望在Windows中编写蓝牙’hcitool’equivelant全部内容,希望文章能够帮你解决希望在Windows中编写蓝牙’hcitool’equivelant所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。