苹果,安卓等等智能移动终端的飞速发展,刺激了wifi的飞速发展。一时间全国各个城市充满了大大小小的wifi热点。而wifi的发展,又刺激了wifi应用方面的发展。智能家具,穿戴设备在低价,高速,稳定的WIFI基础上迅速发展起来。
智能家具出于体积,简捷,成本和稳定方面考虑,很少设计用于显示的显示屏和用于输入的按键。而让智能家具连接上网,需要在智能家具开启状态下,把当前的wifi的名字和密码告诉智能家具。帮助智能家具联网的方法比较多,一般不同的公司会根据公司情况选择相应的技术。其中UDP组播也是常用的方式。比如使用marvell芯片的小米家具。
service端收
public class UDPMulticastServer {
final static int RECEIVE_LENGTH = 1024;
static String multicastHost = "224.0.0.1";
static int localPort = 9998;
public static void main(String[] args) throws Exception {
InetAddress receiveAddress = InetAddress.getByName(multicastHost);
if (!receiveAddress.isMulticastAddress()) {// 测试是否为多播地址
throw new Exception("请使用多播地址");
}
int port = localPort;
MulticastSocket receiveMulticast = new MulticastSocket(port);
receiveMulticast.joinGroup(receiveAddress);
DatagramPacket dp = new DatagramPacket(new byte[RECEIVE_LENGTH],
RECEIVE_LENGTH);
receiveMulticast.receive(dp);
System.out.println(new String(dp.getData()).trim());
receiveMulticast.close();
}
}
client发
public class UDPMulticastClient {
static String destAddressStr = "224.0.0.1";
static int destPortInt = 9998;
static int TTLTime = 4;
public static void main(String[] args) throws Exception {
InetAddress destAddress = InetAddress.getByName(destAddressStr);
if (!destAddress.isMulticastAddress()) {// 检测该地址是否是多播地址
throw new Exception("地址不是多播地址");
}
int destPort = destPortInt;
int TTL = TTLTime;
MulticastSocket multiSocket = new MulticastSocket();
multiSocket.setTimeToLive(TTL);
/*
byte[] sendMSG = "11#msg".getBytes();
DatagramPacket dp = new DatagramPacket(sendMSG, sendMSG.length,
destAddress, destPort);
multiSocket.send(dp);
*/
DatagramPacket outPacket = new DatagramPacket(new byte[0], 0,
destAddress, destPort);
// 创建键盘输入流
Scanner scan = new Scanner(System.in);
// 不断地读取键盘输入
while (scan.hasNextLine()) {
// 将键盘输入的一行字符串转换成字节数组
byte[] buff = scan.nextLine().getBytes();
// 设置发送用的DatagramPacket中的字节数据
outPacket.setData(buff);
// 发送数据报
multiSocket.send(outPacket);
// 读取Socket中的数据,读到的数据放在inPacket所封装的字节数组中
//multiSocket.receive(inPacket);
//System.out.println(new String(inBuff, 0, inPacket.getLength()));
}
multiSocket.close();
}
}
service发
public class BroadServer {
static String Broadcast_IP = "224.0.0.1";
static int Broadcast_PORT = 9998;
public static void main(String[] args) throws IOException, InterruptedException {
//Server
MulticastSocket mSocket = new MulticastSocket(Broadcast_PORT);
InetAddress group=InetAddress.getByName(Broadcast_IP);
byte[] buff = "192.168.0.124".getBytes();
mSocket.joinGroup(group);
mSocket.setTimeToLive(1);
DatagramPacket packet = new DatagramPacket(buff,buff.length,group,Broadcast_PORT);
while(true)
{
mSocket.send(packet);
Thread.sleep(1000);
}
}
}
client收
public class BroadClient {
static String Broadcast_IP = "224.0.0.1";
static int Broadcast_PORT = 8501;
//接收广播中的IP地址
public static String getBroadCastIP() {
byte[] buffer;
buffer = new byte[15];
try {
MulticastSocket s = new MulticastSocket(Broadcast_PORT);
InetAddress group = InetAddress.getByName(Broadcast_IP);
s.joinGroup(group);
DatagramPacket packet = new DatagramPacket(buffer , 15);
s.receive(packet);
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int i = 0;
while(buffer[i]!=0){
i++;
}
String IP = new String(buffer,0,i);
System.out.println("AAAA"+IP);
return IP;
}
public static void main(String[] args) throws Exception {
getBroadCastIP();
}
}
android端
public class NetUtil {
private static final String TAG="Net.Utils";
private static final int MULTICAST_PORT=9998;
private static final String GROUP_IP="224.0.0.1";
private static byte[] sendData;
static{
sendData=new byte[4];
// 0xEE78F1FB
sendData[3] = (byte) 0xEE;
sendData[2] = (byte) 0x78;
sendData[1] = (byte) 0xF1;
sendData[0] = (byte) 0xFB;
}
public static String findServerIpAddress() throws IOException{
String ip=null;
MulticastSocket multicastSocket=new MulticastSocket(MULTICAST_PORT);
multicastSocket.setLoopbackMode(true);
InetAddress group = InetAddress.getByName(GROUP_IP);
multicastSocket.joinGroup(group);
DatagramPacket packet=new DatagramPacket(sendData, sendData.length,group,MULTICAST_PORT);
for(;;){
multicastSocket.send(packet);
Log.d(TAG,">>>send packet ok");
byte[] receiveData=new byte[256];
Log.d(TAG,"11111111");
packet=new DatagramPacket(receiveData, receiveData.length);
Log.d(TAG,"2222222222");
multicastSocket.receive(packet);
Log.d(TAG,"3333");
String packetIpAddress=packet.getAddress().toString();
packetIpAddress=packetIpAddress.substring(1, packetIpAddress.length());
Log.d(TAG,"packet ip address: "+packetIpAddress);
StringBuilder packetContent=new StringBuilder();
for(int i=0;i<receiveData.length;i++){
if(receiveData[i]==0){
Log.d(TAG,"packet ip address: 111111");
break;
}
packetContent.append((char)receiveData[i]);
}
ip=packetContent.toString();
Log.d(TAG,"packet content ip is: "+ip);
if(ip.equals(packetIpAddress)){
Log.d(TAG,"find server ip address: "+ip);
Log.d(TAG,"packet ip address: 2222");
break;
}else{
Log.d(TAG,"not find server ip address, continue …");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
return ip;
}
}
public class MainActivity extends Activity {
MulticastLock multicastLock;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allowMulticast();
//new Thread().start();
new Thread() {
public void run() {
try {
NetUtil.findServerIpAddress();
} catch (IOException e) {
throw new RuntimeException(e);
}
Log.d("multicast.demo", "find ip ok.");
}
}.start();
}
private void allowMulticast() {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
multicastLock = wifiManager.createMulticastLock("multicast.test");
multicastLock.acquire();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
multicastLock.release();
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}