add wifi register
This commit is contained in:
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@@ -26,6 +26,7 @@
|
||||
</set>
|
||||
</option>
|
||||
<option name="resolveModulePerSourceSet" value="false" />
|
||||
<option name="useQualifiedModuleNames" value="true" />
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
|
||||
<uses-feature
|
||||
android:glEsVersion="0x00020000"
|
||||
@@ -61,7 +63,14 @@
|
||||
android:label="路况服务"
|
||||
android:launchMode="singleTask" />
|
||||
|
||||
<receiver android:name=".WifiBroadCastReceiver" />
|
||||
<receiver android:name=".wifi.WifiBroadCastReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="android.net.wifi.RSSI_CHANGED"/>
|
||||
<action android:name="android.net.wifi.STATE_CHANGE" />
|
||||
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
|
||||
</intent-filter>
|
||||
|
||||
</receiver>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -1,24 +1,29 @@
|
||||
package com.mogo.cloud;
|
||||
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceView;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import android.widget.ToggleButton;
|
||||
|
||||
|
||||
import com.mogo.cloud.trafficlive.api.ITrafficLiveCallBack;
|
||||
import com.mogo.cloud.trafficlive.api.MoGoAiCloudTrafficLive;
|
||||
import com.mogo.cloud.wifi.IWifiStateListener;
|
||||
import com.mogo.cloud.wifi.WifiStateManager;
|
||||
|
||||
|
||||
public class LivePlayActivity extends AppCompatActivity implements ITrafficLiveCallBack {
|
||||
public class LivePlayActivity extends AppCompatActivity implements ITrafficLiveCallBack, IWifiStateListener {
|
||||
private String TAG = "LiveActivity";
|
||||
|
||||
private SurfaceView surfaceView;
|
||||
private ToggleButton liveToggleBtn;
|
||||
private EditText etLookRoomId;
|
||||
private TextView tvWifiState;
|
||||
private String liveSn;
|
||||
|
||||
@Override
|
||||
@@ -28,6 +33,7 @@ public class LivePlayActivity extends AppCompatActivity implements ITrafficLiveC
|
||||
surfaceView = findViewById(R.id.surfaceView);
|
||||
etLookRoomId = findViewById(R.id.etLookRoomId);
|
||||
liveToggleBtn = findViewById(R.id.liveToggleBtn);
|
||||
tvWifiState = findViewById(R.id.tvWifiState);
|
||||
liveToggleBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
|
||||
if (isChecked) {
|
||||
@@ -37,6 +43,7 @@ public class LivePlayActivity extends AppCompatActivity implements ITrafficLiveC
|
||||
MoGoAiCloudTrafficLive.stopLive(liveSn);
|
||||
}
|
||||
});
|
||||
WifiStateManager.getInstance().registerWifiStateListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,6 +51,7 @@ public class LivePlayActivity extends AppCompatActivity implements ITrafficLiveC
|
||||
super.onDestroy();
|
||||
MoGoAiCloudTrafficLive.stopLive(liveSn);
|
||||
MoGoAiCloudTrafficLive.destroyLive();
|
||||
WifiStateManager.getInstance().unRegisterWifiStateListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,4 +68,9 @@ public class LivePlayActivity extends AppCompatActivity implements ITrafficLiveC
|
||||
public void onError(String errorMsg) {
|
||||
Log.d(TAG, "发生错误 onError msg: " + errorMsg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWifiState(WifiInfo wifiInfo) {
|
||||
tvWifiState.setText("wifiLevel: " + Math.abs(wifiInfo.getRssi()));
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.mogo.cloud;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import com.mogo.cloud.utils.logger.Logger;
|
||||
|
||||
public class WifiBroadCastReceiver extends BroadcastReceiver {
|
||||
|
||||
private static final String TAG = "WifiBroadCastReceiver";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
int wifiState = intent.getIntExtra("wifi_state", 0);
|
||||
WifiManager wifiManager = ((WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE));
|
||||
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
||||
int linkSpeed = wifiInfo.getLinkSpeed();
|
||||
Logger.d(TAG, "wifi linkSpeed : " + linkSpeed); //wifi连接速度
|
||||
//0到-50表示信号最好,-50到-70表示信号偏差,小于-70表示最差,有可能连接不上或者掉线
|
||||
int level = Math.abs(wifiInfo.getRssi());
|
||||
Logger.d(TAG, "wifi level : " + level); //wifi信号强度
|
||||
switch (wifiState) {
|
||||
case WifiManager.WIFI_STATE_DISABLING:
|
||||
Logger.d(TAG, "WIFI State : DISABLING");
|
||||
break;
|
||||
case WifiManager.WIFI_STATE_DISABLED:
|
||||
Logger.d(TAG, "WIFI State : DISABLED");
|
||||
break;
|
||||
case WifiManager.WIFI_STATE_ENABLING:
|
||||
Logger.d(TAG, "WIFI State : ENABLING");
|
||||
break;
|
||||
case WifiManager.WIFI_STATE_ENABLED:
|
||||
Logger.d(TAG, "WIFI State : ENABLED");
|
||||
break;
|
||||
case WifiManager.WIFI_STATE_UNKNOWN:
|
||||
Logger.d(TAG, "WIFI State : UNKNOWN");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.mogo.cloud.wifi;
|
||||
|
||||
import android.net.wifi.WifiInfo;
|
||||
|
||||
public interface IWifiStateListener {
|
||||
|
||||
void onWifiState(WifiInfo wifiInfo);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.mogo.cloud.wifi;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import com.mogo.cloud.utils.logger.Logger;
|
||||
|
||||
public class WifiBroadCastReceiver extends BroadcastReceiver {
|
||||
|
||||
private static final String TAG = "WifiBroadCastReceiver";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
int wifiState = intent.getIntExtra("wifi_state", 0);
|
||||
WifiManager wifiManager = ((WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE));
|
||||
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
||||
if (wifiInfo != null) {
|
||||
Logger.d(TAG, "onReceive wifiInfo, update wifiState");
|
||||
WifiStateManager.getInstance().updateWifiState(wifiInfo);
|
||||
|
||||
int linkSpeed = wifiInfo.getLinkSpeed();
|
||||
Logger.d(TAG, "wifi linkSpeed : " + linkSpeed); //wifi连接速度
|
||||
//0到-50表示信号最好,-50到-70表示信号偏差,小于-70表示最差,有可能连接不上或者掉线
|
||||
int level = Math.abs(wifiInfo.getRssi());
|
||||
Logger.d(TAG, "wifi level : " + level); //wifi信号强度
|
||||
|
||||
switch (wifiState) {
|
||||
case WifiManager.WIFI_STATE_DISABLING:
|
||||
Logger.d(TAG, "WIFI State : DISABLING");
|
||||
break;
|
||||
case WifiManager.WIFI_STATE_DISABLED:
|
||||
Logger.d(TAG, "WIFI State : DISABLED");
|
||||
break;
|
||||
case WifiManager.WIFI_STATE_ENABLING:
|
||||
Logger.d(TAG, "WIFI State : ENABLING");
|
||||
break;
|
||||
case WifiManager.WIFI_STATE_ENABLED:
|
||||
Logger.d(TAG, "WIFI State : ENABLED");
|
||||
break;
|
||||
case WifiManager.WIFI_STATE_UNKNOWN:
|
||||
Logger.d(TAG, "WIFI State : UNKNOWN");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
46
app/src/main/java/com/mogo/cloud/wifi/WifiStateManager.java
Normal file
46
app/src/main/java/com/mogo/cloud/wifi/WifiStateManager.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.mogo.cloud.wifi;
|
||||
|
||||
import android.net.wifi.WifiInfo;
|
||||
|
||||
import com.zhidao.manager.system.wifi.WifiState;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WifiStateManager {
|
||||
|
||||
private WifiStateManager() {
|
||||
|
||||
}
|
||||
|
||||
public static WifiStateManager getInstance() {
|
||||
return Holder.wifiStateManager;
|
||||
}
|
||||
|
||||
private static final class Holder {
|
||||
private static final WifiStateManager wifiStateManager = new WifiStateManager();
|
||||
}
|
||||
|
||||
private final List<IWifiStateListener> wifiStateListeners = new ArrayList<>();
|
||||
|
||||
public void registerWifiStateListener(IWifiStateListener listener) {
|
||||
if (listener != null && !wifiStateListeners.contains(listener)) {
|
||||
wifiStateListeners.add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public void unRegisterWifiStateListener(IWifiStateListener listener) {
|
||||
if (listener != null) {
|
||||
wifiStateListeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateWifiState(WifiInfo wifiInfo) {
|
||||
if (wifiStateListeners.size() > 0) {
|
||||
for (IWifiStateListener listener : wifiStateListeners) {
|
||||
listener.onWifiState(wifiInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
>
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<SurfaceView
|
||||
android:id="@+id/surfaceView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvWifiState"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="#17FBF4"
|
||||
android:textSize="32px"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etLookRoomId"
|
||||
|
||||
Reference in New Issue
Block a user