smartcard - 阿里云鹰卡组件

示例代码

var network = require('network');
var smartcard = require('smartcard');

var networkClient = network.openNetWorkClient();

var NETWORK_OPERATOR_UNKNOWN = 0;
var NETWORK_OPERATOR_CHINA_MOBILE = 1;
var NETWORK_OPERATOR_CHINA_UNICOM = 2;
var NETWORK_OPERATOR_CHINA_TELECOM = 3;
var NETWORK_OPERATOR_AUTO_NEXT = 4;

var networkOperatorNamesList = ['Unknown', 'China Mobile', 'China Unicom', 'China Telecom'];

function networkOperatorGet()
{
  var iccid = networkClient.getInfo().iccid;
  var iccidPrefix = iccid.substr(0, 6);

  switch (iccidPrefix) {
  case '898600':
  case '898602':
  case '898604':
  case '898607':
    return NETWORK_OPERATOR_CHINA_MOBILE;
  case '898601':
  case '898606':
  case '898609':
    return NETWORK_OPERATOR_CHINA_UNICOM;
  case '898603':
  case '898611':
    return NETWORK_OPERATOR_CHINA_TELECOM;
  default:
    console.log('unknown network operator ' + iccid);
  }
  return NETWORK_OPERATOR_UNKNOWN;
}

function networkSignalStrengthGet()
{
  return networkClient.getInfo().signal;
}

function smartcardSelectOperatorManual()
{
  console.log('Selecting operator ' + networkOperatorNamesList[NETWORK_OPERATOR_CHINA_MOBILE]);
  smartcard.select(NETWORK_OPERATOR_CHINA_MOBILE);
  var operator = networkOperatorGet();
  var strength = networkSignalStrengthGet();
  console.log('current operator is ' + networkOperatorNamesList[operator] + ', signal strength is ' + strength);

  console.log('Selecting operator ' + networkOperatorNamesList[NETWORK_OPERATOR_CHINA_UNICOM]);
  smartcard.select(NETWORK_OPERATOR_CHINA_UNICOM);
  var operator = networkOperatorGet();
  var strength = networkSignalStrengthGet();
  console.log('current operator is ' + networkOperatorNamesList[operator] + ', signal strength is ' + strength);

  console.log('Selecting operator ' + networkOperatorNamesList[NETWORK_OPERATOR_CHINA_TELECOM]);
  smartcard.select(NETWORK_OPERATOR_CHINA_TELECOM);
  var operator = networkOperatorGet();
  var strength = networkSignalStrengthGet();
  console.log('current operator is ' + networkOperatorNamesList[operator] + ', signal strength is ' + strength);
}

function smartcardInit()
{
  var ret = smartcard.init();
  if (ret != 0) {
  	console.log('smartcard init fail ' + ret);
  } else {
  	console.log('smartcard init success');
  	smartcardSelectOperatorManual();
  }
}

console.log('smartcard get network status');
var networkStatus = networkClient.getStatus();

if (networkStatus == 'connect') {
  smartcardInit();
} else {
  networkClient.on('connect', function() {
    smartcardInit();
  });
}

init()

初始化云鹰卡组件

入参

返回

  • Number类型。0 - 初始化成功,负值 - 初始化失败

select(Number operator)

切换运营商(支持中国移动、中国联通、中国电信)

入参

参数

类型

必填

描述

operator

Number

需要切换到的运行商ID,可选参数:

1 - 中国移动

2 - 中国联通

3 - 中国电信

4 - 自动选择

返回

  • Number类型。0 - 切换成功,负值 - 切换失败