UWP Unpairing with device Failed
I'm currently developing a functionality to existing UWP app to test device's bluetooth device's capability. With that I'm currently facing 2 problems 1 one them is main one:
I'm building bluetooth capability based on Device Enumeration and pairing Sample from Windows Universal samples. Link here . I can successfully find and enumerate BT devices and even pair with them, but in a Weird Way I'm unable to Unpair with device.
Code I've made is found here, I added whole class for you to be able to test is fully if needed:
class UWP_Bluetooth
{
DeviceWatcher deviceWatcher;
public StringBuilder btNetworkInfo = new StringBuilder();
//EventHandler<BluetoothWin32AuthenticationEventArgs> authHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(handleAuthRequests);
//BluetoothWin32Authentication authenticator = new BluetoothWin32Authentication(authHandler);
public ObservableCollection<RfcommChatDeviceDisplay> ResultCollection
{
get;
private set;
}
public void SearchForBTNetworks()
{
ResultCollection = new ObservableCollection<RfcommChatDeviceDisplay>();
string requestedProperties = new string { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
deviceWatcher = DeviceInformation.CreateWatcher("(System.Devices.Aep.ProtocolId:="{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}")",
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
//deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Added += new TypedEventHandler<DeviceWatcher, DeviceInformation>((watcher, deviceInfo) =>
{
//Debug.WriteLine("Something added");
// Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
// Make sure device name isn't blank
if (deviceInfo.Name != "")
{
//Debug.WriteLine(deviceInfo.Name);
//Debug.WriteLine(deviceInfo.Id);
ResultCollection.Add(new RfcommChatDeviceDisplay(deviceInfo));
//rootPage.NotifyUser(
//String.Format("{0} devices found.", ResultCollection.Count),
//NotifyType.StatusMessage);
}
});
deviceWatcher.Updated += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>((watcher, deviceInfoUpdate) =>
{
//Debug.WriteLine("Something updated");
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Id == deviceInfoUpdate.Id)
{
rfcommInfoDisp.Update(deviceInfoUpdate);
break;
}
}
});
deviceWatcher.Removed += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>((watcher, deviceInfoUpdate) =>
{
//Debug.WriteLine("Something removed");
// Find the corresponding DeviceInformation in the collection and remove it
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Id == deviceInfoUpdate.Id)
{
//Debug.WriteLine(rfcommInfoDisp.Name + " Removed");
ResultCollection.Remove(rfcommInfoDisp);
break;
}
}
});
deviceWatcher.Start();
Debug.WriteLine("Started searching BT devices");
//base.OnNavigatedTo(e);
}
public void StopSearchingBTNetworks()
{
if (deviceWatcher.Status == DeviceWatcherStatus.Started)
{
deviceWatcher.Stop();
Debug.WriteLine("Stopped searching BT devices");
}
}
public string DisplayBTDevices()
{
btNetworkInfo.AppendLine("ID,Name,Type,Pairing");
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
btNetworkInfo.Append($"{rfcommInfoDisp.Id}t");
btNetworkInfo.Append($"{rfcommInfoDisp.Name}t");
btNetworkInfo.Append($"{rfcommInfoDisp.GetType()}t");
btNetworkInfo.Append($"{rfcommInfoDisp.DeviceInformation.Pairing.IsPaired}t");
Debug.WriteLine(rfcommInfoDisp.Name);
Debug.WriteLine(rfcommInfoDisp.Id);
btNetworkInfo.AppendLine();
//Debug.WriteLine("n");
}
return btNetworkInfo.ToString();
}
public async Task<int> PairWithSelectedAsync(string SSIDToPairWith)
{
int returnvalue = 5;
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Name == SSIDToPairWith)
{
returnvalue = 3;
Debug.WriteLine("Found device to connect to");
Debug.WriteLine(rfcommInfoDisp.Name);
Debug.WriteLine(rfcommInfoDisp.Id);
if (rfcommInfoDisp.DeviceInformation.Pairing.CanPair == true)
{
//rfcommInfoDisp.DeviceInformation.Properties.
DevicePairingResult pairingResult = await rfcommInfoDisp.DeviceInformation.Pairing.PairAsync();
if (pairingResult.Status == 0)
{
Debug.WriteLine("Connected with bluetooth device");
returnvalue = 0;
}
else
{
returnvalue = (int)pairingResult.Status;
}
}
}
}
if (returnvalue == 5)
Debug.WriteLine("Didn't find device");
return returnvalue;
}
public async Task<int> UnpairWithSelectedAsync(string SSIDToUnpairWith)
{
int returnvalue = 0;
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Name == SSIDToUnpairWith)
{
Debug.WriteLine("Found device to unconnect from");
//Debug.WriteLine(rfcommInfoDisp.Name);
//Debug.WriteLine(rfcommInfoDisp.Id);
Debug.WriteLine(rfcommInfoDisp.DeviceInformation.Pairing.IsPaired);
//rfcommInfoDisp.DeviceInformation.Properties.
DeviceUnpairingResult pairingResult = await rfcommInfoDisp.DeviceInformation.Pairing.UnpairAsync();
Debug.WriteLine("Unpairing result: " +pairingResult.Status);
if (pairingResult.Status == 0 )
{
Debug.WriteLine("Successfully unpaired with bluetooth device");
returnvalue = 0;
}
else
{
returnvalue = (int)pairingResult.Status;
}
//Debug.WriteLine("n");
}
}
return returnvalue;
}
}
public class RfcommChatDeviceDisplay : INotifyPropertyChanged
{
private DeviceInformation deviceInfo;
public RfcommChatDeviceDisplay(DeviceInformation deviceInfoIn)
{
deviceInfo = deviceInfoIn;
//UpdateGlyphBitmapImage();
}
public DeviceInformation DeviceInformation
{
get
{
return deviceInfo;
}
private set
{
deviceInfo = value;
}
}
public string Id
{
get
{
return deviceInfo.Id;
}
}
public string Name
{
get
{
return deviceInfo.Name;
}
}
public BitmapImage GlyphBitmapImage
{
get;
private set;
}
public void Update(DeviceInformationUpdate deviceInfoUpdate)
{
deviceInfo.Update(deviceInfoUpdate);
//UpdateGlyphBitmapImage();
}
/*
private async void UpdateGlyphBitmapImage()
{
DeviceThumbnail deviceThumbnail = await deviceInfo.GetGlyphThumbnailAsync();
BitmapImage glyphBitmapImage = new BitmapImage();
await glyphBitmapImage.SetSourceAsync(deviceThumbnail);
GlyphBitmapImage = glyphBitmapImage;
OnPropertyChanged("GlyphBitmapImage");
}
*/
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Currently main problem is lack of ability to unpair with device already paired with.
Bonus: If any of you know how to remove prompt or popup that appears if I attempt to pair with Bluetooth device (the device itself has 0 security which mans no asking for any PIN or prompt) that would also help a lot.
uwp bluetooth enumeration
add a comment |
I'm currently developing a functionality to existing UWP app to test device's bluetooth device's capability. With that I'm currently facing 2 problems 1 one them is main one:
I'm building bluetooth capability based on Device Enumeration and pairing Sample from Windows Universal samples. Link here . I can successfully find and enumerate BT devices and even pair with them, but in a Weird Way I'm unable to Unpair with device.
Code I've made is found here, I added whole class for you to be able to test is fully if needed:
class UWP_Bluetooth
{
DeviceWatcher deviceWatcher;
public StringBuilder btNetworkInfo = new StringBuilder();
//EventHandler<BluetoothWin32AuthenticationEventArgs> authHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(handleAuthRequests);
//BluetoothWin32Authentication authenticator = new BluetoothWin32Authentication(authHandler);
public ObservableCollection<RfcommChatDeviceDisplay> ResultCollection
{
get;
private set;
}
public void SearchForBTNetworks()
{
ResultCollection = new ObservableCollection<RfcommChatDeviceDisplay>();
string requestedProperties = new string { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
deviceWatcher = DeviceInformation.CreateWatcher("(System.Devices.Aep.ProtocolId:="{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}")",
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
//deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Added += new TypedEventHandler<DeviceWatcher, DeviceInformation>((watcher, deviceInfo) =>
{
//Debug.WriteLine("Something added");
// Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
// Make sure device name isn't blank
if (deviceInfo.Name != "")
{
//Debug.WriteLine(deviceInfo.Name);
//Debug.WriteLine(deviceInfo.Id);
ResultCollection.Add(new RfcommChatDeviceDisplay(deviceInfo));
//rootPage.NotifyUser(
//String.Format("{0} devices found.", ResultCollection.Count),
//NotifyType.StatusMessage);
}
});
deviceWatcher.Updated += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>((watcher, deviceInfoUpdate) =>
{
//Debug.WriteLine("Something updated");
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Id == deviceInfoUpdate.Id)
{
rfcommInfoDisp.Update(deviceInfoUpdate);
break;
}
}
});
deviceWatcher.Removed += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>((watcher, deviceInfoUpdate) =>
{
//Debug.WriteLine("Something removed");
// Find the corresponding DeviceInformation in the collection and remove it
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Id == deviceInfoUpdate.Id)
{
//Debug.WriteLine(rfcommInfoDisp.Name + " Removed");
ResultCollection.Remove(rfcommInfoDisp);
break;
}
}
});
deviceWatcher.Start();
Debug.WriteLine("Started searching BT devices");
//base.OnNavigatedTo(e);
}
public void StopSearchingBTNetworks()
{
if (deviceWatcher.Status == DeviceWatcherStatus.Started)
{
deviceWatcher.Stop();
Debug.WriteLine("Stopped searching BT devices");
}
}
public string DisplayBTDevices()
{
btNetworkInfo.AppendLine("ID,Name,Type,Pairing");
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
btNetworkInfo.Append($"{rfcommInfoDisp.Id}t");
btNetworkInfo.Append($"{rfcommInfoDisp.Name}t");
btNetworkInfo.Append($"{rfcommInfoDisp.GetType()}t");
btNetworkInfo.Append($"{rfcommInfoDisp.DeviceInformation.Pairing.IsPaired}t");
Debug.WriteLine(rfcommInfoDisp.Name);
Debug.WriteLine(rfcommInfoDisp.Id);
btNetworkInfo.AppendLine();
//Debug.WriteLine("n");
}
return btNetworkInfo.ToString();
}
public async Task<int> PairWithSelectedAsync(string SSIDToPairWith)
{
int returnvalue = 5;
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Name == SSIDToPairWith)
{
returnvalue = 3;
Debug.WriteLine("Found device to connect to");
Debug.WriteLine(rfcommInfoDisp.Name);
Debug.WriteLine(rfcommInfoDisp.Id);
if (rfcommInfoDisp.DeviceInformation.Pairing.CanPair == true)
{
//rfcommInfoDisp.DeviceInformation.Properties.
DevicePairingResult pairingResult = await rfcommInfoDisp.DeviceInformation.Pairing.PairAsync();
if (pairingResult.Status == 0)
{
Debug.WriteLine("Connected with bluetooth device");
returnvalue = 0;
}
else
{
returnvalue = (int)pairingResult.Status;
}
}
}
}
if (returnvalue == 5)
Debug.WriteLine("Didn't find device");
return returnvalue;
}
public async Task<int> UnpairWithSelectedAsync(string SSIDToUnpairWith)
{
int returnvalue = 0;
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Name == SSIDToUnpairWith)
{
Debug.WriteLine("Found device to unconnect from");
//Debug.WriteLine(rfcommInfoDisp.Name);
//Debug.WriteLine(rfcommInfoDisp.Id);
Debug.WriteLine(rfcommInfoDisp.DeviceInformation.Pairing.IsPaired);
//rfcommInfoDisp.DeviceInformation.Properties.
DeviceUnpairingResult pairingResult = await rfcommInfoDisp.DeviceInformation.Pairing.UnpairAsync();
Debug.WriteLine("Unpairing result: " +pairingResult.Status);
if (pairingResult.Status == 0 )
{
Debug.WriteLine("Successfully unpaired with bluetooth device");
returnvalue = 0;
}
else
{
returnvalue = (int)pairingResult.Status;
}
//Debug.WriteLine("n");
}
}
return returnvalue;
}
}
public class RfcommChatDeviceDisplay : INotifyPropertyChanged
{
private DeviceInformation deviceInfo;
public RfcommChatDeviceDisplay(DeviceInformation deviceInfoIn)
{
deviceInfo = deviceInfoIn;
//UpdateGlyphBitmapImage();
}
public DeviceInformation DeviceInformation
{
get
{
return deviceInfo;
}
private set
{
deviceInfo = value;
}
}
public string Id
{
get
{
return deviceInfo.Id;
}
}
public string Name
{
get
{
return deviceInfo.Name;
}
}
public BitmapImage GlyphBitmapImage
{
get;
private set;
}
public void Update(DeviceInformationUpdate deviceInfoUpdate)
{
deviceInfo.Update(deviceInfoUpdate);
//UpdateGlyphBitmapImage();
}
/*
private async void UpdateGlyphBitmapImage()
{
DeviceThumbnail deviceThumbnail = await deviceInfo.GetGlyphThumbnailAsync();
BitmapImage glyphBitmapImage = new BitmapImage();
await glyphBitmapImage.SetSourceAsync(deviceThumbnail);
GlyphBitmapImage = glyphBitmapImage;
OnPropertyChanged("GlyphBitmapImage");
}
*/
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Currently main problem is lack of ability to unpair with device already paired with.
Bonus: If any of you know how to remove prompt or popup that appears if I attempt to pair with Bluetooth device (the device itself has 0 security which mans no asking for any PIN or prompt) that would also help a lot.
uwp bluetooth enumeration
Your code is incomplete. Have you tried to test 'unpairring' in UWP official code BluetoothRfcommChat sample to see if it works on your side?
– Xavier Xie - MSFT
Nov 23 '18 at 6:22
add a comment |
I'm currently developing a functionality to existing UWP app to test device's bluetooth device's capability. With that I'm currently facing 2 problems 1 one them is main one:
I'm building bluetooth capability based on Device Enumeration and pairing Sample from Windows Universal samples. Link here . I can successfully find and enumerate BT devices and even pair with them, but in a Weird Way I'm unable to Unpair with device.
Code I've made is found here, I added whole class for you to be able to test is fully if needed:
class UWP_Bluetooth
{
DeviceWatcher deviceWatcher;
public StringBuilder btNetworkInfo = new StringBuilder();
//EventHandler<BluetoothWin32AuthenticationEventArgs> authHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(handleAuthRequests);
//BluetoothWin32Authentication authenticator = new BluetoothWin32Authentication(authHandler);
public ObservableCollection<RfcommChatDeviceDisplay> ResultCollection
{
get;
private set;
}
public void SearchForBTNetworks()
{
ResultCollection = new ObservableCollection<RfcommChatDeviceDisplay>();
string requestedProperties = new string { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
deviceWatcher = DeviceInformation.CreateWatcher("(System.Devices.Aep.ProtocolId:="{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}")",
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
//deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Added += new TypedEventHandler<DeviceWatcher, DeviceInformation>((watcher, deviceInfo) =>
{
//Debug.WriteLine("Something added");
// Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
// Make sure device name isn't blank
if (deviceInfo.Name != "")
{
//Debug.WriteLine(deviceInfo.Name);
//Debug.WriteLine(deviceInfo.Id);
ResultCollection.Add(new RfcommChatDeviceDisplay(deviceInfo));
//rootPage.NotifyUser(
//String.Format("{0} devices found.", ResultCollection.Count),
//NotifyType.StatusMessage);
}
});
deviceWatcher.Updated += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>((watcher, deviceInfoUpdate) =>
{
//Debug.WriteLine("Something updated");
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Id == deviceInfoUpdate.Id)
{
rfcommInfoDisp.Update(deviceInfoUpdate);
break;
}
}
});
deviceWatcher.Removed += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>((watcher, deviceInfoUpdate) =>
{
//Debug.WriteLine("Something removed");
// Find the corresponding DeviceInformation in the collection and remove it
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Id == deviceInfoUpdate.Id)
{
//Debug.WriteLine(rfcommInfoDisp.Name + " Removed");
ResultCollection.Remove(rfcommInfoDisp);
break;
}
}
});
deviceWatcher.Start();
Debug.WriteLine("Started searching BT devices");
//base.OnNavigatedTo(e);
}
public void StopSearchingBTNetworks()
{
if (deviceWatcher.Status == DeviceWatcherStatus.Started)
{
deviceWatcher.Stop();
Debug.WriteLine("Stopped searching BT devices");
}
}
public string DisplayBTDevices()
{
btNetworkInfo.AppendLine("ID,Name,Type,Pairing");
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
btNetworkInfo.Append($"{rfcommInfoDisp.Id}t");
btNetworkInfo.Append($"{rfcommInfoDisp.Name}t");
btNetworkInfo.Append($"{rfcommInfoDisp.GetType()}t");
btNetworkInfo.Append($"{rfcommInfoDisp.DeviceInformation.Pairing.IsPaired}t");
Debug.WriteLine(rfcommInfoDisp.Name);
Debug.WriteLine(rfcommInfoDisp.Id);
btNetworkInfo.AppendLine();
//Debug.WriteLine("n");
}
return btNetworkInfo.ToString();
}
public async Task<int> PairWithSelectedAsync(string SSIDToPairWith)
{
int returnvalue = 5;
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Name == SSIDToPairWith)
{
returnvalue = 3;
Debug.WriteLine("Found device to connect to");
Debug.WriteLine(rfcommInfoDisp.Name);
Debug.WriteLine(rfcommInfoDisp.Id);
if (rfcommInfoDisp.DeviceInformation.Pairing.CanPair == true)
{
//rfcommInfoDisp.DeviceInformation.Properties.
DevicePairingResult pairingResult = await rfcommInfoDisp.DeviceInformation.Pairing.PairAsync();
if (pairingResult.Status == 0)
{
Debug.WriteLine("Connected with bluetooth device");
returnvalue = 0;
}
else
{
returnvalue = (int)pairingResult.Status;
}
}
}
}
if (returnvalue == 5)
Debug.WriteLine("Didn't find device");
return returnvalue;
}
public async Task<int> UnpairWithSelectedAsync(string SSIDToUnpairWith)
{
int returnvalue = 0;
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Name == SSIDToUnpairWith)
{
Debug.WriteLine("Found device to unconnect from");
//Debug.WriteLine(rfcommInfoDisp.Name);
//Debug.WriteLine(rfcommInfoDisp.Id);
Debug.WriteLine(rfcommInfoDisp.DeviceInformation.Pairing.IsPaired);
//rfcommInfoDisp.DeviceInformation.Properties.
DeviceUnpairingResult pairingResult = await rfcommInfoDisp.DeviceInformation.Pairing.UnpairAsync();
Debug.WriteLine("Unpairing result: " +pairingResult.Status);
if (pairingResult.Status == 0 )
{
Debug.WriteLine("Successfully unpaired with bluetooth device");
returnvalue = 0;
}
else
{
returnvalue = (int)pairingResult.Status;
}
//Debug.WriteLine("n");
}
}
return returnvalue;
}
}
public class RfcommChatDeviceDisplay : INotifyPropertyChanged
{
private DeviceInformation deviceInfo;
public RfcommChatDeviceDisplay(DeviceInformation deviceInfoIn)
{
deviceInfo = deviceInfoIn;
//UpdateGlyphBitmapImage();
}
public DeviceInformation DeviceInformation
{
get
{
return deviceInfo;
}
private set
{
deviceInfo = value;
}
}
public string Id
{
get
{
return deviceInfo.Id;
}
}
public string Name
{
get
{
return deviceInfo.Name;
}
}
public BitmapImage GlyphBitmapImage
{
get;
private set;
}
public void Update(DeviceInformationUpdate deviceInfoUpdate)
{
deviceInfo.Update(deviceInfoUpdate);
//UpdateGlyphBitmapImage();
}
/*
private async void UpdateGlyphBitmapImage()
{
DeviceThumbnail deviceThumbnail = await deviceInfo.GetGlyphThumbnailAsync();
BitmapImage glyphBitmapImage = new BitmapImage();
await glyphBitmapImage.SetSourceAsync(deviceThumbnail);
GlyphBitmapImage = glyphBitmapImage;
OnPropertyChanged("GlyphBitmapImage");
}
*/
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Currently main problem is lack of ability to unpair with device already paired with.
Bonus: If any of you know how to remove prompt or popup that appears if I attempt to pair with Bluetooth device (the device itself has 0 security which mans no asking for any PIN or prompt) that would also help a lot.
uwp bluetooth enumeration
I'm currently developing a functionality to existing UWP app to test device's bluetooth device's capability. With that I'm currently facing 2 problems 1 one them is main one:
I'm building bluetooth capability based on Device Enumeration and pairing Sample from Windows Universal samples. Link here . I can successfully find and enumerate BT devices and even pair with them, but in a Weird Way I'm unable to Unpair with device.
Code I've made is found here, I added whole class for you to be able to test is fully if needed:
class UWP_Bluetooth
{
DeviceWatcher deviceWatcher;
public StringBuilder btNetworkInfo = new StringBuilder();
//EventHandler<BluetoothWin32AuthenticationEventArgs> authHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(handleAuthRequests);
//BluetoothWin32Authentication authenticator = new BluetoothWin32Authentication(authHandler);
public ObservableCollection<RfcommChatDeviceDisplay> ResultCollection
{
get;
private set;
}
public void SearchForBTNetworks()
{
ResultCollection = new ObservableCollection<RfcommChatDeviceDisplay>();
string requestedProperties = new string { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
deviceWatcher = DeviceInformation.CreateWatcher("(System.Devices.Aep.ProtocolId:="{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}")",
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
//deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Added += new TypedEventHandler<DeviceWatcher, DeviceInformation>((watcher, deviceInfo) =>
{
//Debug.WriteLine("Something added");
// Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
// Make sure device name isn't blank
if (deviceInfo.Name != "")
{
//Debug.WriteLine(deviceInfo.Name);
//Debug.WriteLine(deviceInfo.Id);
ResultCollection.Add(new RfcommChatDeviceDisplay(deviceInfo));
//rootPage.NotifyUser(
//String.Format("{0} devices found.", ResultCollection.Count),
//NotifyType.StatusMessage);
}
});
deviceWatcher.Updated += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>((watcher, deviceInfoUpdate) =>
{
//Debug.WriteLine("Something updated");
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Id == deviceInfoUpdate.Id)
{
rfcommInfoDisp.Update(deviceInfoUpdate);
break;
}
}
});
deviceWatcher.Removed += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>((watcher, deviceInfoUpdate) =>
{
//Debug.WriteLine("Something removed");
// Find the corresponding DeviceInformation in the collection and remove it
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Id == deviceInfoUpdate.Id)
{
//Debug.WriteLine(rfcommInfoDisp.Name + " Removed");
ResultCollection.Remove(rfcommInfoDisp);
break;
}
}
});
deviceWatcher.Start();
Debug.WriteLine("Started searching BT devices");
//base.OnNavigatedTo(e);
}
public void StopSearchingBTNetworks()
{
if (deviceWatcher.Status == DeviceWatcherStatus.Started)
{
deviceWatcher.Stop();
Debug.WriteLine("Stopped searching BT devices");
}
}
public string DisplayBTDevices()
{
btNetworkInfo.AppendLine("ID,Name,Type,Pairing");
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
btNetworkInfo.Append($"{rfcommInfoDisp.Id}t");
btNetworkInfo.Append($"{rfcommInfoDisp.Name}t");
btNetworkInfo.Append($"{rfcommInfoDisp.GetType()}t");
btNetworkInfo.Append($"{rfcommInfoDisp.DeviceInformation.Pairing.IsPaired}t");
Debug.WriteLine(rfcommInfoDisp.Name);
Debug.WriteLine(rfcommInfoDisp.Id);
btNetworkInfo.AppendLine();
//Debug.WriteLine("n");
}
return btNetworkInfo.ToString();
}
public async Task<int> PairWithSelectedAsync(string SSIDToPairWith)
{
int returnvalue = 5;
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Name == SSIDToPairWith)
{
returnvalue = 3;
Debug.WriteLine("Found device to connect to");
Debug.WriteLine(rfcommInfoDisp.Name);
Debug.WriteLine(rfcommInfoDisp.Id);
if (rfcommInfoDisp.DeviceInformation.Pairing.CanPair == true)
{
//rfcommInfoDisp.DeviceInformation.Properties.
DevicePairingResult pairingResult = await rfcommInfoDisp.DeviceInformation.Pairing.PairAsync();
if (pairingResult.Status == 0)
{
Debug.WriteLine("Connected with bluetooth device");
returnvalue = 0;
}
else
{
returnvalue = (int)pairingResult.Status;
}
}
}
}
if (returnvalue == 5)
Debug.WriteLine("Didn't find device");
return returnvalue;
}
public async Task<int> UnpairWithSelectedAsync(string SSIDToUnpairWith)
{
int returnvalue = 0;
foreach (RfcommChatDeviceDisplay rfcommInfoDisp in ResultCollection)
{
if (rfcommInfoDisp.Name == SSIDToUnpairWith)
{
Debug.WriteLine("Found device to unconnect from");
//Debug.WriteLine(rfcommInfoDisp.Name);
//Debug.WriteLine(rfcommInfoDisp.Id);
Debug.WriteLine(rfcommInfoDisp.DeviceInformation.Pairing.IsPaired);
//rfcommInfoDisp.DeviceInformation.Properties.
DeviceUnpairingResult pairingResult = await rfcommInfoDisp.DeviceInformation.Pairing.UnpairAsync();
Debug.WriteLine("Unpairing result: " +pairingResult.Status);
if (pairingResult.Status == 0 )
{
Debug.WriteLine("Successfully unpaired with bluetooth device");
returnvalue = 0;
}
else
{
returnvalue = (int)pairingResult.Status;
}
//Debug.WriteLine("n");
}
}
return returnvalue;
}
}
public class RfcommChatDeviceDisplay : INotifyPropertyChanged
{
private DeviceInformation deviceInfo;
public RfcommChatDeviceDisplay(DeviceInformation deviceInfoIn)
{
deviceInfo = deviceInfoIn;
//UpdateGlyphBitmapImage();
}
public DeviceInformation DeviceInformation
{
get
{
return deviceInfo;
}
private set
{
deviceInfo = value;
}
}
public string Id
{
get
{
return deviceInfo.Id;
}
}
public string Name
{
get
{
return deviceInfo.Name;
}
}
public BitmapImage GlyphBitmapImage
{
get;
private set;
}
public void Update(DeviceInformationUpdate deviceInfoUpdate)
{
deviceInfo.Update(deviceInfoUpdate);
//UpdateGlyphBitmapImage();
}
/*
private async void UpdateGlyphBitmapImage()
{
DeviceThumbnail deviceThumbnail = await deviceInfo.GetGlyphThumbnailAsync();
BitmapImage glyphBitmapImage = new BitmapImage();
await glyphBitmapImage.SetSourceAsync(deviceThumbnail);
GlyphBitmapImage = glyphBitmapImage;
OnPropertyChanged("GlyphBitmapImage");
}
*/
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Currently main problem is lack of ability to unpair with device already paired with.
Bonus: If any of you know how to remove prompt or popup that appears if I attempt to pair with Bluetooth device (the device itself has 0 security which mans no asking for any PIN or prompt) that would also help a lot.
uwp bluetooth enumeration
uwp bluetooth enumeration
asked Nov 19 '18 at 9:26
LempsPCLempsPC
375
375
Your code is incomplete. Have you tried to test 'unpairring' in UWP official code BluetoothRfcommChat sample to see if it works on your side?
– Xavier Xie - MSFT
Nov 23 '18 at 6:22
add a comment |
Your code is incomplete. Have you tried to test 'unpairring' in UWP official code BluetoothRfcommChat sample to see if it works on your side?
– Xavier Xie - MSFT
Nov 23 '18 at 6:22
Your code is incomplete. Have you tried to test 'unpairring' in UWP official code BluetoothRfcommChat sample to see if it works on your side?
– Xavier Xie - MSFT
Nov 23 '18 at 6:22
Your code is incomplete. Have you tried to test 'unpairring' in UWP official code BluetoothRfcommChat sample to see if it works on your side?
– Xavier Xie - MSFT
Nov 23 '18 at 6:22
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371609%2fuwp-unpairing-with-device-failed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371609%2fuwp-unpairing-with-device-failed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your code is incomplete. Have you tried to test 'unpairring' in UWP official code BluetoothRfcommChat sample to see if it works on your side?
– Xavier Xie - MSFT
Nov 23 '18 at 6:22