���������� ������� ���� �� ������� ��������� �� �� ������� ��������
����� ������ �������� ������ ������ ������� ��������� �������� �����
������ 1. ������������ ...................................................................................... 3 2. ����� �� �� �������� ........................................................................... 5 3. ���������� .......................................................................................... � 4. ���������� ��� ������ � ������� ................................................... � 5. ������ ��� �������� � ������� ..................................................... � 5.1. ������������������� ...................................... ...................... ........................................ ................................. ......... � 5.2. ���� ............................................................................................... 13 �. ������������ .................................................................................... 20 �. ����� �� �� ��������.......................................... ....................... ........................................... ............................... ....... 21 �. ������������ ....................................................................................... 22
Práctica 3 – Sistema Android para para control control de de dispositivo dispositivo domótico domótico por Bluetooth
2
�� ������������ �� ���� �������� ��� ���� ��������� �� �� ������ �� �� �������� � ���� ���� ����� � ������� ��� ����� �� ��������� ������ ����� �� ������� � �������� �� ��������� �� ����� �������. •
•
•
��������� �������� � �� �� �������� �� �������� ������� �� ����������� ��� ��������, ��������� ��������� �� ������� ����������, ���������, ��������� � ������������, � ��� ������ ����� ���������� ��� ����� �� ����� ���������� � ���������� �� ������������, ��������� � ������������, � ���� ������� ���� �� ������ ���������, ����� ������ � ����� ��� �����. �� ������ ������� ���� �� ����������� �� �� ���������� �� �� ������ ����������� �� �� ������� ������� . �������� �� ��� ���������� �� �������� �����, ������ �� ��� ����� ��� �� ���������������� � �� ������� �� ����������, �������� ���� ��������� �� ��� �� �� ����������� �� ��������� ������������������. �������� �� �� ������� ��������� ������ �� �� ������ ����� �������� ������������� ���� ������������ �������.
�� �������� �������� �� �� ������� ��� ���� ��������� ��� ��� ������ ��� ����� � ��� �� A������. �� ����� ����� �� �� ��� �� ������� �� ������� �� ����� � ����� ������ ���� � �� ����������. �� ������ �� A������ �� �� ��������� �� ������ ��� ������� ���� ��� ��� ���� � �� ���������� �� ��������� ��������� ��� ����� � �� ���� ������������� �� ��� ����. �� �������� ��� ����� ��������� ������ ������ ���� � ���� ��� ������ ���������� � �� ���� �� ��������� ��� ��� ������ B�������� �� ��� ��������� �� ���� ���� �����������. �� �������������� �� �� ���������, ���� �������� �� ���������� ��� �������� �� ����� ���� �������� ��� ������������ B�������� ��� �� ���������� �������. �� �� ��������� ��� ����������� A������ ������� �� B�������� ������� ������� ���������� �� �� �������������. ��� ��� ����� ���� ���������� ��� ������������ � �� ��������� �� ��� ��� B�2 (B�������� ����� �� A������), �������� �������� �������� �� ������� �� �� �������� �� ��� � �� �������� �� ����������� ����� ��� �� ����� �� A������. ������������ ������� ��� ������ �� ��� ���� ��� ��� ���������, ���� ������������� � ����� �� ���������/�������. ����� ������� ��������� �� ����� ����� ����� ��������� � �������� ��� ��������� �� �� ��������, �� ����� ������� ���� ������ �������� ������ � �������� ���� �������� �� �� ����� ����� ��������� � �� ����� A������. �� ����� �� ���������/������� ��� ������� ���� ������� ���� � �� ��� � ������ ���� � �� ���. ���� ��� �� ����� � ������ � �������� ��� ���������, ������ ���� ������ �� ������ �� ������� �������� �� A������, �� ��� ������� �� ������� ��� �������� ��� �������� �� ����� �� A������ � ��� ����� �� ������ �� ���� ��������. ������� �� ������ �������� �� �������� �� �� ���� ����� �������� ��������� ���� �� ������ ���� ����� ���� ����� ��� ��������� ���� ��� �������� �� ��������.
Práctica 3 – Sistema Android para para control control de de dispositivo dispositivo domótico domótico por Bluetooth
3
D������ �� ����������� �� ��� ������ ��� ����������� ��� ������ ������������ ���� ��� ��������������.
Práctica 3 – Sistema Android para para control control de de dispositivo dispositivo domótico domótico por Bluetooth
4
�� ����� �� �� �������� ���� �� ��� ����������� ���� �� ������������� �� ���� ����� � ������� �� �����������
�������� �� �� �������� 2. ���� �� �������� ��� �������� ��������� �� �� �������� 2. ���� �� ��������� �� ������� ��� �������� 2 ���� ��� ������� �� ����� �� 50 ������������. ���� �� �������� �� �������� ��� �� ��������� ��� �� ����� �� ��� ��������� � �� ����� @11 (�������� ��� 1). ���� �� �������� �� �������� ���� �� ������������ ��� �� ����� �� A������, � �� ����� �������� ���������, ��� ������� �� ��������� ���� ������ ��� ����� �� �������� ���� ��������� �� ��������� � ������� �� ��� ����� �� �� �����. ���� ���� E� �������� ������ ������ @11 � �� ���� �� 5 �������� @10, �� ���� �� 5 �������� @21 � �� ���� �� 5 �������� @20. ���� ���� E� �������� ���� �� ����������� �� ������� �� ���������� �� ������� � ������ �� ��� ���� �� �����. A�����, �������� �� ����� ��� ������ �� ����� ������� �� ��������� �� �� ���� �� �����. ���� ��� � ��������� ������� ������ �� �� �������� ��� ���������� ���������/������� ���������/������� �� ���������. C����� �� ����� ��� �� ��� ������ �� ���� �������� �� ������� ��������������� �� �������. C��� ��� ��������� �� ���������: � ���1: ��� ��� �����. � ���2: ��� ��� �����. � ���3: ��� ��� ����������. � ���4: ��� ��� ����. A��� ������� �������� ��� ������� ��� �������� ��� �������� �� A������.
Práctica 3 – Sistema Android para para control control de de dispositivo dispositivo domótico domótico por Bluetooth
5
�� ������� ��� ��������� ���� ��������� �� ��� ������ ��� ����� �� �� �������� 2�B. ���� � � ��������) �� �� �� ����� �� �� ����������� �� ������ �� �����������, ������� �� �� �������� �� ����� �� �� ������� ��� ������ �� ����� �� �� ����������� ����� �� ����� A������ �� �����. � ����. ��� ��� �������� ��������� �� ��� ������� ���� �� ����� ���� � ���� �����. �������� �� ��� �� ����� �� ������ �� ����������� ����� ������� �� ���������� ��������� �� ���� ������������� �� ��� ����� ��� �� ������ �� �� ����� ��� ���� ������������� ���� ����� ���������� � �������� ����� �� ��������. ������� ����� ������� �� ����� ���� ������ �� ������� �������� �������� � ����� �� ���� ��� ��� � ��� �� ���� ������������� �������, �� ��� ���� �� �������� �� ����� ��� ������� ���� �� ����� ��� �� �������� ��� ��������� �� �� ���� ��� � ���.
Práctica 3 – Sistema Android para para control control de de dispositivo dispositivo domótico domótico por Bluetooth
6
�� ���������� D������ �� ���� ��� ���������� ��� �� ��� ��������� ���� ���� �������� ���������� � �� ����������� �� �������, �� ��� �� ���������� �� A������ �� ���� �������� ���� � ���. ��� ��� ��������� ������� ���������� � �������� �� �� ������ �� ��������������� �� A������ �� ������ ��� ��������� ���� ��� ��������� ��� ������� A������ � ������������� ���� �� ��������� ���� ��� ���������� ��������� �� A������. ��� ������ �� ��������� �� �������� �� A������ �� ������� ���� ����� ������ ������� �������� �� ���������� �� ���� �� ��������. �� �� �������� �� �������� �� ������� �� .��� (���������� �� A������) ��� ����� ���� ��� ����������� �� ����������� ����� � ����������.
����� A ����� ����������� �� �� ��������� ��� ��/�������� �� ��� ����������� ������� �� ���������� ������������ �������� �� ��� ������� ��� ������� ��� ������� ������� ��� �������� ����� ������� � �� ��� ������� ��� ����������� �����.
�� ���������� ��� ������ � ������� C��� �� ����� ��������� ����� �� ���������� ���� �� ���������� �� �� �������� �� A������ ������ �� ������������ �� ����, ��� �� ��� ��������� �������� ��� �������� .����.
�������� ���� � ����.���� � B��������C���������.���� �� �� ��������� �������� ������������ ��� ��������� �� ������.
��� ���� ���� ������������� �� ����� ����� ������� ��������, �������, ������, ���� �� �������� ��� �� �������� ���� � �������� ����� �� ����� ��� ��������� �������� ���� �����������, ���� ���� ���� �� ����� �������� �� ������� ���, ���������� ��� �������� ������ �� ������ �� ���/��������, �� ������ ����� ����� �������� ��� �� ������ ��� �������� � ����� �������, ������ ����
D������ ����������� �� �������� ������� �� ������� �������� ���� ���� ������ �� �� ������� ���/������ ��������� ������� ������� ��� �� ���� �������� �������� �������� �������� ��������� ���� �������, ��������, � ����� �� �� ������ ��� ��� ��������� �� ������������� � ����� �������� �� ��� ���������.
Práctica 3 – Sistema Android para para control control de de dispositivo dispositivo domótico domótico por Bluetooth
7
������ -
������� ���������� ��������� �� �� ��������� � ����� ������ �� �� ���� ��������� �� ����� �������� . ��� ��� ������� ���� ����� � ������� �� ��������� �� ��� ��� B�2 (����� �� A������ B��������) � ��������� ��� ���� �� ����� �������� �� ����� �� ��������� ��� ������ ��� �� ����� �������� �� �� ������������.
(A��� 2 ��������, ��� �� ����� �� ��������� �� B�2, � ���� ������� �� ����������� ��� �� ������ ����� � �����)
��� ������ �� ������� ������������, ���� ������� �� �� ������� ����������� �� �� �������� ���� A������ �� ��� ����� �� ������������� ���������, ����� ������� ��� � �������� ��� ����� � ���� �� ��������:
- �� ��� ��� ����� (������� ����� �� ���� �� ����� �������� �� �� ����������� A������, �� ���� ���� �� 10 �� ��������� � A������ 2.3.
- ������ ����������, �����, ������� �� ��������, ���...
Práctica 3 – Sistema Android para para control control de de dispositivo dispositivo domótico domótico por Bluetooth
8
�� ������ ��� �������� � ������� �� ���� �������� ����� � �� ������ ������� ������ ��������� ��� ������, ����������. ������� ����� ��� ������� ���� ������������� ���� �� �� ����� ������������������� . ����� ����� ���� ���� ����������� ����� ��� ������ ������������ ��� �� B������� �� �� ����� ������������������� � ��� �� ����� ���� ����� ����� ��� ����� ���� �������� ���� ��������� ��� �������� �� �� ����� ������������������� .
���� �������������������
// ������� � ��������� ��������� package com.domotica; com .domotica; import import import import
java.io.DataInputStream; java.io.DataOutputStream; java.io.IOException; java.util.UUID;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket;
// A��� ���������� ������� ������� ����� B��������C��������� B��������C��������� ������� ���� ���������� ���������� ���� ���� ����� ��� �� ����� ��������. public class clas s BluetoothConnection BluetoothCo nnection { public static final int BLUETOOTH_NOT_ B LUETOOTH_NOT_AVAILABLE AVAILABLE = 1; public static final int BLUETOOTH_NOT_ B LUETOOTH_NOT_CONNECTED CONNECTED = 2; public static final int BLUETOOTH_SOCK B LUETOOTH_SOCKET_CREATION_ ET_CREATION_ERROR ERROR = 3; public static final int BLUETOOTH_CONN B LUETOOTH_CONNECTION_ERROR ECTION_ERROR = 4; public static final int BLUETOOTH_CREA B LUETOOTH_CREATION_SUCCESS TION_SUCCESS = 5; public static final int BLUETOOTH_OUTP B LUETOOTH_OUTPUTSTREAM_FAI UTSTREAM_FAIL L = 6; public static final int BLUETOOTH_SEND B LUETOOTH_SEND_FAIL _FAIL = 7; public static final int BLUETOOTH_SEND B LUETOOTH_SEND_SUCCESS _SUCCESS = 8; 8; public static final String Strin g BLUETOOTH_INPUTSTREAM_F BLUETOOTH_I NPUTSTREAM_FAIL= AIL= "Error_DataInputStream"; public static final String Strin g BLUETOOTH_RECIEVE_FAIL= BLUETOOTH_R ECIEVE_FAIL= "Error_Recieve";
// ����� ��������� ��������� ��� ���� �� �������� �������� ��� ������ ������ ��� ���� ���� �� ����������� ����������� ��� ������, �� ������ ���� ����� ����� �� ��������/������ � ��� ������� �� ������� � ������ ��� ����� ��� ���������� �� ��� �������� ��� ���� � ������ �� �� ������. private BluetoothAdapter Bluet oothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); private String Strin g mac; private BluetoothDevice Bluet oothDevice device; d evice; private BluetoothSocket Bluet oothSocket socket_device; s ocket_device; private DataOutputStream DataO utputStream tmpOut;
Práctica 3 – Sistema Android para para control control de de dispositivo dispositivo domótico domótico por Bluetooth
9
private DataInputStre DataInputStream am tmpIn; public BluetoothConnectio Blueto othConnection(String n(String mac){ mac) { this.mac=mac; }
// E� ���� ������� �� ��� �� ���� �� ������� ������� ����������� �� ������ B�������� B�������� A������ �� ������� ��������� ��� B��������, ����������� �� ������� �� B�������� �������� � ��, ��� ��� ����� ���� ����������� �� ��� ��� �� ������� � ������ �������� �� ������� B�2 (����� �� A������ B��������) � �� �������� ������������� ���� ��� ����� �� ������ ��� �� ����������� B�2. ��� ������ �� ������ ������������� �� ����� ���� �� ��� �� B�2 �� ������� �� �������� �� ����� ��������� � ������� ����������� A������. E� ���� �� ����� ������� ����������� �� �� ���� �������� � ��, ���������� �� � ��� ���� �� ����� ��� �������� �������� ���� ������� �������. �������. public Integer Intege r doConnect() doConnect( ) { //INICIALIZACION mBtAdapter = BluetoothAd BluetoothAdapter.getDefa apter.getDefaultAdapter() ultAdapter(); ; if (mBtAdapter == null) { return BLUETOOTH_NOT_AVAILABLE; //Devuelve FALSO en caso de no estar disponible el Bluetooth } if (!mBtAdapter.isEnabled()) { return BLUETOOTH_NOT_CONNECTED; } //OBTENIENDO //OBTENIEND O DISPOSITIVO this.device=mBtAdapter.getRemoteDevice(this.mac); //CREANDO SOCKET try { this.socket_device this.socket_ device = this.device.createRfcommSocketToServiceRecord(UUID.fr omString("00001101-0000-1000-8000-00805F9B34FB")); } catch (IOException e) { return BLUETOOTH_SOCKET_CREATION_ERROR; } //CREANDO CONEXION try { this.socket_device.connect(); } catch (IOException e) { try { this.socket_device.close(); } catch (IOException e2) { return BLUETOOTH_CONNECTION_ERROR; } } return BLUETOOTH_CREATION_SUCCESS; }
// E��� ������� �� ������� ������ ��� ����� �� �� ������������ ����� ��� 2 ������������, �� ����� �� �����. ������� ������� ��� �������� �� ������ � �� ������: tmpOut = new DataOutputStream(this.socket_device.getOutputStream());
��� ������� ���� �������� ��� ����� ��� �������� ������ �� �����������. E� ����� ����� ������ �������� �� ������ ������, �� �� ������ �� ������� � ������ �� ����� �� ���� ������� �� ���� ������� (�������� ��� �� ���������� �� B�2) � ���� ��� ����� ������ �� ��� ��� �� ��� ������ ��� �� ������� ����� ��� ������ ��� ����� ������ �� ���� ���� ������. Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 10
public Integer Intege r sendInfo(String sendInfo(S tring message) message ) { byte[] buffer; buffe r; try { tmpOut = new DataOutputStream (this.socket_device.getOutputStream()); } catch (IOException e) { return BLUETOOTH_OUTPUTSTREAM_FAIL; } try { buffer = (message (m essage + '\r').getBytes() '\r' ).getBytes(); ; tmpOut.write(buffer); tmpOut.flush(); } catch (IOException e) { return BLUETOOTH_SEND_FAIL; } return BLUETOOTH_SEND_SUCCESS; }
// E��� ������� �� ������� ������ ��� ����� �� �� ������������ ����� ��� 2 ������������, �� ������������ �� �����, ����� � ��� ��� �������� � �� ��������. ������� ������� ��� �������� �� ������ tmpIn = new DataInputSt DataInputStream(this.so ream(this.socket_device cket_device.getInputStr .getInputStream()); eam());
E� ���� ���� �� ������ �� ������ ��� �� ������ �� ���� �� ������� �� ����� (�����). ��� ��� ����� ���� ������� �� ����� ���� �� ������� ���� ��� ���� ���� �������� ��� �� ����� ��� �� ������� ����B��� ��� ������ ��� ����� ������ ����� ����� ������������ ��� �� �������� �������� '#�. A��� �� ����� ������� ��� ������� �� � ������� ���� ��� ��������� �� ������ �� ���������� ��� �������, ���� ����� �� ���� ��� ������ ������� ������������� ������� ������� ���������� ������ ��� ��� ���� � ��� ����� �� ������� ������� ������ ��� �� ��� �����. public String receiveInfo receiveInfo(){ (){ try { tmpIn = new DataInputStream (this.socket_device.getInputStream()); } catch (IOException e) { return "BLUETOOTH_INPUTSTREAM_FAIL"; } try { char readChar=(char)tmpIn.readByte(); while(readChar!='#'){ readChar=(char)tmpIn.readByte(); } Integer cBytes= Integer.parseInt("" Integer.pa rseInt("" + (char)tmpIn. (char)tmpIn.readByte()) readByte()); ; byte buffer[] buffer [] = new byte[cBytes]; byte [cBytes]; tmpIn.read(buffer,0,cBytes); return new String(buffer); } catch (IOException e) { return "BLUETOOTH_RECIEVE_FAIL"; } }
// A��� ����������� �� ��� ������� ��� ����������� �� ������ ��� �� ������� (�������� ���������� ���� �������) ��������� �� ������ ��� �� ������ ��� �����.
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 11
public static String showResult(I showResult(Integer nteger result) { switch (result){ case BLUETOOTH_NOT_AVAILABLE:{ return ("BLUETOOTH_NOT_AVAILABLE");} case BLUETOOTH_NOT_CONNECTED:{ return ("BLUETOOTH_NOT_CONNECTED");} case BLUETOOTH_SOCKET_CREATION_ERROR:{ return ("BLUETOOTH_SOCKET_CREATION_ERROR");} case BLUETOOTH_CONNECTION_ERROR:{ return ("BLUETOOTH_CONNECTION_ERROR");} case BLUETOOTH_CREATION_SUCCESS:{ return ("BLUETOOTH_CREATION_SUCCESS");} case BLUETOOTH_OUTPUTSTREAM_FAIL:{ return ("BLUETOOTH_OUTPUTSTREAM_FAIL");} case BLUETOOTH_SEND_FAIL:{ return ("BLUETOOTH_SEND_FAIL");} case BLUETOOTH_SEND_SUCCESS:{ return ("BLUETOOTH_SEND_SUCCESS");} default: {return ("Not understood!");} } } }
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 12
���� ����
�� �������� �������� �������� � ��������� ��������� package com.domotic com.domotica; a;
import import import import import import import import import import import import import import import
android.app.Activity; android.bluetooth.BluetoothAdapter; android.bluetooth.BluetoothDevice; android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.content.IntentFilter; android.content.pm.ActivityInfo; android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.ImageView; android.widget.TextView; android.widget.Toast;
public class clas s Main extends extend s Activity implements i mplements OnClickListener OnC lickListener{ { private static stati c final int REQUEST_ENABL REQUEST_ENABLE_BT E_BT = 0; private private private private private private private private private
Button Butto n button_scaner; button_sca ner; Strin g statusBotonTodo="ENCEND String statusBoto nTodo="ENCENDIDO"; IDO"; Image View botonTodo; ImageView botonTo do; Image View bulb1Image; ImageView bulb1Im age; Image View bulb2Image; ImageView bulb2Im age; Image View bulb3Image; ImageView bulb3Im age; Image View bulb4Image; ImageView bulb4Im age; Image View bulb5Image; ImageView bulb5Im age; Image View fanImage; ImageView fanImag e;
private ImageView Image View bluetoothIndicator; bluetoo thIndicator; private TextView TextV iew bluetoothStatus; bluetoot hStatus; private View arduinoLayou arduinoLayout; t; private Bluet BluetoothAdapter oothAdapter mBtAdapter; private BluetoothConnecti Bluet oothConnection on btC; private Strin String g macArduinoBT2=""; macArduino BT2=""; private int[] boardStatus = {0,0,0,0,0,0}; {0,0,0,0,0 ,0}; private final BroadcastRe BroadcastReceiver ceiver mReceiver mRecei ver = new BroadcastReceiver() {
// A��� �� ����� ����� ������������ �� ������ ������ ���� ��������� �� ����������� ����������� � ���������� �� ������ � �� �������� ��� A�D����B�2 ��������� �� ��� ���� ����� ��������� ��� ��������. D������ ������������ ������������ ����������� �� ����������� ����������� ���������� � �� �� ������� �� ��������� � ����� ������� ������� �� �� ������ ������ ��� ��������� ��� ����������� ����������� � �� ����� �����, �����, �� ����� ��� �������� ��� ����� ��� ��� ���������, ���� �������������, ���� ������������ ���������� �� ������� ���� ��������� ��� �� ������ �� ��� ��������� �� �� ����� � ��� ����� ���� ������� �� ����� � ���� ��� ��������. @Override public void onReceive(Co onReceive(Context ntext arg0, Intent I ntent arg1) { String action = arg1.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { //Detecta evento de descubrimiento de dispositivo.
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 13
BluetoothDev ice device = arg1.getParc BluetoothDevice arg1.getParcelableExtra elableExtra (BluetoothDevice.EXTRA_DEVICE); if (device.getName().contains("ARDUINOBT2")==true) macArduinoBT2=device.get macArduinoB T2=device.getAddress(); Address(); Toast.makeText(getApplicationContext(), device.getName() device.getNa me() + " " + device.getAddress(), device.getAddress(), Toast.LENGTH_LONG).show(); }else if (BluetoothAdapter.ACTIO (BluetoothAd apter.ACTION_DISCOVERY_ N_DISCOVERY_FINISHED.eq FINISHED.equals(action) uals(action)) ) { //Detecta evento de fin de búsqueda de dispositivos. Toast toast = Toast.makeText(getApplicationContext(), "Fin de búsqueda", Toast.LENGTH_SHORT); Toast.LENGTH_SHORT); toast.show(); button_scaner.setText(ge button_scan er.setText(getResources() tResources().getString(R. .getString(R.string.scan) string.scan)); ); if (macArduinoB (macArduinoBT2!="") T2!="") if (tryConnectArduino()==true){ arduinoLayout.setVisibility(0); //Hacemos visible el layout del comando. String status= sendCommand("@ST"); //preguntamos //pregunta mos por el estado de los elementos de la placa for (int i = 0; i
// ��� ������� ��������� ��������� � �������� ��� ��������� � �� �������� �������� �� ������������� � �� ������ �� ������� ����� �����. C�������� �� ������ ��� ��������� ���� ����� ���������� � ��� ������� ����������. /** Called when the activity is first created. */ @Override public void onCreate(Bun onCreate(Bundle dle savedInstanceState) savedIns tanceState) { super.onCreate(savedInstanceState); this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) ; setContentView(R.layout.main); this.botonTodo = (ImageView)f this.botonTodo (ImageView)findViewById indViewById(R.id.botonT (R.id.botonTodo); odo); this.botonTodo.setOnClickListener(this); this.bulb1Image = (ImageView)findViewById(R.id.bulb1); this.bulb1Image.setOnClickListener(this); this.bulb2Image = (ImageView)findViewById(R.id.bulb2); this.bulb2Image.setOnClickListener(this); this.bulb3Image = (ImageView)findViewById(R.id.bulb3); this.bulb3Image.setOnClickListener(this); this.bulb4Image = (ImageView)findViewById(R.id.bulb4); this.bulb4Image.setOnClickListener(this); this.bulb5Image = (ImageView)findViewById(R.id.bulb5); this.bulb5Image.setOnClickListener(this); this.fanImage = (ImageView)findViewById(R.id.fan); this.fanImage.setOnClickListener(this);
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 14
this.button_scaner = (Button)find this.button_scaner (Button)findViewById(R. ViewById(R.id.button1); id.button1); this.button_scaner.setOnClickListener(this); this.bluetoothIndicator = (ImageView)findViewById (ImageView)f indViewById(R.id.btIndi (R.id.btIndicator); cator); this.bluetoothStatus this.blueto othStatus = (TextView)findViewById(R.id.connectedStatus); this.arduinoLayout=
(View)findViewById(R.id.linearLayout2);
//Comprueba la conectividad del bluetooth para actualizar el icono. mBtAdapter = BluetoothAda BluetoothAdapter.getDefau pter.getDefaultAdapter(); ltAdapter(); if ((mBtAdapter == null) || (!mBtAdapter.isEnabled())){ bluetoothIndicator.setIma bluetoothIndi cator.setImageDrawable(ge geDrawable(getResources() tResources().getDrawable( .getDrawable(R R .drawable.bluetooth_disc)); bluetoothStatus.setText(g bluetoothStat us.setText(getResources() etResources().getString(R .getString(R.string.bt_di .string.bt_dis s connected)); } }
// E��� �� �� ������� ������� ���� ��� ������� �� �����, (�������� �� ���� ����). ����). E������� ����� ��� �����/������ ����� �� �� �������� ��������� �� ���� � ����. E� �������� ��� �������� ���� ����� �� ������1 ��� ����� ��� ������� ������������ �� ���������� ��� �� �� ������� ��� ���������� �� ��������� ���� ��������� ������� ����������� ���������. �� ����� ����� ������� ���� �������� � ������ ��������� ��� �������� �� ������� ���� ������� � ���������� �� �������� �� �� ����� �����. A �� ��� �������������� �� ������ �� ������� ������. @Override public void onClick(View o nClick(View v) { // TODO Auto-generated method stub String response=""; switch(v.getId()){ case (R.id.button1): startScanner(); break; case(R.id.botonTodo): if (statusBotonTodo=="ENCENDIDO"){ response=sendCommand("@P1"); if (response.compareTo("#OKPON")==0) for (int i = 0;i
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 15
} break; case(R.id.bulb1): if (boardStatu (boardStatus[0]==0){ s[0]==0){ response=sendCommand("@11"); {if (response.compareTo("#OK1ON")==0) boardStatus[0]=1;}} boardStatus [0]=1;}} else{ response=sendCommand("@10"); {if (response.compareTo("#OK1OFF")==0) boardStatus[0]=0;}} boardStatus [0]=0;}} break; case(R.id.bulb2): if (boardStatu (boardStatus[1]==0){ s[1]==0){ response=sendCommand("@21"); {if (response.compareTo("#OK2ON")==0) boardStatus[1]=1;}} boardStatus [1]=1;}} else{ response=sendCommand("@20"); {if (response.compareTo("#OK2OFF")==0) boardStatus[1]=0;}} boardStatus [1]=0;}} break; case(R.id.bulb3): if (boardStatu (boardStatus[2]==0){ s[2]==0){ response=sendCommand("@31"); {if (response.compareTo("#OK3ON")==0) boardStatus[2]=1;}} boardStatus [2]=1;}} else{ response=sendCommand("@30"); {if (response.compareTo("#OK3OFF")==0) boardStatus[2]=0;}} boardStatus [2]=0;}} break; case(R.id.bulb4): if (boardStatu (boardStatus[3]==0){ s[3]==0){ response=sendCommand("@41"); {if (response.compareTo("#OK4ON")==0) boardStatus[3]=1;}} boardStatus [3]=1;}} else{ response=sendCommand("@40"); {if (response.compareTo("#OK4OFF")==0) boardStatus[3]=0;}} boardStatus [3]=0;}} break; case(R.id.bulb5): if (boardStatu (boardStatus[4]==0){ s[4]==0){ response=sendCommand("@51"); {if (response.compareTo("#OK5ON")==0) boardStatus[4]=1;}} boardStatus [4]=1;}} else{ response=sendCommand("@50"); {if (response.compareTo("#OK5OFF")==0) boardStatus[4]=0;}} boardStatus [4]=0;}} break; case(R.id.fan): if (boardStatu (boardStatus[5]==0){ s[5]==0){ response=sendCommand("@F1"); {if (response.compareTo("#OKFON")==0) boardStatus[5]=1;}} boardStatus [5]=1;}} else{ response=sendCommand("@F0"); {if (response.compareTo("#OKFOFF")==0) boardStatus[5]=0;}} boardStatus [5]=0;}} break; } updateImages(); }
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 16
// F������ E�CA�E�, E�CA�E�, ���� ������� �� �� ��� ��� ������� �� ������� �� ��� ��������� ��� ����� ��������� �� �� ����� B��������C���������. B��������C���������. ������� ��� ������� �� ������ B��������A������ ���� �������� �� ��������� �� B��������. � ��� �� ����������� �� �� ������ B�������� �� ������� ����������� A������ � ���� �������, � �� ��� ���� �� ���������. ��� ��� �������� ����������� �� ������ ��� ������ ���� ������� ��� ������� ����������, � �� ����� ��� ����� �� E������� �������� � E��������� ���� �� ���������� ��� ���������� ��������: �� ����� �� ������ ���� ����� ��������� �� ����������� B�2 � ����������� �� ������ �� �� ������ B����������������. �� ����� �� ������ ���� ������� �� ������������ � ����������� ��� �������������� �� ������������. - ����������� �� ������ �� �� ������ B����������������. ��� ������� ������� �� ������ B��������C��������� ��� �� ��� ��� B�2. � ���������� �� �������� ��� �� ������� ��C������.
public void startScanner( s tartScanner(){ ){ mBtAdapter = BluetoothAd BluetoothAdapter.getDefa apter.getDefaultAdapter() ultAdapter(); ; //Inicializa manejador Bluetooth. Android no nos da un manejador if (mBtAdapter == null) { //Entra si el dispositivo Bluetooth no está disponible o si no hay dispositivo Bluetooth en nuestro terminal Toast.makeText(this, Toast.makeT ext(this, "Bluetooth no está disponible", Toast.LENGTH_LONG).show(); finish(); return; } if (!mBtAdapt (!mBtAdapter.isEnabled er.isEnabled()) ()) { //Entra si no es capaz de habilitar el manejador. El blueetooth está apagado Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //Lanza un Intent de solicitud de activación del dispositivo Bluetooth. startActivityForResult(enableIntent, REQUEST_ENABLE_BT); //Recoge el resultado en REQUEST_ENABLE_BT previamente declarada por nosotros. } while(!mBtAdapter.isEna while(!mBtA dapter.isEnabled()){} bled()){} bluetoothIn dicator.setImageDrawable( bluetoothIndicator.setIm ageDrawable(getResources( getResources().getDrawabl ).getDrawable(R.drawa e(R.drawa ble.bluetooth)); ble.bluetoo th)); bluetoothSt atus.setText(getResources bluetoothStatus.setText( getResources().getString( ().getString(R.string.bt_ R.string.bt_connected connected )); button_scan er.setText(getResources() button_scaner.setText(ge tResources().getString(R. .getString(R.string.scann string.scanning)); ing)); mBtAdapter.startDiscover mBtAdapter.s tartDiscovery(); y(); //Empieza el escaner de dispositivos bluetooth. IntentFilter filter = new IntentFilter(BluetoothDe IntentFilte r(BluetoothDevice.ACTION_ vice.ACTION_FOUND); FOUND); //Evento descubrimie descubrimiento nto dispositivo this.registerReceiver(m this.regist erReceiver(mReceiver, Receiver, filter); //Registra evento en BroadcastReceiver. filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //Evento fin de búsqueda de dispositivo. this.registerReceiver(m this.regist erReceiver(mReceiver, Receiver, filter); //Registra evento en BroadcastReceiver. }
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 17
public boolean boolea n tryConnectArduino() tryConnect Arduino() { btC=new BluetoothConnect Blue toothConnection(macArduin ion(macArduinoBT2); oBT2); if (btC.doConnect()==BluetoothConnection.BLUETOOTH_CREATION_SUCCESS) return true; else return false; }
// F������ ���� ������ �������� � ���������� ���������� � ������ �� ��� ��������� ��������� �������� � ����������� private String Str ing sendCommand(String sendComma nd(String command){ co mmand){ btC.sendInfo(command); btC.sendInfo( command); return (btC.receiv (btC.receiveInfo()); eInfo()); }
// E��� ������� ���� �� ��� ���������� ���� ���������� ���������� ��� �������� ��� ������ ������ ����������� ��� ������ ��� ������, ��� ������� ����������� ���� ��� ��� ������� �� ������ ��C���� ������� ����� ����� �� �� ��������. private void voi d updateImages(){ updateImage s(){ if (boardStatus[0]==0) this.bulb1Image.setImageDrawable(getResources().getDrawable(R.dr awable.b1off)); else this.bulb1Image.setImageDrawable(getResources().getDrawable(R.dr awable.b1on)); if (boardStatus[1]==0) this.bulb2Image.setImageDrawable(getResources().getDrawable(R.dr awable.b2off)); else this.bulb2Image.setImageDrawable(getResources().getDrawable(R.dr awable.b2on)); if (boardStatus[2]==0) this.bulb3Image.setImageDrawable(getResources().getDrawable(R.dr awable.b3off)); else this.bulb3Image.setImageDrawable(getResources().getDrawable(R.dr awable.b3on)); if (boardStatus[3]==0) this.bulb4Image.setImageDrawable(getResources().getDrawable(R.dr awable.b4off)); else this.bulb4Image.setImageDrawable(getResources().getDrawable(R.dr awable.b4on)); if (boardStatus[4]==0) this.bulb5Image.setImageDrawable(getResources().getDrawable(R.dr awable.b5off)); else this.bulb5Image.setImageDrawable(getResources().getDrawable(R.dr awable.b5on)); if (boardStatus[5]==0)
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 18
this.fanImage.setImageDrawable(getResources().getDrawable(R.draw able.fan_off)); else this.fanImage.setImageDrawable(getResources().getDrawable(R.draw able.fan_on)); } /* private void voi d show(String message){ Toast.makeText(this, message, Toast.LENGTH_LONG).show(); } */ }
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 19
�� ������������ ���� �������� ��� �� ������� �� �� �������� �� ���� �������� ������� ��� ������������ ��� ����� ������ ���� �������� �� ������� � �� ��������� ��� �� ����������� �� �������� �����������. �� ������ ����� ��� ��� �������� ��� �� ��� �� A������ �������� ����� �� ����� � �� ���� �� ��������� �� �����, �� ����� ����� ����� �� ������� �� ���� ����� �� ����� �������� ���� �� �������� ����������� � ���� ����������� �� A������ ������ ���� � ������ ������������� �� �����������, ��� ��� �������� ������� ��� ���� �������� ������� �� A������ ������ ����� �� �= =� ���� �� ��������� �������� �� ������������ � �� ����� ��������� �� ����������� �������� �� ����� ��� ����� � �������� �� ��������� �� �� ����� � ����. �������� � �� ������������ �� A������ ������� � �� ������������� ��������� � �� ������� �� ������������ B��������� ����� ������ ����������� �� �������� ��� ��� ��� ������� ��������� ��� ���� ����� ������ ������� ������������ �� ������� ��� ������������� ��� ������ �� �� ����� �� A������, ��� ��� ���������� ����� ��������� ������� ����� ��� �������� �������� ��� ���������� ���������� �� A������
�� �� ���� ���������� �
�������� ��������� ����� ������ �������� ��� ����������� �������� ���� ��� A������ � A������.
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 20
�� ����� �� �� �������� ��� ����� ��� �� ��� ����������� �� ���� �������� ���, ������ ��������, ������ ���������, �������������� ��� ������ � �������. •
D����� ��������: ���� ��� �� ���� �� ��������� �� ����� �� ������ ��� �������� ��� ��������.
•
o
������ 25%
o
����� 50%
o
���� 25%
D����� ���������: ��������� ��� ������ �� ��� ��������� � ���������� ��� ������ ���� �� ��������������.
•
o
������ 25%
o
����� 25%
o
���� 50%
�������������� �� ������: ��������� �� ����������� �� ������ ��� �������� �� A������ � ��� �������.
•
o
������ 50%
o
����� 25%
o
���� 25%
�������: ��� ��������� �� �������� �� ������� �� �� ��������. o
������ 33%
o
����� 33%
o
���� 33%
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 21
�� ������������ ���� �� ����� �� �� ���������� �� ���� �������� ����� ������ ��� �� �������� ��������� �� �� ��������, ����� ���������� � �� ������ �� ��������������� �� A������.
����������������������������������������������������������������� ��������������������������������%��%������ ������������������������������������
Práctica 3 – Sistema Sistema Android para control control de dispositivo domótico por por Bluetooth 22