基于语音控制的智能家居系统设计(毕业设计初版)
1、项目组成
1.上位机:安卓手机
2.下位机:STM32单片机(STM32F103C8T6)
3.外接模块:语音识别模块(LD3320),蓝牙模块(BLE CC2541)
4.开发软件:Android Studio 3.1.2,Keil uVision5,Keil uVision4
5.其他实物:LED灯泡2个,直流小电机1个,L298N驱动模块1个,面包板1块,面包板供电模块1个,杜邦线若干
6.下载链接:https://download.csdn.net/download/qq_40093925/11020346
2、参考博客
安卓手机与蓝牙模块联合调试
STM32串口接收字符串并控制LED
感谢以上两位博主的开源项目
3、安卓APP工程(只贴主程序和主布局文件)
3.1、MainActivity.java
package com.cjt.bluetoothscm;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.github.zagum.switchicon.SwitchIconView;
import com.inuker.bluetooth.library.Constants;
import com.inuker.bluetooth.library.connect.options.BleConnectOptions;
import com.inuker.bluetooth.library.connect.response.BleConnectResponse;
import com.inuker.bluetooth.library.connect.response.BleNotifyResponse;
import com.inuker.bluetooth.library.connect.response.BleWriteResponse;
import com.inuker.bluetooth.library.model.BleGattProfile;
import java.io.IOException;
import java.util.UUID;
import static com.inuker.bluetooth.library.Constants.REQUEST_SUCCESS;
/*****************
* 包名:com.cjt.bluetoothscm
* 类名:MainActivity.java
* 时间:2018/9/11 23:28
* 作者:Cao Jiangtao
* 首页:https://1989jiangtao.github.io/index.html
******************/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int REQUEST_CONNECT_DEVICE = 0x100;
TextView mainTitle ;
// 灯组01 ,灯组02 , 电源开关, 风扇开关
SwitchIconView lamp01 , lamp02 , powerSw , fanSw ;
TextView lamp01Name , lamp02Name ,powerName , fanName;
// 蓝牙通信的地址和两个UUID
String MAC = "" ;
UUID serviceUuid , characterUuid ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 运行的时候检查是否打开蓝牙,没有打开就开启蓝牙
if(!MyApp.getBluetoothClient().isBluetoothOpened())
MyApp.getBluetoothClient().openBluetooth();
}
private void initView() {
mainTitle = findViewById(R.id.main_title); //findViewById(R.id.xml文件中对应的id)
lamp01 = findViewById(R.id.sw_lamp_01); //灯1开关
lamp02 = findViewById(R.id.sw_lamp_02); //灯2开关
powerSw = findViewById(R.id.sw_power); //灯开关
fanSw = findViewById(R.id.sw_fan); //风扇开关
lamp01Name = findViewById(R.id.lamp_01_name); //名字
lamp02Name = findViewById(R.id.lamp_02_name);
powerName = findViewById(R.id.power_name);
fanName = findViewById(R.id.fan_name);
// 为按钮设置点击事件
lamp01.setOnClickListener(this);
lamp02.setOnClickListener(this);
powerSw.setOnClickListener(this);
fanSw.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main , menu); // 加载菜单页面
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_scan){
Intent intent = new Intent(MainActivity.this , ScanResultActivity.class);
startActivityForResult(intent , REQUEST_CONNECT_DEVICE);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
// 关闭蓝牙
MyApp.getBluetoothClient().closeBluetooth();
super.onDestroy();
}
@Override
public void onClick(View v) {
// 当蓝牙有连接,并且MAC地址存在,两个UUID都不为空的情况下,点击按钮才有效
// 以下只要有一个条件不满足,就不让点击按钮发送数据
if(!MyApp.getBluetoothClient().isBleSupported()
|| TextUtils.isEmpty(MAC)
|| TextUtils.isEmpty(serviceUuid.toString())
|| TextUtils.isEmpty(characterUuid.toString())){
Toast.makeText(MainActivity.this , "请先检查蓝牙设备与手机是否连接正常",Toast.LENGTH_SHORT).show();
return;
}
switch (v.getId()){
case R.id.sw_lamp_01: // 灯组01
lamp01.switchState();
lamp01Name.setText(lamp01.isIconEnabled() ? "客厅灯开" : "客厅灯关");
writeCmd(MAC , serviceUuid , characterUuid , lamp01.isIconEnabled() ? "L1ON\n" :"L1OFF\n");
break;
case R.id.sw_lamp_02: // 灯组02
lamp02.switchState();
lamp02Name.setText(lamp02.isIconEnabled() ? "房间灯开" : "房间灯关");
writeCmd(MAC , serviceUuid , characterUuid , lamp02.isIconEnabled() ? "L2ON\n" :"L2OFF\n");
break;
case R.id.sw_power: // 电源
powerSw.switchState();
powerName.setText(powerSw.isIconEnabled() ? "电源开" : "电源关");
writeCmd(MAC , serviceUuid , characterUuid , powerSw.isIconEnabled() ? "ON\n" :"OFF\n");
break;