Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取 wifi 列表功能 #42

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Expand Down Expand Up @@ -57,6 +58,10 @@
<activity
android:name=".AppListActivity"
android:exported="true" />

<activity
android:name=".WifiListActivity"
android:exported="true" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/cloud/sonic/android/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package org.cloud.sonic.android;

import android.app.Activity;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import org.cloud.sonic.android.wifi.SonicWifiManager;
import org.cloud.sonic.android.wifi.wifi_scan.ScanResultsListener;

import java.util.List;

public class MainActivity extends Activity {

Expand Down
54 changes: 18 additions & 36 deletions app/src/main/java/org/cloud/sonic/android/SonicTouchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class SonicTouchService extends Thread {
private static final String SOCKET = "sonictouchservice";
private static final int DEFAULT_MAX_CONTACTS = 10;
private static final int DEFAULT_MAX_PRESSURE = 0;
private static final int MAX_POINTERS = 2;
private final int width;
private final int height;
private LocalServerSocket serverSocket;
Expand Down Expand Up @@ -256,47 +255,30 @@ public SonicTouchService(int width, int height, Handler handler) {
this.handler = handler;
inputManager = new InputManagerWrapper();
windowManager = new WindowManagerWrapper();
initPointers();
MotionEvent.PointerProperties pointerProps0 = new MotionEvent.PointerProperties();
pointerProps0.id = 0;
pointerProps0.toolType = MotionEvent.TOOL_TYPE_FINGER;
MotionEvent.PointerProperties pointerProps1 = new MotionEvent.PointerProperties();
pointerProps1.id = 1;
pointerProps1.toolType = MotionEvent.TOOL_TYPE_FINGER;
pointerProperties[0] = pointerProps0;
pointerProperties[1] = pointerProps1;

// MotionEvent.PointerProperties pointerProps0 = new MotionEvent.PointerProperties();
// pointerProps0.id = 0;
// pointerProps0.toolType = MotionEvent.TOOL_TYPE_FINGER;
// MotionEvent.PointerProperties pointerProps1 = new MotionEvent.PointerProperties();
// pointerProps1.id = 1;
// pointerProps1.toolType = MotionEvent.TOOL_TYPE_FINGER;
// pointerProperties[0] = pointerProps0;
// pointerProperties[1] = pointerProps1;

// MotionEvent.PointerCoords pointerCoords0 = new MotionEvent.PointerCoords();
// MotionEvent.PointerCoords pointerCoords1 = new MotionEvent.PointerCoords();
// pointerCoords0.orientation = 0;
// pointerCoords0.pressure = 1;
// pointerCoords0.size = 1;
// pointerCoords1.orientation = 0;
// pointerCoords1.pressure = 1;
// pointerCoords1.size = 1;
// pointerCoords[0] = pointerCoords0;
// pointerCoords[1] = pointerCoords1;
MotionEvent.PointerCoords pointerCoords0 = new MotionEvent.PointerCoords();
MotionEvent.PointerCoords pointerCoords1 = new MotionEvent.PointerCoords();
pointerCoords0.orientation = 0;
pointerCoords0.pressure = 1;
pointerCoords0.size = 1;
pointerCoords1.orientation = 0;
pointerCoords1.pressure = 1;
pointerCoords1.size = 1;
pointerCoords[0] = pointerCoords0;
pointerCoords[1] = pointerCoords1;

events[0] = new PointerEvent();
events[1] = new PointerEvent();
}

public void initPointers(){
for (int i=0; i < MAX_POINTERS; i++){
MotionEvent.PointerProperties props = new MotionEvent.PointerProperties();
props.toolType = MotionEvent.TOOL_TYPE_FINGER;

MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
coords.orientation = 0;
coords.size = 0;

pointerProperties[i] = props;
pointerCoords[i] = coords;
}
}


@Override
public void run() {
try {
Expand Down
132 changes: 132 additions & 0 deletions app/src/main/java/org/cloud/sonic/android/WifiListActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.cloud.sonic.android;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import org.cloud.sonic.android.models.SonicWifiInfo;
import org.cloud.sonic.android.models.WifiPacket;
import org.cloud.sonic.android.wifi.SonicWifiManager;
import org.cloud.sonic.android.wifi.wifi_scan.ScanResultsListener;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @author Eason
* get App list
*/
public class WifiListActivity extends Activity {
private static final String TAG = "sonicwifilistactivity";
private static final String SOCKET = "sonicawifilistservice";
private LocalServerSocket serverSocket;

/**
* 数据缓冲大小,因为无法关闭Nagle,所以该参数没有意义
*/
private static final int BUFFER_SIZE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "created");
}

@Override
protected void onStart() {
super.onStart();
new Thread(() -> {
try {
Log.i(TAG, String.format("creating socket %s", SOCKET));
serverSocket = new LocalServerSocket(SOCKET);
} catch (IOException e) {
e.printStackTrace();
return;
}
manageClientConnection();
try {
serverSocket.close();
Log.i(TAG, "client closed");
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}

private void manageClientConnection() {
Log.i(TAG, String.format("Listening on %s", SOCKET));
LocalSocket clientSocket;
try {
clientSocket = serverSocket.accept();
//设置缓冲大小
clientSocket.setReceiveBufferSize(BUFFER_SIZE);
clientSocket.setSendBufferSize(BUFFER_SIZE);
Log.d(TAG, "client connected");
OutputStream outputStream = clientSocket.getOutputStream();
getAllWifi(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}

@SuppressLint("WrongConstant")
public void getAllWifi(OutputStream outputStream) {
List<PackageInfo> packages = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
SonicWifiManager.withContext(this)
.scanWifi(new ScanResultsListener() {
@Override
public void onScanResults(@NonNull List<ScanResult> scanResults) {
WifiPacket wifiPacket = new WifiPacket();
wifiPacket.setConnectWifi(SonicWifiManager.withContext(WifiListActivity.this)
.isWifiConnected());
WifiInfo wifiInfo = SonicWifiManager.withContext(WifiListActivity.this)
.getWifiInfo();
wifiPacket.setConnectedWifi(SonicWifiInfo.transform(wifiInfo));
List<SonicWifiInfo> infos = new ArrayList<>();
for (ScanResult scresult:scanResults){
infos.add(SonicWifiInfo.transform(scresult));
}
wifiPacket.setWifiResults(infos);
try {
byte[] dataBytes = JSON.toJSONString(wifiPacket).getBytes();
// 数据长度转成二进制,存入byte[32]
byte[] lengthBytes = new byte[32];
String binStr = Integer.toBinaryString(dataBytes.length).trim();
char[] binArray = binStr.toCharArray();
for (int x = binArray.length-1, y = lengthBytes.length-1; x >= 0; x--, y--) {
try {
lengthBytes[y] = Byte.parseByte(binArray[x]+"");
} catch (Exception e) {
Log.e(TAG, String.format("char转byte失败,char为:【%s】", binArray[x] + ""));
}
}
// 先发送长度
outputStream.write(lengthBytes);
outputStream.flush();

// 再发送数据
outputStream.write(dataBytes);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}

}
}).start();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.cloud.sonic.android.models;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.support.v4.app.ActivityCompat;

public class SonicWifiInfo {
String SSID = "";
String BSSID = "";
String capabilities = "";
String ipAddress = "";
String macAddress = "";

public String getSSID() {
return SSID;
}

public void setSSID(String SSID) {
this.SSID = SSID;
}

public String getBSSID() {
return BSSID;
}

public void setBSSID(String BSSID) {
this.BSSID = BSSID;
}

public String getCapabilities() {
return capabilities;
}

public void setCapabilities(String capabilities) {
this.capabilities = capabilities;
}

public String getIpAddress() {
return ipAddress;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}

public String getMacAddress() {
return macAddress;
}

public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}

@SuppressLint("MissingPermission")
public static SonicWifiInfo transform(WifiInfo wifiInfo) {
SonicWifiInfo info = new SonicWifiInfo();
info.setSSID(wifiInfo.getSSID());
info.setBSSID(wifiInfo.getBSSID());
info.setIpAddress(wifiInfo.getIpAddress() + "");
info.setMacAddress(wifiInfo.getMacAddress());
return info;
}

@SuppressLint("MissingPermission")
public static SonicWifiInfo transform(ScanResult scanResult) {
SonicWifiInfo info = new SonicWifiInfo();
info.setSSID(scanResult.SSID);
info.setBSSID(scanResult.BSSID);
info.setCapabilities(scanResult.capabilities);
return info;
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/org/cloud/sonic/android/models/WifiPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.cloud.sonic.android.models;

import java.util.List;

public class WifiPacket {
boolean isConnectWifi;
SonicWifiInfo connectedWifi;
List<SonicWifiInfo> wifiResults;

public boolean isConnectWifi() {
return isConnectWifi;
}

public void setConnectWifi(boolean connectWifi) {
isConnectWifi = connectWifi;
}

public SonicWifiInfo getConnectedWifi() {
return connectedWifi;
}

public void setConnectedWifi(SonicWifiInfo connectedWifi) {
this.connectedWifi = connectedWifi;
}

public List<SonicWifiInfo> getWifiResults() {
return wifiResults;
}

public void setWifiResults(List<SonicWifiInfo> wifiResults) {
this.wifiResults = wifiResults;
}
}

Loading