[DllImport("Btdrt.dll", SetLastError=true)] public static extern int BthReadLocalAddr(byte[] pba); |
string text1 = ""; text1 = text1 + pba[5].ToString("X2") + ":"; text1 = text1 + pba [4].ToString("X2") + ":"; text1 = text1 + pba [3].ToString("X2") + ":"; text1 = text1 + pba [2].ToString("X2") + ":"; text1 = text1 + pba [1].ToString("X2") + ":"; return (text1 + pba [0].ToString("X2")); |
INT WSALookupServiceBegin( LPWSAQUERYSET lpqsRestrictions, DWORD dwControlFlags, LPHANDLE lphLookup ); |
[DllImport("ws2.dll", EntryPoint="WSALookupServiceBegin", SetLastError=true)] public static extern int CeLookupServiceBegin(byte[] pQuerySet, LookupFlags dwFlags, ref int lphLookup); |
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. The dwNameSpace member must be NS_BTH. All other WSAQUERYSET members are ignored. |
byte[] buffer1 = new byte[0x400]; BitConverter.GetBytes(60).CopyTo(buffer1, 0); GCHandle handle1 = GCHandle.Alloc(blob1.ToByteArray(), GCHandleType.Pinned); IntPtr ptr1 = handle1.AddrOfPinnedObject(); BitConverter.GetBytes((int) (ptr1.ToInt32() + 4)).CopyTo(buffer1, 0x38); |
int num5 = BitConverter.ToInt32(buffer1, 0x30); int num6 = Marshal.ReadInt32((IntPtr) num5, 8); int num7 = Marshal.ReadInt32((IntPtr) num5, 12); SocketAddress address1 = new SocketAddress(AddressFamily.Unspecified, num7); |
因为.net框架的地址族里面没有蓝牙,所以我们这里用的是AddressFamily.Unspecified。
然后的工作就是从中获取远程设备的ID了:
前面我们已经计算出,这个Address里面的前六个字节是byte数组形式的设备ID,第七到第二十二个字节是蓝牙的Service Guid,在后面四个字节是端口号,所以我们只需要分别提取出来即可。
四. 监听服务
监听服务调用的是非托管API WSASetService,其原型是
INT WSASetService( LPWSAQUERYSET lpqsRegInfo, WSAESETSERVICEOP essoperation, DWORD dwControlFlags ); |
lpqsRegInfo | dwSize | sizeof(WSAQUERYSET) |
lpszServiceInstanceName | Not supported on Windows CE. Set to 0. | |
lpServiceClassId | Not supported on Windows CE. Set to 0. | |
dwNameSpace | NS_BTH. | |
dwNumberOfCsAddrs | Not supported on Windows CE. Set to 0. | |
IpcsaBuffer | Not supported on Windows CE. Set to 0. | |
lpBlob | Points to a BTHNS_SETBLOB structure, containing information about the service to be added. | |
* |
All other WSAQUERYSET fields are ignored. |
[DllImport("mscoree", EntryPoint="@339")] public static extern int connect(int s, byte[] name, int namelen); |
new Socket((AddressFamily) 0x20, SocketType.Stream, ProtocolType.Ggp); |
[DllImport("Btdrt.dll", SetLastError=true)] public static extern int BthGetPINRequest(byte[] pba); |
[DllImport("btdrt.dll", SetLastError=true)] public static extern int BthSetPIN(byte[] pba, int cPinLength, byte[] ppin); |
[DllImport("Btdrt.dll", SetLastError=true)] public static extern int BthCreateACLConnection(byte[] pbt, ref ushort phandle); |
[DllImport("Btdrt.dll", SetLastError=true)] public static extern int BthAuthenticate(byte[] pbt); |
[DllImport("Btdrt.dll", SetLastError=true)] public static extern int BthCloseConnection(ushort handle); |
public enum RadioMode { Connectable = 1, Discoverable = 2, PowerOff = 0 } |
[DllImport("BthUtil.dll", SetLastError=true)] public static extern int BthSetMode(RadioMode dwMode); |
[DllImport("BthUtil.dll", SetLastError=true)] public static extern int BthGetMode(ref RadioMode dwMode); |