我们查看Window CE 4.2的SDK文档,得知获取本地设备ID的函数是BthReadLocalAddr,在btdrt.dll中。SDK文档中的英文原文是这样的:“This function retrieves the Bluetooth address of the current device.”好了,知道了这个就好说了:
首先封装本地托管函数:
[DllImport("Btdrt.dll", SetLastError=true)]
public static extern int BthReadLocalAddr(byte[] pba);
其实谈到获取远程设备的ID就涉及到如何去发现远程设备了,所以这里就一并把发现设备的方法也说明了吧。 发现设备需要用到三个Winsock API,分别是WSALookupServiceBegin、WSALookupServiceNext和WSALookupServiceEnd,这三个API到底起什幺作用可以去查看Windows CE 4.2的SDK,这里就不详细解释了,只谈一下几个需要注意的地方。
由于是针对蓝牙作的开发,所以我们要查看一下这些参数应该是哪些值。Windows CE 4.2的SDK中说,蓝牙开发时,struct LPWSAQUERYSET中的如下成员应当为这些值:
The dwSize member must be sizeof(WSAQUERYSET).
The lpBlob member (itself a pointer to a BLOB structure) is optional, but if used, the device inquire parameters valid for LUP_FLUSHCACHE are the following:
The cbSize member of the BLOB structure must be sizeof(BTH_QUERY_DEVICE).
The pBlobData member is a pointer to a BTH_QUERY_DEVICE structure, for which the LAP member is the Bluetooth inquiry access code, and the length member is the length of the inquiry, in seconds.