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

