android智能家居在线语音控制

对于android 智能家居项目,如果能实现语音控制,无疑会丰富项目功能,改善用户体验,android语音识别的方法有三种:一是使用intent调用语音识别程序,二 是应用程序自己调用语音识别库,三是使用service调用语音识别程序,本文重点介绍第一种方法,一言以蔽之,其主要原理就是将语音发送到google 云端,然后云端处理,匹配数据,再发送给客户端。

1、需要通过intent传递语音识别的模式:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);需要开启一个语音活动;

2、intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);这是对语音模式和*形式的语音识别;

3、提示用户开始输入语音:

intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音");

4、开始语音识别:

startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);

这里调用startActivityForResult方法,这种方法可以用onActivityResult(int requestCode, int resultCode, Intent data)方法获得请求Activity结束之后的操作。

用户在输入语音数据之后,就是用上述方法获取经google云端处理的语音数据:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                // TODO Auto-generated method stub
                // 回调获取从谷歌得到的数据
                if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
                && resultCode == RESULT_OK) {
                        // 取得语音的字符
                        ArrayList<String> results = data

.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);其中参数RecognizerIntent.EXTRA_RESULTS 的作用就是将字符串返回到一个

ArrayList<String> 的对象中
        for (int i = 0; i < results.size(); i++) {
                resultString += results.get(i);
        }

这段代码的作用就是遍历数组,获取语音字符串;

下面是语音控制打开M0开发板上led灯的程序:

if ((resultString.indexOf("开") > -1)
        | (resultString.indexOf("打") > -1)) {
                if (resultString.indexOf("灯") > -1) {

if (nodeInfo.getLed() == 0) {
                                btn_light.setImageResource(R.drawable.light_on);
                                s = "正在打开电灯";
                                Toast toast = Toast.makeText(getApplicationContext(),
                                s, Toast.LENGTH_SHORT);
                                toast.show();
                                nodeInfo.setLed(1);
                                if (!writeThreadIsCreated) {
                                        WriteThread = new WriteThread();
                                        WriteThread.begin(nodeInfo.getDevNum(),
                                        Cmd.DevCmd.LEDON);
                                } else
                                WriteThread.begin(nodeInfo.getDevNum(),
                                Cmd.DevCmd.LEDON);
                        }

}

以下是语音控制打开M0开发板上风扇的程序:

if (resultString.indexOf("风扇") > -1) {
                s = "正在打开风扇";

Toast.makeText(this, s, 1).show();

if (nodeInfo.getFun() == 0) {

btn_fan.setImageResource(R.drawable.fan_on);

nodeInfo.setFun(1);
                        if (!writeThreadIsCreated) {
                                WriteThread = new WriteThread();
                                WriteThread.begin(nodeInfo.getDevNum(),
                                Cmd.DevCmd.FANON);
                        } else {
                                WriteThread.begin(nodeInfo.getDevNum(),
                                Cmd.DevCmd.FANON);
                        }
                }

}

以下是语音控制打开M0开发板上蜂鸣器的程序

if ((resultString.indexOf("蜂") > -1)
        | (resultString.indexOf("鸣") > -1)) {
                if (nodeInfo.getBeep() == 0) {
                        btn_beep.setImageResource(R.drawable.beep_on);
                        s = "正在打开蜂鸣器";
                        // toast delay 3
                        Toast toast = Toast.makeText(getApplicationContext(),
                        s, Toast.LENGTH_SHORT);
                        // toast show
                        toast.show();

nodeInfo.setBeep(1);
                        if (!writeThreadIsCreated) {
                                WriteThread = new WriteThread();
                                WriteThread.begin(nodeInfo.getDevNum(),
                                Cmd.DevCmd.SPKON);
                        } else
                        WriteThread.begin(nodeInfo.getDevNum(),
                        Cmd.DevCmd.SPKON);
                }
        }

以下是语音控制打开M0开发板上数码管的程序

if (resultString.indexOf("数") > -1) {

if (nodeInfo.getNixietub() == 0) {
                        btn_ne.setImageResource(R.drawable.seg_on);
                        s = "打开数码管";
                        // 弹窗显示延时3秒
                        Toast toast = Toast.makeText(getApplicationContext(),
                        s, Toast.LENGTH_SHORT);
                        // 显示toast信息
                        toast.show();
                        nodeInfo.setNixietub(1);
                        if (!writeThreadIsCreated) {
                                WriteThread = new WriteThread();
                                WriteThread.begin(nodeInfo.getDevNum(),
                                Cmd.DevCmd.NTON);
                        } else
                        WriteThread.begin(nodeInfo.getDevNum(),
                        Cmd.DevCmd.NTON);
                }
        }

上一篇:【leetcode】Sum Root to Leaf Numbers(hard)


下一篇:Python基础--Python简介和入门