本文實例講述了Android編程調用紅外線遙控功能。分享給大家供大家參考,具體如下:
Android API Demos中有紅外線遙控的小例子,在網上找了很久相關的資料,發現比較少,或許找的方法不對。
Github上有一個與之相關的開源項目https://github.com/timnew/AndroidInfrared,還沒來得及學習。希望有相關資料或學習項目的大神們多指導 。
/** * Android紅外線遙控官方Demo * * @description: * @author ldm * @date 2016-4-28 下午5:06:28 */public class ConsumerIrActivity extends Activity { private static final String TAG = "ConsumerIrTest"; private TextView mFreqsText; // Android4.4之后 紅外遙控ConsumerIrManager,可以被小米4調用 private ConsumerIrManager mCIR; @SuppressLint("InlinedApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.consumer_ir); // 獲取系統的紅外遙控服務 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, "未找到紅外發身器!"); return; } // 一種交替的載波序列模式,通過毫秒測量 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條件下進行模式轉換 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("未找到紅外發身器!"); 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());// 顯示結果 } };}<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>
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android資源操作技巧匯總》、《Android操作json格式數據技巧總結》、《Android開發入門與進階教程》、《Android編程之activity操作技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答