package com.example.liyi.ui;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.example.liyi.ui.adapter.User_list_adapter;
import com.example.liyi.ui.data.Users;
import com.example.liyi.ui.thread.ConnectThread;
import com.example.liyi.ui.thread.ListenerThread;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
//import com.xiaoniu.wifihotspotdemo.adapter.WifiListAdapter;
import com.example.liyi.ui.thread.ConnectThread;
import com.example.liyi.ui.thread.ListenerThread;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class SecondActivity extends AppCompatActivity implements View.OnClickListener{
private static String ss;
private ListView listView;
private Button btn_create_hostspot;
private Button btn_close_hostspot;
private Button btn_send;
private Button btn_search;
private TextView textview;
private TextView text_state;
private User_list_adapter user_list_adapter;
private WifiManager wifiManager;
// private WifiListAdapter wifiListAdapter;
private WifiConfiguration config;
private int wcgID;
/**
* 热点名称
*/
private static final String WIFI_HOTSPOT_SSID = "TEST";
/**
* 端口号
*/
private static final int PORT = 8086;
private static final int WIFICIPHER_NOPASS = 1;
private static final int WIFICIPHER_WEP = 2;
private static final int WIFICIPHER_WPA = 3;
public static final int DEVICE_CONNECTING = 1;//有设备正在连接热点
public static final int DEVICE_CONNECTED = 2;//有设备连上热点
public static final int SEND_MSG_SUCCSEE = 3;//发送消息成功
public static final int SEND_MSG_ERROR = 4;//发送消息失败
public static final int GET_MSG = 6;//获取新消息
private ConnectThread connectThread;
private ListenerThread listenerThread;
private int c1,c2,c3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initVIew();
c1=c2=c3=0;
initBroadcastReceiver();
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
listenerThread = new ListenerThread(PORT, handler);
listenerThread.start();
}
private void initVIew() {
btn_send = (Button) findViewById(R.id.btn_send);
text_state = (TextView) findViewById(R.id.text_state);
btn_send.setOnClickListener(this);
listView=(ListView) findViewById(R.id.teacher_listView);
user_list_adapter = new User_list_adapter(this, R.layout.user_item, Users.getAllTeachers());
listView.setAdapter(user_list_adapter);
}
private void connect(WifiConfiguration config) {
text_state.setText("连接中...");
wcgID = wifiManager.addNetwork(config);
wifiManager.enableNetwork(wcgID, true);
}
private void initBroadcastReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
// intentFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
registerReceiver(receiver, intentFilter);
}
//按钮事件以及调用connect线程
@Override
public void onClick(View v) {
switch (v.getId()) {
/*
case R.id.btn_create_hostspot:
createWifiHotspot();
break;
case R.id.btn_close_hostspot:
closeWifiHotspot();
break;
*/
case R.id.btn_send:
if (connectThread != null) {
connectThread.sendData("1");
}else{
Log.w("AAA","connectThread == null");
}
break;
/*
case R.id.btn_search:
search();
break;
*/
}
}
/**
* 创建Wifi热点
*/
private void createWifiHotspot() {
if (wifiManager.isWifiEnabled()) {
//如果wifi处于打开状态,则关闭wifi,
wifiManager.setWifiEnabled(false);
}
WifiConfiguration config = new WifiConfiguration();
config.SSID = WIFI_HOTSPOT_SSID;
config.preSharedKey = "123456789";
config.hiddenSSID = true;
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.OPEN);//开放系统认证
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
//通过反射调用设置热点
try {
Method method = wifiManager.getClass().getMethod(
"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
boolean enable = (Boolean) method.invoke(wifiManager, config, true);
if (enable) {
textview.setText("热点已开启 SSID:" + WIFI_HOTSPOT_SSID + " password:123456789");
} else {
textview.setText("创建热点失败");
}
} catch (Exception e) {
e.printStackTrace();
textview.setText("创建热点失败");
}
}
/**
* 关闭WiFi热点
*/
public void closeWifiHotspot() {
try {
Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
method.setAccessi