智能家居代码.docx

上传人:b****7 文档编号:11327422 上传时间:2023-02-26 格式:DOCX 页数:51 大小:1.57MB
下载 相关 举报
智能家居代码.docx_第1页
第1页 / 共51页
智能家居代码.docx_第2页
第2页 / 共51页
智能家居代码.docx_第3页
第3页 / 共51页
智能家居代码.docx_第4页
第4页 / 共51页
智能家居代码.docx_第5页
第5页 / 共51页
点击查看更多>>
下载资源
资源描述

智能家居代码.docx

《智能家居代码.docx》由会员分享,可在线阅读,更多相关《智能家居代码.docx(51页珍藏版)》请在冰豆网上搜索。

智能家居代码.docx

智能家居代码

1.课程简介2

2.智能家居发展情况(了解)2

2.1智能家居起源2

2.2国内发展现状3

3.案例-智能摄像头(★★)3

3.1准备工作3

3.2工作原理4

3.3代码实现6

4.案例-智能继电器(★★)18

4.1准备工作18

4.2工作原理18

4.3代码实现19

Android-智能家居

1.课程简介

智能家居是在互联网的影响之下物联化体现。

智能家居通过物联网技术将家中的各种设备连接到一起,提供各种控制或者定时控制的功能和手段。

与普通家居相比,智能家居不仅具有传统的家庭居住功能,同时还兼备建筑、网络通信、信息家电、设备自动化功能,提供全方位的信息交互功能。

在本课程中一共有两个案例,第一个是智能摄像头,我将演示如何通过自己编写Android应用控制网络摄像头,实现远程监控功能。

第二个是蓝牙继电器,通过手机的蓝牙跟智能设备进行通信进而控制开关,实现电器设备的“智能化”。

2.智能家居发展情况(了解)

2.1智能家居起源

智能家居的概念起源很早,但是直到1984年美国联合科技公司(UnitedTechnologiesBuildingSystem)将建筑设备信息化、整合化概念应用于美国康涅狄格州(Connecticut)哈特佛市(Hartford)的CityPlaceBuilding时,才出现了首栋的“智能型建筑”,从此揭开了全世界争相建造智能家居派的序幕。

智能家居从开始到现在主要经历了四个阶段。

家庭自动化

通过一个中央微处理机接受相关电子产品(检测环境变化)的信息,再发送给其他产品。

家庭网络

家庭网络是在家庭范围内将家电(安全系统、照明系统)和广域网相连接的一种新技术。

网络家电

利用数字技术、网络技术及智能控制技术设计改进的新型家电产品,比如网络空调。

信息家电

能够通过网络系统交互信息的家电产品。

2.2国内发展现状

智能家居在我国还是一个新生产业,处于一个导入期与成长期的临界点,我国政府在2013年8月14日发表了关于促进信息消费扩大内需的若干意见,大力发展宽带业务,也为智能家居打下了坚实的基础,加之智能家居市场消费观念还未形成,市场的消费潜力必然是巨大的,产业前景光明。

萌芽期/智能小区期(1994年-1999年)

概念熟悉、产品认知的阶段,还没有出现专业的智能家居生产厂商。

开创期(2000年-2005年)

成立了五十多家智能家居研发生产企业,没有进入国内市场。

徘徊期(2006-2010年)

过分夸大智能家居的功能,行业用户、媒体开始质疑智能家居的实际效果,国内企业转型,国外企业进入(罗格朗、霍尼韦尔)。

融合演变期(2011-2020年)

进入2014年以来,各大厂商已开始密集布局智能家居,经过一年多产业磨合,2015年合作企业已普遍进入到出成果时刻,智能家居新品已经层出不穷的出现了。

3.案例-智能摄像头(★★)

3.1准备工作

网络摄像头

我这里采用的是品牌:

EasyN/易视眼型号:

TM007,产品图片如下。

 

对应SDK

我们开发的软件是基于第三方智能网平台的,因此需要使用到第三方的SDK。

3.2工作原理

首先当我们将网络摄像头买回来的时候,需要给其设置wifi账号和密码,以让我们的硬件能够连接到物联网云平台。

在我们这个案例中使用到的物联网云平台是台湾TUTK公司ofThingsCloud物联网云)平台。

摄像头使用的是深圳市普顺达科技有限公司

这两家公司的网页首页截图如下:

当我们的摄像头通上电并设置好网络后会自动连接到IOTC服务器。

然后处于等待状态,等待接收来自App端发送的指令。

