Bagikan melalui


Menghitung perangkat SMS

Platform MOBILE Broadband SMS menyediakan kemampuan untuk mendapatkan perangkat broadband seluler berkemampuan SMS pertama, atau untuk mendapatkan daftar semua perangkat broadband seluler berkemampuan SMS. Contoh kode berikut menunjukkan pembuatan instans objek SMS dengan perangkat SMS default dan dengan perangkat tertentu.

Catatan Dalam aplikasi yang menggunakan C# atau C++ di Windows 8, Windows 8.1, atau Windows 10, penggunaan pertama objek SmsDevice untuk memanggil GetDefaultAsync atau FromIdAsync harus berada di utas STA. Panggilan dari utas MTA dapat mengakibatkan perilaku yang tidak terdefinisi.

Contoh kode JavaScript untuk menggunakan perangkat SMS default

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

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

Contoh kode JavaScript untuk menghitung semua perangkat SMS

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);
  }
}

Mendeteksi kesalahan akses perangkat SMS

Anda dapat mendeteksi apakah menghitung perangkat SMS gagal karena aplikasi tidak memiliki akses ke SMS. Ini dapat terjadi jika pengguna secara eksplisit menolak akses ke aplikasi atau jika metadata perangkat belum memberikan akses ke aplikasi.

Contoh kode JavaScript untuk mendeteksi kesalahan akses perangkat SMS

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);
  }
}

Mengembangkan aplikasi SMS