溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中調(diào)用紅外線遙控功能

發(fā)布時(shí)間:2021-03-10 14:59:25 來源:億速云 閱讀:780 作者:Leah 欄目:移動(dòng)開發(fā)

怎么在Android中調(diào)用紅外線遙控功能?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

具體如下:

public class ConsumerIrActivity extends Activity {
  private static final String TAG = "ConsumerIrTest";
  private TextView mFreqsText;
  // Android4.4之后 紅外遙控ConsumerIrManager,可以被小米4調(diào)用
  private ConsumerIrManager mCIR;
  @SuppressLint("InlinedApi")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.consumer_ir);
    // 獲取系統(tǒng)的紅外遙控服務(wù)
    mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);
    initViewsAndEvents();
  }
  private void initViewsAndEvents() {
    findViewById(R.id.send_button).setOnClickListener(mSendClickListener);
    findViewById(R.id.get_freqs_button)
        .setOnClickListener(mOnClickListener);
    mFreqsText = (TextView) findViewById(R.id.freqs_text);
  }
  View.OnClickListener mSendClickListener = new View.OnClickListener() {
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void onClick(View v) {
      if (!mCIR.hasIrEmitter()) {
        Log.e(TAG, "未找到紅外發(fā)身器!");
        return;
      }
      // 一種交替的載波序列模式,通過毫秒測(cè)量
      int[] pattern = { 1901, 4453, 625, 1614, 625, 1588, 625, 1614, 625,
          442, 625, 442, 625, 468, 625, 442, 625, 494, 572, 1614,
          625, 1588, 625, 1614, 625, 494, 572, 442, 651, 442, 625,
          442, 625, 442, 625, 1614, 625, 1588, 651, 1588, 625, 442,
          625, 494, 598, 442, 625, 442, 625, 520, 572, 442, 625, 442,
          625, 442, 651, 1588, 625, 1614, 625, 1588, 625, 1614, 625,
          1588, 625, 48958 };
      // 在38.4KHz條件下進(jìn)行模式轉(zhuǎn)換
      mCIR.transmit(38400, pattern);
    }
  };
  @SuppressLint("NewApi")
  View.OnClickListener mOnClickListener = new View.OnClickListener() {
    public void onClick(View v) {
      StringBuilder b = new StringBuilder();
      if (!mCIR.hasIrEmitter()) {
        mFreqsText.setText("未找到紅外發(fā)身器!");
        return;
      }
      // 獲得可用的載波頻率范圍
      ConsumerIrManager.CarrierFrequencyRange[] freqs = mCIR
          .getCarrierFrequencies();
      b.append("IR Carrier Frequencies:\n");// 紅外載波頻率
      // 邊里獲取頻率段
      for (ConsumerIrManager.CarrierFrequencyRange range : freqs) {
        b.append(String.format("  %d - %d\n",
            range.getMinFrequency(), range.getMaxFrequency()));
      }
      mFreqsText.setText(b.toString());// 顯示結(jié)果
    }
  };
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
<Button
    android:id="@+id/send_button"
    android:text="@string/ir_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <Button
    android:id="@+id/get_freqs_button"
    android:text="@string/ir_get_freqs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <ScrollView
    android:id="@+id/freqs_text_scroll"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
    <TextView
      android:id="@+id/freqs_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="3dp"
      android:paddingRight="3dp" />
  </ScrollView>
</LinearLayout>

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI