locked
【UWP】Bluetooth Rfcomm(SPP)通信 異なるデバイス名(”SPP Dev”)が取得される RRS feed

  • 質問

  • お世話になります。

    ペアリング済みデバイスの列挙を行いターゲットとなるデバイスとSPP通信を行うソフトを作成しております。

    Windows8.1でのストアアプリ開発は完了しており、問題なくデバイス列挙が行えていました。

    現在はUWPに移行作業を行っておりまして、UWPで同様に取得すると”SPP Dev”となってしまいます。

    本来であれば、ペアリングデバイス検索時に表示されるデバイス名が表示されます。

    取得方式は下記2つ試しましたが同じ結果でした。

    ①DeviceInformation.FindAllAsync( RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));

      Deviceinformation.Name

    ②deviceWatcher = DeviceInformation.CreateWatcher(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));

      deviceWatcher.Addedハンドラにて

      Deviceinformation.Properties.TryGetValue("System.ItemNameDisplay", out object);

    UWPでは取得先が異なるのでしょうか。

    些細な情報でも構いません。ご助力の程よろしくお願いいたします。

    【参考】

    ・従来のソフトでしたら、Windows8.1、10ともに正常に取得できます。

    ・”SPP Dev”はデバイスマネージャーで該当COMのプロパティでは「バスによって報告されるデバイスの説明」にあたります。

    【開発環境】

    Windows10 pro, VisualStudio2017

    2018年12月10日 3:01

回答

  • Win10 1803で以下のコードで取得と通信できました。

    using System;
    using System.IO;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace App3
    {
        using System.Threading.Tasks;
        using Windows.Devices.Bluetooth.Rfcomm;
        using Windows.Devices.Enumeration;
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private System.Threading.CancellationTokenSource cancelSource;
    
            private async void SearchButton_Click(object sender, RoutedEventArgs e)
            {
                var button = (Button)sender;
                button.IsEnabled = false;
    
                cancelSource=new System.Threading.CancellationTokenSource(10000);
    
                var deviceInformations = await DeviceInformation.FindAllAsync(Windows.Devices.SerialCommunication.SerialDevice.GetDeviceSelector());
                foreach (var di in deviceInformations)
                {
                    string n = di.Name;
    
                    System.Diagnostics.Debug.WriteLine(di.Name);
                    if (di.Pairing != null && di?.Pairing.IsPaired == true && di.IsEnabled)
                    {
                        System.Diagnostics.Debug.WriteLine(di.Name);
    
                        //using (var sd = await Windows.Devices.SerialCommunication.SerialDevice.FromIdAsync(di.Id))
                        //{
                        //    await Read(sd.InputStream);
                        //}
    
                        break;
                    }
                }
           
                System.Diagnostics.Debug.WriteLine("*******************");
    
                deviceInformations = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
                foreach (var di in deviceInformations)
                {
                    var rfcom = await RfcommDeviceService.FromIdAsync(di.Id);
                    if (rfcom != null && rfcom.Device != null)
                    {
                        var dev = rfcom.Device;
                        if (dev.DeviceInformation.Pairing != null && dev.DeviceInformation.Pairing.IsPaired)
                        {
                            System.Diagnostics.Debug.WriteLine(rfcom.Device.Name);
    
                            //var socket = new Windows.Networking.Sockets.StreamSocket();
                            //await socket.ConnectAsync(rfcom.ConnectionHostName, rfcom.ConnectionServiceName, Windows.Networking.Sockets.SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
                            //await Read(socket.InputStream);
                        }
                        break;
                    }
                }
    
                cancelSource.Dispose();
                cancelSource=null;
                button.IsEnabled = true;
            }
    
            private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
                cancelSource?.Cancel();
            }
    
            private async Task Read(Windows.Storage.Streams.IInputStream input)
            {
                System.IO.MemoryStream ms = new MemoryStream();
                var dr = new Windows.Storage.Streams.DataReader(input);
                dr.InputStreamOptions = Windows.Storage.Streams.InputStreamOptions.Partial;
                for (; ; )
                {
                    if (cancelSource?.Token.IsCancellationRequested == true)
                    {
                        return;
                    }
                    var count = await dr.LoadAsync(1);
                    if (count > 0)
                    {
                        var bs = new byte[count];
                        dr.ReadBytes(bs);
    
                        foreach (byte b in bs)
                        {
                            System.Diagnostics.Debug.Write(b.ToString("X02"));
                        }
                        System.Diagnostics.Debug.WriteLine("");
                    }
                }
            }
        }
    }

    個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)

    • 回答としてマーク mk_y 2018年12月12日 6:39
    2018年12月11日 23:30

