locked
デバイスを使うときのFromIdAsyncの返り値がnullになる。 RRS feed

  • 質問

  • 外部デバイスと接続して、送られてくる情報(温度とか)をUWPアプリケーションで表示させてたいと考えております。

    BluetoothLEによるGATT接続を行い通信しようと思っていたのですが、SelectedBleDeviceIdは取得できているにも関わらず、

    bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(rootPage.SelectedBleDeviceId);

    bluetoothLeDevice は必ずnullになります。

    GattDeviceService service = await GattDeviceService.FromIdAsync(SelectedBleDeviceId);

    としても結果は同じでした。

    接続しているBluetooth機器に問題があるかもしれないと思い、有線によるUSBシリアル通信も行ってみたのですが、

    SerialDevice serialPort = await SerialDevice.FromIdAsync(entry.Id)

    serialPort がnullになります。Bluetoothだけの問題ではないので、何か根本的なところでミスや抜けがあるのではないかと考えております。

    外部デバイスを利用することができず、困っております。

    ご存知の方がおられましたら教えていただけますでしょうか。よろしくお願いいたします。

    シリアルポートのプログラムを記載させていただきます。

    public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } List<DeviceInformation> listOfDevices = new List<DeviceInformation>(); private async void ListAvailablePorts() { try { string aqs = SerialDevice.GetDeviceSelector(); var dis = await DeviceInformation.FindAllAsync(aqs); for (int i = 0; i < dis.Count; i++) { listOfDevices.Add(dis[i]); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } } SerialDevice serialPort; DataWriter dataWriteObject; DataReader dataReaderObject; private async Task OpenPort() { DeviceInformation entry = (DeviceInformation)listOfDevices[0];

    serialPort = await SerialDevice.FromIdAsync(entry.Id);

    // entry.Idには接続されているCOMポートやIDが入っているが、serialPort がnullにしかならない serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); serialPort.BaudRate = 9600; serialPort.Parity = SerialParity.None; serialPort.StopBits = SerialStopBitCount.One; serialPort.DataBits = 8; serialPort.Handshake = SerialHandshake.None; dataWriteObject = new DataWriter(serialPort.OutputStream); dataReaderObject = new DataReader(serialPort.InputStream); } private async void Button_Click(object sender, RoutedEventArgs e) { ListAvailablePorts(); if (listOfDevices.Count != 0) { await OpenPort(); } } }

    Package.appxmanifestの変更内容

    <Capabilities>を下記に変更。他はデフォルト

      <Capabilities>
        <Capability Name="internetClient" />
            <DeviceCapability Name="serialcommunication">
              <Device Id="vidpid:0D91 2025">
                <Function Type="name:serialPort" />
          </Device>
        </DeviceCapability>
      </Capabilities>


    • 編集済み Fumio.S 2017年12月26日 0:16 解決
    2017年11月30日 7:46

回答


  • 会社のセキュリティ関係でストアアプリからデバイスへのアクセスを禁止されていました。

    グループポリシーを外せば、動作いたしました。

    ありがとうございました。

    • 回答としてマーク Fumio.S 2017年12月26日 0:18
    2017年12月26日 0:18

すべての返信

  • こんにちは。

    DeviceCapabitily に bluetooth.genericAttributeProfile を指定していますか。
    CreatorsUpdate後からは上記を指定しないとBTのFromIdAsyncが取得できない現象が以前ありました。

    2017年11月30日 8:30
    モデレータ
  • こんにちは。

    返信ありがとうございます。

    bluetoothを使用するときは、下記のようにDeviceCapabitily に bluetooth.genericAttributeProfile を指定しています。

        <DeviceCapability Name="bluetooth.genericAttributeProfile">
          <Device Id="any">
            <Function Type="name:genericAccess" />
          </Device>
        </DeviceCapability>

    Function Typeの部分はUUIDを入れてみたりいろいろ試してみましたが駄目でした。

    2017年11月30日 8:48

  • 会社のセキュリティ関係でストアアプリからデバイスへのアクセスを禁止されていました。

    グループポリシーを外せば、動作いたしました。

    ありがとうございました。

    • 回答としてマーク Fumio.S 2017年12月26日 0:18
    2017年12月26日 0:18