SMS デバイスの列挙

モバイル ブロードバンド SMS プラットフォームでは、最初の SMS 対応モバイル ブロードバンド デバイスを取得したり、SMS 対応のすべてのモバイル ブロードバンド デバイスのリストを取得したりできます。 下のコード例は、既定の SMS デバイスと特定のデバイスを使用して SMS オブジェクトをインスタンス化する方法を示しています。

Windows 8、Windows 8.1、または Windows 10 で C# または C++ を使用するアプリでは、STA スレッドで GetDefaultAsync または FromIdAsync を呼び出すために SmsDevice オブジェクトを最初に使用する必要があります。 MTA スレッドからの呼び出しにより、不定の動作が発生する可能性があります。

既定の SMS デバイスを使用する JavaScript コード例

var smsDevice = new Windows.Devices.Sms.SmsDevice.getDefault();

try
{
  var smsDeviceOperation = Windows.Devices.Sms.SmsDevice.getDefaultAsync();
  smsDeviceOperation.done(smsDeviceReceived, errorCallback);
}
catch (err)
{
  // handle error
}

すべての SMS デバイスを列挙する JavaScript コード例

Windows.Devices.Enumeration.DeviceInformation.findAllAsync(Windows.Devices.Sms.SmsDevice.getDeviceSelector()).then(function (smsdevices) 
{
  if (smsdevices.length > 0)
  {
    // for simplicity we choose the first device
    var smsDeviceId = smsdevices[0].Id;
    var smsDeviceOperation = Windows.Devices.Sms.SmsDevice.fromIdAsync(smsNotificationDetails.deviceId); 
    smsDeviceOperation.done(function (smsDeviceResult)
    {
      smsDevice = smsDeviceResult;
    }, errorCallback);
  }
}

SMS デバイスのアクセス エラーを検出する

アプリが SMS にアクセスできないために SMS デバイスの列挙に失敗したかどうかを検出できます。 これは、ユーザーがアプリへのアクセスを明示的に拒否した場合、またはデバイス メタデータがアプリへのアクセスを許可していない場合に発生する可能性があります。

SMS デバイスのアクセス エラーを検出する JavaScript コード例

Windows.Devices.Enumeration.DeviceInformation.findAllAsync(Windows.Devices.Sms.SmsDevice.getDeviceSelector()).then(function (smsdevices)
{
  if (smsdevices.length > 0)
  {
    // for simplicity we choose the first device
    var smsDeviceId = smsdevices[0].Id.slice(startIndex,endIndex + 1);
    var smsDeviceOperation = Windows.Devices.Sms.SmsDevice.fromIdAsync(smsNotificationDetails.deviceId); 
    smsDeviceOperation.done(function (smsDeviceResult)
    {
      smsDevice = smsDeviceResult;
    }, errorCallback); 

    // detect if SMS access is denied due to user not granting app consent to use SMS or if metadata is missing or invalid.

  }

function errorCallback(error)
{
  WinJS.log(error.name + " : " + error.description, "sample", "error");

  // If the error was caused due to access being denied to this app
  // then the HResult is set to E_ACCESSDENIED (0x80007005)

  // var hResult = hex(error.number);

}

function hex(nmb)
{
  if (nmb >= 0)
  {
    return nmb.toString(16);
  }
  else
  {
    return (nmb + 0x100000000).toString(16);
  }
}

SMS アプリの開発