当前位置:   article > 正文

智能家居_com.tutk.iotc.monitor

com.tutk.iotc.monitor

课程简介

智能家居是在互联网的影响之下物联化体现。智能家居通过物联网技术将家中的各种设备连接到一起,提供各种控制或者定时控制的功能和手段。与普通家居相比,智能家居不仅具有传统的家庭居住功能,同时还兼备建筑、网络通信、信息家电、设备自动化功能,提供全方位的信息交互功能。

在本课程中一共有两个案例,第一个是智能摄像头,我将演示如何通过自己编写Android应用控制网络摄像头,实现远程监控功能。第二个是蓝牙继电器,通过手机的蓝牙跟智能设备进行通信进而控制开关,实现电器设备的“智能化”。

智能家居发展情况

智能家居起源

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

1、家庭自动化

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

2、家庭网络

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

3、网络家电

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

4、信息家电

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

国内发展现状

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

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

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

2、开创期(2000年-2005年)

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

3、徘徊期(2006-2010年)

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

4、融合演变期(2011-2020年)

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

智能家电

智能灯泡
智能摄像头
智能空调

蓝牙灯泡

什么是蓝牙灯泡

智能家居的一部分,将蓝牙模块和灯泡相结合,通过手机和蓝牙模块进行通讯,控制电灯中的电压板,从而控制灯泡的打开、关闭、点动等操作。

应用场景

住家、卧室、客厅、厨房、浴厕,办公室、会议室、地下 室、汗蒸房、美容院、医院、疗养院等等地方。

优缺点

优点:方便、快捷、能耗低、寿命长、扩展性好
缺点:受距离限制(5-10米),不稳定

基本组成

灯泡

智能家居

蓝牙模块

智能家居

手机,目标:通过手机控制灯泡的打开、关闭、点动操作

基本流程

智能家居

//获取本地蓝牙适配器
BluetoothAdapter mBluetoothAdapter =  BluetoothAdapter.getDefaultAdapter();

//打开手机蓝牙
mBluetoothAdapter .enable();     
//关闭手机蓝牙
mBluetoothAdapter.disable();

//扫描蓝牙设备
mBluetoothAdapter.startDiscovery();
//取消扫描蓝牙设备,减少资源的消耗
mBluetoothAdapter.cancelDiscovery();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

startDiscovery()和cancelDiscovery()必须在工程中注册一个蓝牙广播接受者

//添加蓝牙广播接受者

IntentFilter filter = new IntentFilter();
// 开始扫描的广播
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);    
// 扫描完成的广播
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);   
// 发现一个可用的设备的广播
filter.addAction(BluetoothDevice.ACTION_FOUND);
mBluetoothReceiver = new BluetoothReceiver();
//注册监听
registerReceiver(mBluetoothReceiver, filter);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

蓝牙广播接受者

class BluetoothReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
                //获取蓝牙设备
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

连接设备

public void connectServer(final BluetoothDevice device) {
    new Thread(new Runnable(){
        @Override
        public void run() {
        try {
                    System.out.println(randomUUID.toString());
                    BluetoothSocket clientSocket = device.
            createRfcommSocketToServiceRecord(
        UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
                    clientSocket.connect();
                    out = clientSocket.getOutputStream();
                    System.out.println("连接成功");
                    Looper.prepare();
                   Toast.makeText(BluetoothDemoActivity.this, "连接成功", 0).show();
                   Looper.loop();
        } catch (IOException e) {
                   e.printStackTrace();
        }
        }}).start();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

注意:蓝牙2.1版本不用使用匹配的UUID也可以,但不保证准确性,蓝牙4.0版本需要使用匹配的UUID,为了兼容性都使用匹配的UUID
UUID: 00001101-0000-1000-8000-00805f9b34fb
匹配密码:1518(高版本为了低功耗不用密码验证)

Looper :是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理
Looper.prepare() :在一个线程中运行一个消息循环,通过perpare开启消息循环
Looper.loop() :循环处理消息,直到循环结束为止

打开灯泡

private void openLight() {
        if(out == null) return;
        try {
            // 向服务端写数据
            byte[] b = new byte[5];
            b[0] = (byte) 0x01;
            b[1] = (byte) 0x99;
            b[2] = (byte) 0x10;
            b[3] = (byte) 0x10;
            b[4] = (byte) 0x99;
            out.write(b);
            out.flush();
        } catch (IOException e) {
            Toast.makeText(this, "打开失败", 0).show();
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

注销广播接受者

@Override
protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBluetoothReceiver);
}
  • 1
  • 2
  • 3
  • 4
  • 5

智能摄像头

准备工作

网络摄像头,我这里采用的是 品牌: EasyN/易视眼 型号: TM007 ,产品图片如下。
智能家居

对应SDK

智能家居

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

工作原理

首先当我们将网络摄像头买回来的时候,需要给其设置wifi账号和密码,以让我们的硬件能够连接到物联网云平台。在我们这个案例中使用到的物联网云平台是台湾TUTK公司http://www.tutk.com/推出的IOTC(Internet of Things Cloud 物联网云)平台。摄像头使用的是深圳市普顺达科技有限公司 http://www.easyn.cn/的硬件设备。

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

智能家居

智能家居

当我们的摄像头通上电并设置好网络后会自动连接到IOTC服务器。然后处于等待状态,等待接收来自App端发送的指令。App通过硬件设备的UID、用户名、密码登陆到IOTC服务器,然后发送指令。IOTC获取到App的指令后在转发给设备,然后设备将返回结果(可能是状态数据也可能是摄像机画面)在返回给IOTC,IOTC再将这些结果数据传给App。这样整个就实现了手机和摄像头的互联互动。在这整个过程中IOTC平台是核心部分,也可以看出智能家居其实拼的就是服务平台。

智能家居

代码实现

我们要做的案例效果图如下,总共有两个界面,第一个界面是登录界面,在文本框中输入名称、UID、密码,其中名称是我们自己给自己的摄像头设备起的名称,可以随意写。UID在设备上有,我们直接抄过来,是设备的唯一标识,不可以修改。密码是设备出厂时给了默认的,我们后期可以修改的。为了防止被黑客给黑掉,其实我们只要UID不外漏即可。

智能家居

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

activity_camera.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <com.tutk.IOTC.Monitor
        android:id="@+id/monitor"
        android:layout_width="400dp"
        android:layout_height="320dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="连接状态:" />
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:gravity="bottom" >
        <View
            android:id="@+id/center"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_centerInParent="true"
            android:layout_margin="20dp"
            android:background="#ff0000" />
        <ImageButton
            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" />
        <ImageButton
            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" />
        <ImageButton
            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" />
        <ImageButton
            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" />
    </RelativeLayout>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

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

智能家居

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

MainActivity代码如下:

package com.example.smartcamera;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.testapp.R;

public class MainActivity extends 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/893487?site
推荐阅读
相关标签
  

闽ICP备14008679号