すべての返信

  • Win10 1803で以下のコードで取得と通信できました。

    using System;
    using System.IO;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace App3
    {
        using System.Threading.Tasks;
        using Windows.Devices.Bluetooth.Rfcomm;
        using Windows.Devices.Enumeration;
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private System.Threading.CancellationTokenSource cancelSource;
    
            private async void SearchButton_Click(object sender, RoutedEventArgs e)
            {
                var button = (Button)sender;
                button.IsEnabled = false;
    
                cancelSource=new System.Threading.CancellationTokenSource(10000);
    
                var deviceInformations = await DeviceInformation.FindAllAsync(Windows.Devices.SerialCommunication.SerialDevice.GetDeviceSelector());
                foreach (var di in deviceInformations)
                {
                    string n = di.Name;
    
                    System.Diagnostics.Debug.WriteLine(di.Name);
                    if (di.Pairing != null && di?.Pairing.IsPaired == true && di.IsEnabled)
                    {
                        System.Diagnostics.Debug.WriteLine(di.Name);
    
                        //using (var sd = await Windows.Devices.SerialCommunication.SerialDevice.FromIdAsync(di.Id))
                        //{
                        //    await Read(sd.InputStream);
                        //}
    
                        break;
                    }
                }
           
                System.Diagnostics.Debug.WriteLine("*******************");
    
                deviceInformations = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
                foreach (var di in deviceInformations)
                {
                    var rfcom = await RfcommDeviceService.FromIdAsync(di.Id);
                    if (rfcom != null && rfcom.Device != null)
                    {
                        var dev = rfcom.Device;
                        if (dev.DeviceInformation.Pairing != null && dev.DeviceInformation.Pairing.IsPaired)
                        {
                            System.Diagnostics.Debug.WriteLine(rfcom.Device.Name);
    
                            //var socket = new Windows.Networking.Sockets.StreamSocket();
                            //await socket.ConnectAsync(rfcom.ConnectionHostName, rfcom.ConnectionServiceName, Windows.Networking.Sockets.SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
                            //await Read(socket.InputStream);
                        }
                        break;
                    }
                }
    
                cancelSource.Dispose();
                cancelSource=null;
                button.IsEnabled = true;
            }
    
            private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
                cancelSource?.Cancel();
            }
    
            private async Task Read(Windows.Storage.Streams.IInputStream input)
            {
                System.IO.MemoryStream ms = new MemoryStream();
                var dr = new Windows.Storage.Streams.DataReader(input);
                dr.InputStreamOptions = Windows.Storage.Streams.InputStreamOptions.Partial;
                for (; ; )
                {
                    if (cancelSource?.Token.IsCancellationRequested == true)
                    {
                        return;
                    }
                    var count = await dr.LoadAsync(1);
                    if (count > 0)
                    {
                        var bs = new byte[count];
                        dr.ReadBytes(bs);
    
                        foreach (byte b in bs)
                        {
                            System.Diagnostics.Debug.Write(b.ToString("X02"));
                        }
                        System.Diagnostics.Debug.WriteLine("");
                    }
                }
            }
        }
    }

    個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)

    • 回答としてマーク mk_y 2018年12月12日 6:39
    2018年12月11日 23:30
  • gekka様

    ご提供いただいたコードで解決しました。

    ①di.Name (Deviceinformaition.Name)

    では同様に”SPP Dev”でした。

    ②rfcom.Device.Name (Deviceinformaition.Id.Device.Name)

    にて正常に取得することができました。

    ①で取得できない疑問は残りますが、とにかく大変助かりました。

    有難うございます。

    2018年12月12日 6:38