蓝牙编程 - 可用设备
问题描述:
我用下面的函数,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;
}
,但我需要知道实际的数据,如果设备在范围内或没有。这个代码总是找到设备,如果它之前被发现,即使这个设备被关闭。为什么以及如何解决这个问题? 我花了几乎整整一天找到解决方案 谢谢
答
不幸的是,没有直接的API。 :-(它是Win32上微软蓝牙堆栈的API中的一大空白。
但是它可以通过组合API的数量。我调查和实施为我的我的共享源代码的.NET蓝牙,http://32feet.codeplex.com如果你的程序可以写成托管代码,然后它将为你节省大量的时间在使用蓝牙。:-)
无论如何,我的方式获取'可发现只'设备列表记录在http://32feetnetdev.wordpress.com/2010/11/15/device-discovery-improvements-on-msftwin32/因此,这是使用蓝牙事件API查看发现的设备,并同时运行正常发现。