UWP与设备取消配对失败

UWP与设备取消配对失败,uwp,bluetooth,enumeration,Uwp,Bluetooth,Enumeration,我目前正在开发现有UWP应用程序的功能,以测试设备的蓝牙设备的功能。因此,我目前面临两个问题:一个是主要问题: 我正在基于Windows Universal示例中的设备枚举和配对示例构建蓝牙功能。我可以成功地找到并列举BT设备,甚至与它们配对,但奇怪的是,我无法与该设备解除配对。 我编写的代码可以在这里找到,我添加了整个类,以便您能够在需要时完全测试: class UWP_Bluetooth { DeviceWatcher deviceWatcher; public String

我目前正在开发现有UWP应用程序的功能,以测试设备的蓝牙设备的功能。因此,我目前面临两个问题:一个是主要问题: 我正在基于Windows Universal示例中的设备枚举和配对示例构建蓝牙功能。我可以成功地找到并列举BT设备,甚至与它们配对,但奇怪的是,我无法与该设备解除配对。 我编写的代码可以在这里找到,我添加了整个类,以便您能够在需要时完全测试:

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));
        }
    }
}
class-UWP\u蓝牙
{
设备观察者设备观察者;
公共StringBuilder btNetworkInfo=新StringBuilder();
//EventHandler authHandler=新的EventHandler(handleAuthRequests);
//BluetoothWin32Authentication authenticator=新的BluetoothWin32Authentication(authHandler);
公共可见收集结果收集
{
得到;
私人设置;
}
公共无效搜索btnetworks()
{
ResultCollection=新的ObservableCollection();
string[]requestedProperties=新字符串[]{“System.Devices.Aep.DeviceAddress”,“System.Devices.Aep.IsConnected”};
deviceWatcher=DeviceInformation.CreateWatcher(“(System.Devices.Aep.ProtocolId:=\”{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\”),
请求的属性,
设备信息种类、关联点);
//deviceWatcher.Added+=deviceWatcher\u Added;
deviceWatcher.Added+=新类型DevenHandler((watcher,deviceInfo)=>
{
//Debug.WriteLine(“添加的内容”);
//由于集合已绑定到UI元素,因此需要在UI线程上更新集合。
//确保设备名称不是空的
如果(deviceInfo.Name!=“”)
{
//Debug.WriteLine(deviceInfo.Name);
//Debug.WriteLine(deviceInfo.Id);
ResultCollection.Add(新的RfcommChatDeviceDisplay(deviceInfo));
//rootPage.NotifyUser(
//String.Format(“{0}已找到设备。”,ResultCollection.Count),
//NotifyType.StatusMessage);
}
});
deviceWatcher.Updated+=新类型DevenHandler((watcher,DeviceInfo Update)=>
{
//Debug.WriteLine(“更新的东西”);
foreach(RFCommChatDevice在ResultCollection中显示rfcommInfoDisp)
{
if(rfcomminfo disp.Id==设备信息更新.Id)
{
rfcomminfo.Update(设备信息更新);
打破
}
}
});
deviceWatcher.Removed+=新类型DevenHandler((watcher,DeviceInfo更新)=>
{
//Debug.WriteLine(“删除的东西”);
//在集合中找到相应的DeviceInformation并将其删除
foreach(RFCommChatDevice在ResultCollection中显示rfcommInfoDisp)
{
if(rfcomminfo disp.Id==设备信息更新.Id)
{
//Debug.WriteLine(rfcomminfo.Name+“已删除”);
ResultCollection.Remove(rfcomminfo-disp);
打破
}
}
});
deviceWatcher.Start();
WriteLine(“已开始搜索BT设备”);
//基地。导航到(e);
}
公共无效停止搜索BTNetworks()
{
if(deviceWatcher.Status==DeviceWatcherStatus.Started)
{
deviceWatcher.Stop();
Debug.WriteLine(“停止搜索BT设备”);
}
}
公共字符串DisplayBTDevices()
{
btNetworkInfo.AppendLine(“ID、名称、类型、配对”);
foreach(RFCommChatDevice在ResultCollection中显示rfcommInfoDisp)
{
btNetworkInfo.Append($“{rfcomminfo.Id}\t”);
btNetworkInfo.Append($“{rfcomminfo.Name}\t”);
btNetworkInfo.Append($“{rfcomminfo.GetType()}\t”);
btNetworkInfo.Append($“{rfcomminfo.DeviceInformation.Pairing.IsPaired}\t”);
Debug.WriteLine(rfcomminfo.Name);
Debug.WriteLine(rfcomminfo.Id);
btNetworkInfo.AppendLine();
//Debug.WriteLine(“\n”);
}
返回btNetworkInfo.ToString();
}
公共异步任务对与SelectedAsync(字符串SSIDToPairWith)
{
int返回值=5;
foreach(RFCommChatDevice在ResultCollection中显示rfcommInfoDisp)
{
if(rfcomminfo disp.Name==SSIDToPairWith)
{
返回值=3;
Debug.WriteLine(“找到要连接的设备”);
Debug.WriteLine(rfcomminfo.Name);
Debug.WriteLine(rfcomminfo.Id);
if(rfcomminfo.DeviceInformation.Pairing.CanPair==true)
{
//rfcomminfo disp.DeviceInformation.Properties。
DevicePairingResult pairingResult=等待rfcomminfo.DeviceInformation.Pairing.PairAsync();
if(pairingResult.Status==0)
{
Debug.WriteLine(“与蓝牙设备连接”);
返回值=0;
}
其他的
{
returnvalue=(int)pairingResult.Status;
}
}
}
}
如果(返回值==5)
Debug.WriteLine(“未找到设备”);
返回值;
}
公共异步任务与SelectedAsync不成对(字符串SSIDToUnpairWith)
{
int返回值=0;
foreach(RFCommChatDevice在ResultCollection中显示rfcommInfoDisp)
{
if(rfcomminfo.Name==SSIDToUnpairWith)
{
Debug.WriteLine(“找到要断开连接的设备”);
//调试命令