App通过硬件设备的UID、用户名、密码登陆到IOTC服务器,然后发送指令。

IOTC获取到App的指令后在转发给设备,然后设备将返回结果(可能是状态数据也可能是摄像机画面)在返回给IOTC,IOTC再将这些结果数据传给App。

这样整个就实现了手机和摄像头的互联互动。

在这整个过程中IOTC平台是核心部分,也可以看出智能家居其实拼的就是服务平台。

服务大概流程图如下:

3.3代码实现

我们要做的案例效果图如下,总共有两个界面,第一个界面是登录界面,在文本框中输入名称、UID、密码,其中名称是我们自己给自己的摄像头设备起的名称,可以随意写。

UID在设备上有,我们直接抄过来,是设备的唯一标识,不可以修改。

密码是设备出厂时给了默认的,我们后期可以修改的。

为了防止被黑客给黑掉,其实我们只要UID不外漏即可。

编写布局文件布局文件共两个,第一个登录界面的布局太简单了,就不给出来了。

值给出第二个布局文件。

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

orientation="vertical">

android:

id="@+id/monitor"

android:

layout_width="400dp"

android:

layout_height="320dp"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

gravity="center_horizontal"

android:

orientation="horizontal">

android:

id="@+id/tv_state"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="连接状态:

"/>

android:

layout_width="match_parent"

android:

layout_height="200dp"

android:

layout_gravity="center"

android:

gravity="bottom">

android:

id="@+id/center"

android:

layout_width="10dp"

android:

layout_height="10dp"

android:

layout_centerInParent="true"

android:

layout_margin="20dp"

android:

background="#ff0000"/>

android:

id="@+id/ib_left"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_centerVertical="true"

android:

layout_toLeftOf="@id/center"

android:

background="@drawable/left"

android:

contentDescription="@null"/>

android:

id="@+id/ib_right"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_centerVertical="true"

android:

layout_toRightOf="@id/center"

android:

background="@drawable/right"

android:

contentDescription="@null"/>

android:

id="@+id/ib_bottom"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@id/center"

android:

layout_centerHorizontal="true"

android:

background="@drawable/bottom"

android:

contentDescription="@null"/>

android:

id="@+id/ib_top"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_above="@id/center"

android:

layout_centerHorizontal="true"

android:

background="@drawable/top"

android:

contentDescription="@null"/>

添加类库如下图所示,考虑到类库的保密性在该文档中就不再给出下载地址。

核心代码总共有两个Activity,分别是MainActivity和CameraPlayActivity

MainActivity代码如下:

packagecom.example.smartcamera;

importandroid.support.v7.app.ActionBarActivity;

importandroid.text.TextUtils;

importandroid.view.View;

importandroid.widget.EditText;

importandroid.widget.Toast;

importandroid.content.Intent;

importandroid.os.Bundle;

publicclassMainActivityextendsActionBarActivity{

publicstaticfinalStringNAME="name";

publicstaticfinalStringPSW="psw";

publicstaticfinalStringUID="uid";

privateEditTextet_name;

privateEditTextet_pwd;

privateEditTextet_uid;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

/**

*初始化子控件

*/

privatevoidinitView(){

et_name=(EditText)findViewById(R.id.et_devname);

et_pwd=(EditText)findViewById(R.id.et_psw);

et_uid=(EditText)findViewById(R.id.et_uid);

}

/**

*点击Button开始链接

*

*@paramview

*/

publicvoidconnect(Viewview){

Stringname=et_name.getText().toString();

Stringpwd=et_pwd.getText().toString();

Stringuid=et_uid.getText().toString();

if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)||TextUtils.isEmpty(uid)){

Toast.makeText(this,"数据不能为空。

",Toast.LENGTH_SHORT).show();

return;

}

Intentintent=newIntent(this,CameraPlayActivity.class);

intent.putExtra(NAME,name);

intent.putExtra(PSW,pwd);

intent.putExtra(UID,uid);

//跳转到播放页面

startActivity(intent);

}

}

CameraPlayActivity代码如下:

packagecom.example.smartcamera;

importcom.tutk.IOTC.AVIOCTRLDEFs;

importcom.tutk.IOTC.AVIOCTRLDEFs.SMsgAVIoctrlPtzCmd;

importcom.tutk.IOTC.Camera;

importcom.tutk.IOTC.IRegisterIOTCListener;

importcom.tutk.IOTC.Monitor;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.graphics.Bitmap;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.ImageButton;

importandroid.widget.TextView;

