【发布时间】:2019-02-05 07:51:23
【问题描述】:
当我想验证 Android 设备是否连接到特定的 Wi-Fi(由 SSID 识别)并且同时有一个活动的移动数据网络时,我遇到了一些奇怪的问题。 我的问题也可能与 Android 9 (SDK 28) 有关,因为我没有收到使用以前 Android 版本的用户的任何投诉。
这就是我所拥有的:
这是我的主要方法,它应该告诉我设备当前是否连接到它的目标 Wi-Fi:
public boolean isConnectedToTrackerHotSpot() {
if (isConnectedToWiFi()) {
final String ssid = getConnectedSSID();
if (ssid.startsWith(TRACKER_WIFI_PREFIX)) {
Log.i(TAG, "isConnectedToTrackerHotSpot() --> YEAH, connected to Tracker: " + ssid);
return true;
} else {
Log.d(TAG, "isConnectedToTrackerHotSpot() --> NOT connected to Tracker, but: " + ssid);
return false;
}
}
Log.d(TAG, "isConnectedToTrackerHotSpot() --> NOT connected to any Wi-Fi!");
return false;
}
这里是辅助方法:
检查我们当前是否有 Wi-Fi 连接:
public boolean isConnectedToWiFi() {
if (!isWifiEnabled()) {
Log.w(TAG, "isConnectedToWiFi(): Wi-Fi is DISABLED");
return false;
}
final WifiInfo wifiInfo = getConnectionInfo();
if (wifiInfo == null) {
Log.w(TAG, "isConnectedToWiFi() --> wifiInfo is null!");
return false;
}
final int networkId = wifiInfo.getNetworkId();
if (networkId == -1) {
Log.d(TAG, "isConnectedToWiFi() --> NOT connected to any Wi-Fi (networkId == -1)");
return false;
}
Log.d(TAG, "isConnectedToWiFi() --> connected to Wi-Fi with networkId: " + networkId);
return true;
}
检查我们是否连接到任何网络:
public boolean isConnected() {
/*
Returns details about the currently active default data network.
When connected, this network is the default route for outgoing connections.
*/
final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
Log.d(TAG, "isConnected() --> getActiveNetworkInfo() == NULL (no network connection)");
return false;
} else {
Log.d(TAG, "isConnected() --> networkInfo.getTypeName() = " + networkInfo.getTypeName());
}
if (networkInfo.isConnectedOrConnecting()) {
return true;
}
Log.w(TAG, "isConnected() --> !networkInfo.isConnectedOrConnecting()");
return false;
}
现在在我的广播接收器中,我想在连接到预期的设备后立即处理此案:
private BroadcastReceiver connectivityBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!Objects.equals(intent.getAction(), CONNECTIVITY_CHANGE)) {
Log.d(TAG, "connectivityBroadcastReceiver.onReceive() --> not the intent we are interested in");
return;
}
if (wiFiConnectionManager.isConnectedToTrackerHotSpot()) {
Log.i(TAG, "connectivityBroadcastReceiver.onReceive() --> CONNECTED to Tracker --> fragment.updateUiConnectedToTracker()");
fragment.updateUiConnectedToTracker();
openTrackerSocketConnection();
} else {
// Make sure we only show 1 AlertDialog per CONNECT attempt (= button press)
if (userAwaitsResponse) {
Log.d(TAG, "connectivityBroadcastReceiver.onReceive() --> fragment.updateUiFailedToConnect()");
fragment.updateUiFailedToConnect();
userAwaitsResponse = false;
} else {
Log.d(TAG, "connectivityBroadcastReceiver.onReceive() --> SUPPRESSING UI update (since already done)");
}
}
}
};
但是,当我在装有 Android 9 的 Pixel 3 上进行测试并启用移动网络时,我收到了以下日志:
D/ConnectDeviceFragment: connectButton --> onClick()
I/WiFiConnectionManager: isWiFiEnabled(): true
I/WiFiConnectionManager: isWiFiEnabled(): true
D/ConnectDeviceFragmentPresenter: connectToTrackersWiFi() --> use existing WifiConfiguration SSID: "TRACKER-pat08" (networkId: 141)
D/ConnectDeviceFragmentPresenter: connectToTrackersWiFi() --> going to disconnect from current network...
I/WiFiConnectionManager: disconnect() success: true
D/ConnectDeviceFragmentPresenter: connectToTrackersWiFi() --> in order to enableNetwork for networkId: 141
I/WiFiConnectionManager: enableNetwork() --> netId: 141 | success: true
D/WiFiConnectionManager: isConnectedToWiFi() --> NOT connected to any Wi-Fi (networkId == -1)
D/WiFiConnectionManager: isConnectedToTrackerHotSpot() --> NOT connected to any Wi-Fi!
D/WiFiConnectionManager: isConnected() --> networkInfo.getTypeName() = MOBILE
I/MainFragmentPresenter: connectivityBroadcastReceiver.onReceive() --> Assuming we have internet...
D/WiFiConnectionManager: isConnectedToWiFi() --> NOT connected to any Wi-Fi (networkId == -1)
D/WiFiConnectionManager: isConnectedToTrackerHotSpot() --> NOT connected to any Wi-Fi!
D/ConnectDeviceFragmentPresenter: connectivityBroadcastReceiver.onReceive() --> fragment.updateUiFailedToConnect()
D/ConnectDeviceFragment: updateUiFailedToConnect() --> creating but not showing yet (status might still change)
D/WiFiConnectionManager: isConnectedToWiFi() --> NOT connected to any Wi-Fi (networkId == -1)
D/WiFiConnectionManager: isConnectedToTrackerHotSpot() --> NOT connected to any Wi-Fi!
D/WiFiConnectionManager: isConnected() --> networkInfo.getTypeName() = MOBILE
I/MainFragmentPresenter: connectivityBroadcastReceiver.onReceive() --> Assuming we have internet...
D/WiFiConnectionManager: isConnectedToWiFi() --> NOT connected to any Wi-Fi (networkId == -1)
D/WiFiConnectionManager: isConnectedToTrackerHotSpot() --> NOT connected to any Wi-Fi!
D/ConnectDeviceFragmentPresenter: connectivityBroadcastReceiver.onReceive() --> SUPPRESSING UI update (since already done)
I/ConnectDeviceFragment: updateUiFailedToConnect() --> run() delayed by: 5000
我做错了什么?我的代码在任何没有启用移动网络的设备上都能正常工作...
编辑:
我可能已经找到了根本原因:
boolean openSocketConnection() {
if (isSocketReady()) {
Log.i(TAG, "openSocketConnection() --> isSocketReady() --> return true");
return true;
}
//FIXME: Timeout if Wi-Fi AND mobile network are active --> force open socket on Wi-Fi
try {
socket = new Socket(InetAddress.getByName(SERVER_IP), SERVER_PORT);
outputStream = new DataOutputStream(socket.getOutputStream());
inputStream = new DataInputStream(socket.getInputStream());
Log.i(TAG, "openSocketConnection() --> connection opened and input/output streams created!");
return true;
} catch (IOException e) {
Log.e(TAG, "openSocketConnection() --> IOException!", e);
return false;
}
}
所以新问题是:如何在 Wi-Fi 连接上打开套接字(尽管活动网络将是移动网络)。因为我的嵌入式设备的 Wi-Fi 连接永远不会有互联网,但我需要打开一个套接字进行通信......
非常感谢任何帮助!
【问题讨论】:
标签: android sockets android-wifi android-networking wifimanager