基于语音控制的智能家居控制系统(安卓,蓝牙,语音控制,单片机)
上位机是安卓机,下位机是51,上位机(语音识别出指令如开关灯)与下位机(接蓝牙模块BLE:CC2451)通过蓝牙通信进行控制。
初步设想是语音识别部分用科大讯飞的sdk,然后再找个开源的蓝牙通信Demo来进行二次开发,无奈本人小白,无安卓开发经验,java零基础,虽然论坛上有很多现成的蓝牙控制单片机的例子,但是好像没有语音识别这一部分的,做得很难受。
以下贴出我的进展图、App闪退错误代码,以及半成品Demo(只实现了语音识别部分),希望有大牛指导一下。
开发环境:Android Studio 2.1.3
Gradle版本:com.android.tools.build:gradle:2.1.3
SDK版本:android-sdk_r24.4.1-windows
JDK版本:jdk1.8.0_201
Compile Sdk Version:API 25
Build Tools Version:28.0.3
半成品Demo:(https://download.csdn.net/download/qq_40093925/10975515)
**
AndroidManifest.xml
**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luoyn.speechdemo">
<!--连接网络权限,用于执行云端语音能力 -->
<uses-permission android:name="android.permission.INTERNET" />
<!--获取手机录音机使用权限,听写、识别、语义理解需要用到此权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!--读取网络信息状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--获取当前wifi状态 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序改变网络连接状态 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<!--读取手机信息权限 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--读取联系人权限,上传联系人需要用到此权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!--外存储写权限,构建语法需要用到此权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--外存储读权限,构建语法需要用到此权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--配置权限,用来记录应用配置信息 -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!--手机定位信息,用来为语义等功能提供定位,提供更精准的服务--> <!--定位信息是敏感信息,可通过Setting.setLocationEnable(false)关闭定位请求 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--蓝牙权限-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="智能家居控制面板"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
MainActivitiy.java
package com.example.luoyn.speechdemo;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//显示听写结果
private TextView textView;
//语音听写对象
private SpeechRecognizer speechRecognizer;
//语音听写UI
private RecognizerDialog recognizerDialog;
//是否显示听写UI
private boolean isShowDialog = true;
//缓存
private SharedPreferences sharedPreferences;
//用hashmap存储听写结果
private HashMap<String, String> hashMap = new LinkedHashMap<String, String>();
//引擎类型(云端或本地)
private String mEngineType = null;
//函数返回值
private int ret = 0;
private Toast toast;
//插入部分--------------------------------------------------------------------------------------
public static final int REQUEST_BT_ENABLE_CODE = 200;
public static final String BT_UUID = "00001101-0000-1000-8000-00805F9B34FB";//YahBoom_uuid
private BluetoothAdapter mBluetoothAdapter;//蓝牙适配器
private BlueToothStateReceiver mReceiver;//广播接收器
private ConnectThread mConnectThread; //客户端线程
private AcceptThread mAcceptThread; //服务端线程
private RecyclerView mRecyclerView;
private RvAdapter mRvAdapter;
private RecyclerView mMessageView;
private static MsgAdapter mMessageAdapter;
private EditText inputEt;
private static Handler mHandler = new Handler()