publicclassCameraPlayActivityextendsActivityimplementsOnClickListener,IRegisterIOTCListener{

privateMonitormonitor;

privateImageButtonib_left;

privateImageButtonib_right;

privateImageButtonib_top;

privateImageButtonib_bottom;

privateTextViewtv_state;

privateStringmName;

privateStringmPsw;

privateStringmUID;

privateCameramCamera;

Handlerhandler=newHandler(){

@Override

publicvoidhandleMessage(android.os.Messagemsg){

switch(msg.what){

//针对不同的连接状态做不同的处理

caseCamera.CONNECTION_STATE_CONNECT_FAILED:

tv_state.setText("连接失败");

break;

//如果是已经连接成功

caseCamera.CONNECTION_STATE_CONNECTED:

tv_state.setText("已连接");

//调用play()方法,完成Camera和Monitor的绑定,最终将画面显示出来

play();

break;

caseCamera.CONNECTION_STATE_CONNECTING:

tv_state.setText("连接中...");

break;

caseCamera.CONNECTION_STATE_DISCONNECTED:

tv_state.setText("未连接");

break;

caseCamera.CONNECTION_STATE_TIMEOUT:

tv_state.setText("连接超时");

break;

caseCamera.CONNECTION_STATE_UNKNOWN_DEVICE:

tv_state.setText("未知设备");

break;

caseCamera.CONNECTION_STATE_UNSUPPORTED:

tv_state.setText("不支持的设备");

break;

caseCamera.CONNECTION_STATE_WRONG_PASSWORD:

tv_state.setText("密码不正确");

break;

caseCamera.CONNECTION_STATE_NONE:

tv_state.setText("未知错误");

break;

default:

tv_state.setText("未知"+msg.what);

break;

}

};

};

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_camera);

//初始化控件

initView();

//初始化数据

initData();

//开始连接网络

connectNet();

}

/**

*显示画面的核心方法

*/

protectedvoidplay(){

/**

*如果条件满足则

*1将界面monitor和Camera进行绑定

*2开始播放Camera画面

*

*/

if(mCamera!

=null&&mCamera.isChannelConnected(Camera.DEFAULT_AV_CHANNEL)){

monitor.attachCamera(mCamera,Camera.DEFAULT_AV_CHANNEL);

mCamera.startShow(Camera.DEFAULT_AV_CHANNEL,true);

}

}

privatevoidconnectNet(){

//初始化摄像头内部加载c语言库

Camera.init();

//新创建一个Camera实例不是Android中的Camera而是com.tutk.IOTC.Camera.Camera

mCamera=newCamera();

/**

*给Camera注册IOTC监听

*IOTCInternetofThingsCloud物联网云平台

*该平台是台湾TUTK公司推出,有偿使用,因此SDK不开源

*/

mCamera.registerIOTCListener(this);

/**

*连接到IOTC云平台

*连接传入的是UID

*每个硬件设备出厂时都有唯一的UID编号,并且出厂时已经将该编号注册到IOTC平台服务器

*硬件在连接服务器的时候需要带着自己的身边标识(就是UID)

*/

mCamera.connect(mUID);

/**

*仅仅让硬件连接网络还是不够了,作为终端用户我们想看到云平台上的摄像头画面那么还需要用户

*通过用户名和密码登陆进行身份验证

*

*@parameterCamera.DEFAULT_AV_CHANNEL

*我们可以把一个摄像头硬件理解为一个电视机,那么电视机可以有多个频道,这里我们使用默认频道即可

*@parametermName用户名

*@parametermPsw密码

*

*当开始连接的时候,由于之前注册了Camera监听,因此接收到的数据会以回调的形式传到形参中

*,因此这个时候我们就去IRegisterIOTCListener的回调方法中等数据就行了

*/

mCamera.start(Camera.DEFAULT_AV_CHANNEL,mName,mPsw);

}

/**

*从上一个Activity中获取用户数据

*/

privatevoidinitData(){

Intentintent=getIntent();

mName=intent.getStringExtra(MainActivity.NAME);

mPsw=intent.getStringExtra(MainActivity.PSW);

mUID=intent.getStringExtra(MainActivity.UID);

}

/**

*当前类已经实现了OnClickListener接口因此绑定点击事件只需要传递this即可

*/

privatevoidinitView(){

monitor=(Monitor)findViewById(R.id.monitor);

ib_left=(ImageButton)findViewById(R.id.ib_left);

ib_right=(ImageButton)findViewById(R.id.ib_right);

ib_top=(ImageButton)findViewById(R.id.ib_top)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 商业计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1