蓝牙编程 - 可用设备

时间:2011-04-08 23:51:10

标签: .net bluetooth

我使用了以下函数,来自Microsoft的gots:

        public List<Device> DiscoverAllDevices()
        {
            List<Device> devices = new List<Device>();

            // Initialize WinSock
            WsaData wsadata = new WsaData();

            int result =
                BluetoothHelper.WSAStartup(BluetoothHelper.MakeWord(2, 2),
                                           ref wsadata);
            if (result != 0)
                BluetoothHelper.GetError();

            // Scan for bluetooth devices
            QuerySet wsaq = new QuerySet();
            //Initialize queryset structure with device specific 
            //information.
            wsaq.Size = Marshal.SizeOf(typeof(QuerySet));
            wsaq.NameSpace = BluetoothHelper.NS_BTH;
            IntPtr lookup = IntPtr.Zero;
            uint flags = BluetoothHelper.LUP_RETURN_NAME
                           | BluetoothHelper.LUP_CONTAINERS
                           | BluetoothHelper.LUP_RETURN_ADDR
                           | BluetoothHelper.LUP_FLUSHCACHE
                           | BluetoothHelper.LUP_RETURN_TYPE
                           | BluetoothHelper.LUP_RETURN_BLOB
                           | BluetoothHelper.LUP_RES_SERVICE;

            //Initiates a client query that is constrained by the 
            //information contained within a queryset structure.

            result = BluetoothHelper.WSALookupServiceBegin(wsaq,
                                                           flags,
                                                           ref lookup);
            if (result != 0)
                BluetoothHelper.GetError();

            while (0 == result)
            {
                int buffer = 0x10000;

                IntPtr bufferPtr = Marshal.AllocHGlobal(buffer);
                QuerySet qsResult = new QuerySet();

                //Retrieves the requested device information.
                result = BluetoothHelper.WSALookupServiceNext(lookup,
                                                         flags,
                                                         ref buffer,
                                                         bufferPtr);
                if (0 == result)
                {
                    Marshal.PtrToStructure(bufferPtr, qsResult);
                    devices.Add(new Device(qsResult));
                }
                else
                {
                    BluetoothHelper.GetError();
                }
            }
            //end device-lookup
            result = BluetoothHelper.WSALookupServiceEnd(lookup);
            if (result != 0)
                BluetoothHelper.GetError();

            // cleanup winsock
            result = BluetoothHelper.WSACleanup();

            if (result != 0)
                BluetoothHelper.GetError();

            return devices;
        }

但是如果设备在范围内,我需要知道实际数据。如果之前找到设备,此代码始终会找到设备,即使此设备已关闭。为什么以及如何解决这个问题? 我花了差不多一整天才找到解决方案 感谢

1 个答案:

答案 0 :(得分:0)

不幸的是,没有直接的API。 :-(这是Win32上微软蓝牙堆栈API的主要差距。

然而,通过组合许多API是可能的。我为我的共享源.NET蓝牙调查并实现了这个,http://32feet.codeplex.com如果你的程序可以用托管代码编写,那么它将为你节省很多时间使用蓝牙。 : - )

无论如何,获取“仅可发现”设备列表的方式记录在http://32feetnetdev.wordpress.com/2010/11/15/device-discovery-improvements-on-msftwin32/,这样就可以使用蓝牙事件API查看已发现的设备,并同时运行常规发现。