/// <summary> /// Discovers accessible Bluetooth devices and returns their names and addresses. /// </summary> /// <returns>An array of BluetoothDeviceInfo objects describing the devices discovered.</returns> public BluetoothDeviceInfo[] DiscoverDevices() { ArrayList al = new ArrayList(); try { int handle = 0; byte[] queryset = new byte[1024]; BitConverter.GetBytes((int)60).CopyTo(queryset, 0); BitConverter.GetBytes((int)16).CopyTo(queryset, 20); int bufferlen = 1024; int lookupresult = 0; //start looking for Bluetooth devices lookupresult = BthNsLookupServiceBegin(queryset, LookupFlags.Containers, ref handle); while (lookupresult != -1) { lookupresult = BthNsLookupServiceNext(handle, LookupFlags.ReturnAddr | LookupFlags.ReturnName, ref bufferlen, queryset); if (lookupresult != -1) { //pointer to outputbuffer int bufferptr = BitConverter.ToInt32(queryset, 48); //remote socket address int sockaddrptr = Marshal.ReadInt32((IntPtr)bufferptr, 8); //remote socket len int sockaddrlen = Marshal.ReadInt32((IntPtr)bufferptr, 12); BluetoothSocketAddress btsa = new BluetoothSocketAddress(); Marshal.Copy((IntPtr)sockaddrptr, btsa.ToByteArray(), 0, BluetoothSocketAddress.Length); //new deviceinfo BluetoothDeviceInfo newdevice = new BluetoothDeviceInfo(btsa.Address, Marshal.PtrToStringUni((IntPtr)BitConverter.ToInt32(queryset, 4))); //add to discovered list al.Add(newdevice); } } //stop looking lookupresult = BthNsLookupServiceEnd(handle); //return results } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in DiscoverDevices(): " + ex.Message); PriceMarkdown.Helpers.logError("Exception in DiscoverDevices(): " + ex.Message); } return((BluetoothDeviceInfo[])al.ToArray(typeof(BluetoothDeviceInfo))); }
/// <summary> /// Discovers accessible Bluetooth devices and returns their names and addresses. /// </summary> /// <returns>An array of BluetoothDeviceInfo objects describing the devices discovered.</returns> public BluetoothDeviceInfo[] DiscoverDevices() { ArrayList al = new ArrayList(); try { int handle = 0; byte[] queryset = new byte[1024]; BitConverter.GetBytes((int)60).CopyTo(queryset, 0); BitConverter.GetBytes((int)16).CopyTo(queryset, 20); int bufferlen = 1024; int lookupresult = 0; //start looking for Bluetooth devices lookupresult = BthNsLookupServiceBegin(queryset, LookupFlags.Containers, ref handle); while (lookupresult != -1) { lookupresult = BthNsLookupServiceNext(handle, LookupFlags.ReturnAddr | LookupFlags.ReturnName, ref bufferlen, queryset); if (lookupresult != -1) { //pointer to outputbuffer int bufferptr = BitConverter.ToInt32(queryset, 48); //remote socket address int sockaddrptr = Marshal.ReadInt32((IntPtr)bufferptr, 8); //remote socket len int sockaddrlen = Marshal.ReadInt32((IntPtr)bufferptr, 12); BluetoothSocketAddress btsa = new BluetoothSocketAddress(); Marshal.Copy((IntPtr)sockaddrptr, btsa.ToByteArray(), 0, BluetoothSocketAddress.Length); //new deviceinfo BluetoothDeviceInfo newdevice = new BluetoothDeviceInfo(btsa.Address, Marshal.PtrToStringUni((IntPtr)BitConverter.ToInt32(queryset, 4))); //add to discovered list al.Add(newdevice); } } //stop looking lookupresult = BthNsLookupServiceEnd(handle); //return results } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in DiscoverDevices(): " + ex.Message); PriceMarkdown.Helpers.logError("Exception in DiscoverDevices(): " + ex.Message); } return (BluetoothDeviceInfo[])al.ToArray(typeof(BluetoothDeviceInfo)); }