Android open source code

Android software receive the status from eHouse smart home controllers via UDP or TCP/IP

Ethernet eHouse Smart Home controllers automatically send their status via UDP (User Datagram Protocol). Status can be received via any devices in your LAN or WiFi.

UDP Broadcast allows you to disable the download status of the TCP/IP client , which requires server connection to the controller .
UDP Broadcast is a connectionless protocol, and regardless of the number of the receiving devices, does not load the processor more.

Simple UDP broadcast reception function for Android smartphones , panels, smart TV, PADs is shown below .
This is an infinite loop so based on this function you need to create a thread working in the background , giving him the lowest priority of work .
Otherwise, this function may block the execution of other more important activities of the program .

The source code of UDP Broadcast reception Android platform :

public void udp (int port) // parameter port on which we receive broadcast by default 6789 – the status of the controller in binary format
{

byte [ ] broadcastadr = new byte [4 ] ;
byte [ ] localadr = new byte [4 ] ;
WifiManager wifi = ( WifiManager ) getSystemService ( Context.WIFI_SERVICE ) ; // wifi manager
DhcpInfo dhcp = wifi.getDhcpInfo () ; // get dhcp info from the wifi
if ( dhcp == null) // get default value if DHCP is disabled network of eHouse
{// No dhcp or an error for the device
broadcastadr [0] = (byte ) 192 ;
broadcastadr [1 ] = (byte ) 168 ;
broadcastadr [2] = (byte ) 0 ;
broadcastadr [3] = (byte ) 255 ;
}
else // DHCP
{
broadcast int = ( dhcp . IPAddress&dhcp.netmask )| ~ dhcp.netmask ; // For UDP broadcast – if you wish to broadcast the data further , this function only receives the status of drivers
for (int k = 0 ; k < 4 ; k++ ) broadcastadr [ k] = (byte ) ( (Broadcast >> k * 8) & 0xFF ) ;
int local = ( dhcp.iPAddress ) ; // local ip address of the dhcp
for (int k = 0 ; k < 4 ; k++ ) localadr [ k] = (byte ) ( (local >> k * 8) & 0xFF ) ; // for local ip address
}
try
{
ds = new DatagramSocket (port ) ; /// listening UDP data on port from any ip
inside a mask – create an instance of the socket UDP port number of the local
}
catch ( Exception e )
{
// ehousecommunication.l ( ” Error Inet : get address ” + e.getMessage ()) ; for test
}
try
{
setBroadcast (true) ; // enable send broadcast message – transmission
setReuseAddress (true) ; // enable multiple socket use the same IP address
// for . setSoTimeout (400) ; // reception timeout

}
catch (Exception io)
{// Ignore errors but hedge our application and system errors

}

// * Udp broadcast TEST – Test udp broadcastbroadcast from your phone or PAD, smart TV- future applications of communication with the
/ * try
{
DatagramPacket dd = new DatagramPacket ( ” Hello UPD Word from the Android TEST ” . getBytes () , 0 ,
” Hello UPD Word from the Android TEST ” . length () ,
InetAddress . getByAddress ( broadcastadr ) , port) ;
send ( dd) ; // sends a DatagramPacket
}
catch ( Exception e )
{
ehousecommunication.l ( ” Error Transmit Datagram packet : ” + e.getMessage ()) ;

}
* /

MulticastLock ml = wifi.createMulticastLock ( ” Multicast lock enabled ” ) ;
// creates an instance of blocking multicast and broadcast
ml.acquire ( ) ; // Multicast lock enable UDP broadcast reception – can be disabled on Certain android smartphones for batery saving purposes – enables support for UDP broadcastu on the phone if it is locked and only disabled

while (! ehousecommunication.Terminate ) // infinite loop for UDP reception
{// Infinite loop receiving UDP status
DatagramPacket dp = new DatagramPacket (message , message.length) ; // create UDP datagram packet buffer using date – forms a UDP packet using a buffer ” message ”
try {
receive ( dp ) ; // UDP packet reception from ehouse system – receives UDP packet with the UDP port soket
if ( dp.getLength()> 0) // if the size of packets greater than zero – something received
{
EhouseTCP.QueryBuff = dp.getData () ; // assign a UDP broadcast recepition querybuffer – loaded into the buffer status eHouse
// dp.setLength (0) ; – sets the packet size to zero so as not to process the same data multiple times
EhouseTCP.QueryReceived () ; // decode query data and fill devices arrays – decodes the status of controllers and loads the application status table
}
}
catch (Exception os )
{ // Ignore errors
// ehousecommunication.l ( ” asdfasdfsafsa ” + os . getMessage ()) ; // for test only
}

}
ml.release () ; // Multicast lock free for UDP reception of eHouse application – releases the lock multicast
close () ; // close datagram socket at the end – closes the socket at the end of
}

Multicast lock may be required on some Android devices .

Local Broadcast UDP in the case of mobile phones can be active or not , depending on the mobile phone firmware .
It can also be permanently locked in for battery saving and for safety reasons or to reduce the CPU load on the network at work.
( note the kernel configuration : CONFIG_IP_MULTICAST is not set ) .
In addition, some models may be blocked by the local Firewall and you have to activate the transmission on a given port (default 6789 ) .
Outside Broadcast when using GPRS Internet connections , EDGE , 3G , 4G can be locked or unlocked by the network operator. More often , however, must rely , that it is fixed in order to minimize the disposal of links.

Old versions of androids 2.x


In some models of mobile phones , Receiving software implementation statuses UDP drivers can be very difficult or even impossible. May require the use of standard android firmware not limited by GSM operator.
On the basis of several models of HTC phones and other containing the Android operating system version lower than 2 . 3 . 3 observed complete blockage receiving UDP broadcastu . This was the version of android ( 2 . 1 – 2 . 3 of the period of 01 . 01 . 2010 – 14 . 06 . 2011)

In the absence of receipt of UDP broadcast download and install , new ROM Android minimum version 2 . 3 . 3 (of June 14, 2011 ) , which tested the correctness of the work load UDP broadcast .
Kernel Version 2 . 6 . 35 . 10 – g3ef43272 htc – kernel @ and18 – 2 # 1 .
Software Number 3 . 14 . 405 . 1 .
For other phone brands for similar problems should be taken Rom Android firmware from the manufacturer at least version 2 . 3 . 3 .

Newer SmartPhones, PADs, SMART TVs & with newest versions of Android (4.x) do not have this issues and do not require additional actions to receive UDP packages.

You should also bear in mind the essential characteristics of UDP Packets .
They are asynchronous and require read on a regular basis which can cause data read errors , their loss ( shedding ), especially when devices are slow or busy, utilize slow communication links , combine high load and CPU overhead SmartPhones and tablets .
Udp status data are sent twice so be sure to compare whether all the bytes of both copies are the same , and if not, just ignore them.
intelligent building eHouse Home Automation , Home Automation eHouse smart home controllers eHouse

For more information about the use of UDP broadcastu drivers eHouse :
Inteligentny dom eHouse dekodowanie statusu eHouse LAN for PC – Windows XP , Vista , 7 in Delphi
Inteligentny dom ehouse dekodowanie statusu source code
Inteligentny dom eHouse RS-485 dekodowanie statusu source code
inteligentny dom eHouse udp status
Inteligentny dom eHouse komunikacja

Leave a Reply

Your email address will not be published. Required fields are marked *