1
我用下面的函數,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;
}
,但我需要知道實際的數據,如果設備在範圍內或沒有。這個代碼總是找到設備,如果它之前被發現,即使這個設備被關閉。爲什麼以及如何解決這個問題? 我花了幾乎整整一天找到解決方案 謝謝