.net 蓝牙编程-可用设备

.net 蓝牙编程-可用设备,.net,bluetooth,.net,Bluetooth,我使用了以下函数,gots来自Microsoft: public List<Device> DiscoverAllDevices() { List<Device> devices = new List<Device>(); // Initialize WinSock WsaData wsadata = new WsaData();

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

        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;
        }
public List DiscoverAllDevices()
{
列表设备=新列表();
//初始化WinSock
WsaData WsaData=新WsaData();
整数结果=
BluetoothHelper.WSAStartup(BluetoothHelper.MakeWord(2,2),
参考wsadata);
如果(结果!=0)
BluetoothHelper.GetError();
//扫描蓝牙设备
QuerySet wsaq=新QuerySet();
//使用特定于设备的属性初始化queryset结构
//信息。
wsaq.Size=Marshal.SizeOf(typeof(QuerySet));
wsaq.NameSpace=BluetoothHelper.NS_BTH;
IntPtr lookup=IntPtr.Zero;
uint flags=BluetoothHelper.LUP\u RETURN\u NAME
|BluetoothHelper.LUP_容器
|BluetoothHelper.LUP\u RETURN\u ADDR
|BluetoothHelper.LUP_FLUSHCACHE
|BluetoothHelper.LUP\u返回\u类型
|BluetoothHelper.LUP\u RETURN\u BLOB
|BluetoothHelper.LUP_RES_服务;
//启动受约束的客户端查询
//queryset结构中包含的信息。
结果=BluetoothHelper.wsalokupServiceBegin(wsaq,
旗帜,
ref查找);
如果(结果!=0)
BluetoothHelper.GetError();
while(0==结果)
{
int缓冲区=0x10000;
IntPtr bufferPtr=Marshal.AllocHGlobal(缓冲区);
QuerySet qsResult=新的QuerySet();
//检索请求的设备信息。
结果=BluetoothHelper.WSALookupServiceNext(查找,
旗帜,
参考缓冲器,
缓冲区(PTR);
如果(0==结果)
{
Marshal.ptr结构(bufferPtr,qsResult);
设备。添加(新设备(qsResult));
}
其他的
{
BluetoothHelper.GetError();
}
}
//终端设备查找
结果=BluetoothHelper.wsalokupServiceEnd(查找);
如果(结果!=0)
BluetoothHelper.GetError();
//清理winsock
结果=BluetoothHelper.WSACleanup();
如果(结果!=0)
BluetoothHelper.GetError();
返回装置;
}
但我需要知道设备是否在范围内的实际数据。此代码始终查找设备,如果以前找到过,即使此设备已关闭。为什么以及如何解决这个问题? 我几乎花了一整天的时间来寻找解决办法
谢谢

不幸的是,没有直接的API:-(这是Win32上Microsoft Bluetooth堆栈API中的一个主要漏洞

但是,通过组合许多API是可能的。我为我的共享源.NET Bluetooth调查并实现了这一点,如果您的程序可以用托管代码编写,那么它将为您节省大量使用Bluetooth的时间。:-)

无论如何,我获取“仅可发现”设备列表的方法记录在中,即使用Bluetooth events API查看已发现的设备,同时运行正常的发现