希望在 Windows 中编写蓝牙“hcitool"等效项 [英] Looking to write Bluetooth 'hcitool' equivelant in Windows

查看:32
本文介绍了希望在 Windows 中编写蓝牙“hcitool"等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Linux 中使用了 Bluez 蓝牙堆栈,它带有一个方便的实用程序hcitool".希望在 Windows 中构建具有相同或等效功能的类似内容.具体来说,'hcitool name ',显示指定设备是否在范围内.任何指导将不胜感激.

I have used Bluez Bluetooth stack in Linux which comes with a handy utility 'hcitool'. Looking to build something like that in Windows with same or equivalent functionality. Specifically, 'hcitool name < MAC >', which shows if the specified device is within range. Any guidance will be appreciated.

我有 Windows SDK v7 和 Visual Studio 2010,使用 C/C++

I have Windows SDK v7 with Visual Studio 2010, using C/C++

谢谢.

推荐答案

使用我的 32feet.NET类似以下的库.

Using my 32feet.NET library something like the following.

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

EDIT 3rd March: I've now added code to directly lookup the device by address rather than by using device discovery; so that's a simple 'new BluetoothDeviceInfo(...)'.

看看是否能找到您想要的设备.这要求远程设备仅处于可连接"模式,而前者要求其处于可发现"模式.(顺便说一句,我已经保留了发现代码.)

See if that finds the device you want. This requires the remote device to only be in "Connectable" mode whereas the former requires it to be in "Discoverable" mode. (BTW I've left the discovery code in place.)

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

EDIT 8th March: Now does a connect (using the SDP API) to check that the device is in range (and in connectable mode).

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

    }
}

这篇关于希望在 Windows 中编写蓝牙“hcitool"等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