Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scan for paird and unpaired bluetooth devices in UWP app
public async Task<List<Models.Printer>> ListAllPrinters()
{
//Gets all printers inside the Database
var printerDevices = await _deviceService.GetAllPrinters();
//Gets all unpaired Bluetooth devices
var unpairedDevices = BluetoothDevice.GetDeviceSelectorFromPairingState(false);
//Gets all paired Bluetooth devices
var pairedDevices = BluetoothDevice.GetDeviceSelectorFromPairingState(true);
var unpairedNearDevices = (await DeviceInformation.FindAllAsync(unpairedDevices)).
Select(x => x.Name).ToList();
var pairedNearDevices = (await DeviceInformation.FindAllAsync(pairedDevices)).
Select(x => x.Name).ToList();
var nearbyDevices = unpairedNearDevices.Union(pairedNearDevices).ToList();
List <Models.Printer> nearbyDevicesInDB = new List<Models.Printer>();
List<Models.Printer> printers = new List<Models.Printer>();
List<Models.Printer> printersList = new List<Models.Printer>();
foreach (var printer in printerDevices)
{
printers.Add(new Models.Printer
{
SerialNumber = printer.SerialNumber,
MacAddress = printer.MacAddress,
IsInRange = false
});
//if nearby printer devices have the same name as device in database, then add it to nearbydevicesindb list
if (nearbyDevices.Contains(printer.SerialNumber))
{
nearbyDevicesInDB.Add(new Models.Printer
{
SerialNumber = printer.SerialNumber,
MacAddress = printer.MacAddress,
IsInRange = true
});
}
}
printersList = nearbyDevicesInDB;
printersList.Add(new Models.Printer
{
SerialNumber = "--",
MacAddress = "--"
});
printersList.AddRange(printers);
return printersList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment