Android編程調(diào)用紅外線(xiàn)遙控功能示例
本文實(shí)例講述了Android編程調(diào)用紅外線(xiàn)遙控功能。分享給大家供大家參考,具體如下:
Android API Demos中有紅外線(xiàn)遙控的小例子,在網(wǎng)上找了很久相關(guān)的資料,發(fā)現(xiàn)比較少,或許找的方法不對(duì)。
Github上有一個(gè)與之相關(guān)的開(kāi)源項(xiàng)目https://github.com/timnew/AndroidInfrared,還沒(méi)來(lái)得及學(xué)習(xí)。希望有相關(guān)資料或?qū)W習(xí)項(xiàng)目的大神們多指導(dǎo) 。
/** * Android紅外線(xiàn)遙控官方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調(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; } // 一種交替的載波序列模式,通過(guò)毫秒測(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>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android資源操作技巧匯總》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android AsyncTask 后監(jiān)聽(tīng)異步加載完畢的動(dòng)作詳解
這篇文章主要介紹了Android 使用AsyncTask 后監(jiān)聽(tīng)異步加載完畢的動(dòng)作的相關(guān)資料,需要的朋友可以參考下2016-11-11Android Studio實(shí)現(xiàn)長(zhǎng)方體表面積計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)長(zhǎng)方體表面積計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05android自定義環(huán)形統(tǒng)計(jì)圖動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了android自定義環(huán)形統(tǒng)計(jì)圖動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android拆輪子系列之寫(xiě)驗(yàn)證碼控件的方法
這篇文章主要介紹了Android拆輪子系列之寫(xiě)驗(yàn)證碼控件的方法的, 涉及到Canvas和pint的使用和View的基本用法等相關(guān)知識(shí)點(diǎn),本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2016-10-10Android listview動(dòng)態(tài)加載列表項(xiàng)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android listview動(dòng)態(tài)加載列表項(xiàng)實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android中TextView文本高亮和點(diǎn)擊行為的封裝方法
這篇文章主要介紹了Android中TextView文本高亮和點(diǎn)擊行為的封裝方法,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03