Android蓝牙按键监听

package com.teprinciple.webviewdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class BlueToothActivity extends AppCompatActivity {

    private TextView blueText;
    private RecyclerView recyclerView;
    List<String> mData = new ArrayList<>();
    int currentIndex = 0;//小技巧: 默认等于0 则默认选中第一项 默认等于<0 则默认不选中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blue);
        blueText = (TextView) findViewById(R.id.blueText);
        init();
    }

    private void init() {
        recyclerView = (RecyclerView) findViewById(R.id.ly_recycler_view);
        for (int i = 0; i < 30; i++) {
            mData.add("A-"+i);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(new ItemAdapter());

        //notifyItemChanged() 默认有动画
        //去掉默认刷新动画
        ((DefaultItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new ViewHolder(LayoutInflater.from(BlueToothActivity.this).inflate(R.layout.item_layout,viewGroup,false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
            String text = mData.get(position);
            viewHolder.textView.setText(text);
            if (currentIndex == position){
                viewHolder.textView.setBackgroundColor(Color.RED);
            }else {
                viewHolder.textView.setBackgroundColor(Color.BLUE);
            }
            viewHolder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int lastIndex = currentIndex;
                    currentIndex = position;
                    notifyItemChanged(currentIndex);
                    if (!(lastIndex<0)){
                        notifyItemChanged(lastIndex);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{

            TextView textView;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.text_view);
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        printToast(parseKeyCode(keyCode));
        return true;
    }

    public String parseKeyCode(int keyCode) {
        String ret = "";
        switch (keyCode) {
            case KeyEvent.KEYCODE_POWER:
                // 监控/拦截/屏蔽电源键 这里拦截不了
                ret = "get Key KEYCODE_POWER(KeyCode:" + keyCode + ")";
                break;
            case KeyEvent.KEYCODE_RIGHT_BRACKET:
                // 监控/拦截/屏蔽返回键
                ret = "get Key KEYCODE_RIGHT_BRACKET";
                break;
            case KeyEvent.KEYCODE_MENU:
                // 监控/拦截菜单键
                ret = "get Key KEYCODE_MENU";
                break;
            case KeyEvent.KEYCODE_HOME:
                // 由于Home键为系统键,此处不能捕获
                ret = "get Key KEYCODE_HOME";
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                // 监控/拦截/屏蔽上方向键
                ret = "get Key KEYCODE_DPAD_UP";
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                // 监控/拦截/屏蔽左方向键
                ret = "get Key KEYCODE_DPAD_LEFT";
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                // 监控/拦截/屏蔽右方向键
                ret = "get Key KEYCODE_DPAD_RIGHT";
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                // 监控/拦截/屏蔽下方向键
                ret = "get Key KEYCODE_DPAD_DOWN";
                break;
            case KeyEvent.KEYCODE_DPAD_CENTER:
                // 监控/拦截/屏蔽中方向键
                ret = "get Key KEYCODE_DPAD_CENTER";
                break;
            case KeyEvent.FLAG_KEEP_TOUCH_MODE:
                // 监控/拦截/屏蔽长按
                ret = "get Key FLAG_KEEP_TOUCH_MODE";
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                // 监控/拦截/屏蔽下方向键
                ret = "左(KeyCode:" + keyCode + ")";
                int lastIndexLeft = currentIndex;
                currentIndex = currentIndex - 1;
                recyclerView.getAdapter().notifyItemChanged(currentIndex);
                if (!(lastIndexLeft<0)){
                    recyclerView.getAdapter().notifyItemChanged(lastIndexLeft);
                }
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
                // 监控/拦截/屏蔽中方向键
                ret = "右(KeyCode:" + keyCode + ")";
                int lastIndex = currentIndex;
                currentIndex = currentIndex + 1;
                recyclerView.getAdapter().notifyItemChanged(currentIndex);
                if (!(lastIndex<0)){
                    recyclerView.getAdapter().notifyItemChanged(lastIndex);
                }
                break;
            case KeyEvent.KEYCODE_3:
                ret = "下(KeyCode:" + keyCode + ")";
                break;
            case KeyEvent.KEYCODE_4:
                ret = "上(KeyCode:" + keyCode + ")";
                break;
            case KeyEvent.KEYCODE_ENTER:
                ret = "中(KeyCode:" + keyCode + ")";
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
                ret = "耳机确定中(KeyCode:" + keyCode + ")";
                Toast.makeText(this, "跳转故障记录页面进行填写", Toast.LENGTH_SHORT).show();
                break;
            default:
                ret = "其他: "
                        + keyCode;
                break;
        }
        return ret;
    }

    public void printToast(String str) {
        blueText.setText(str);
    }
}

 

 

页面:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".HomeActivity">

    <TextView
        android:id="@+id/blueText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="00000"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:textSize="20dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ly_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

</RelativeLayout>

 

可监听耳机以及蓝牙拍照器

posted @ 2020-07-19 16:27  改变世界的老十七  阅读(982)  评论(0编辑  收藏  举报