ip地址修改
This commit is contained in:
@@ -48,8 +48,8 @@ public class Main extends Service {
|
||||
private final String TAG = "PineappleService";
|
||||
|
||||
// ================= UDP服务 =================
|
||||
// 本机IP地址集合
|
||||
private Set<String> localIPs = new HashSet<>();
|
||||
// ip地址
|
||||
private String localIp = "0.0.0.0";
|
||||
// UDP监听端口
|
||||
private int localPort = 9999;
|
||||
private DatagramSocket udpSocket;
|
||||
@@ -60,7 +60,7 @@ public class Main extends Service {
|
||||
private final ExecutorService networkExecutor = Executors.newFixedThreadPool(2);
|
||||
|
||||
// 设备状态
|
||||
private DeviceInfo deviceInfo = new DeviceInfo("0.0.0.0", "未知序列号", 0, 0, "未知游戏");
|
||||
private DeviceInfo deviceInfo = null;
|
||||
|
||||
// 包名映射
|
||||
private Map<String, String> packageNamesMap = new HashMap<>();
|
||||
@@ -125,9 +125,11 @@ public class Main extends Service {
|
||||
});
|
||||
|
||||
serviceContext = this;
|
||||
|
||||
deviceInfo = new DeviceInfo("0.0.0.0", "未知序列号", 0, 0, "未知游戏");
|
||||
// 初始化本机IP地址
|
||||
initLocalIPs();
|
||||
String ip = getIpAddressFromPico();
|
||||
localIp = ip;
|
||||
deviceInfo.ip = ip;
|
||||
|
||||
// 创建通知渠道
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
@@ -167,37 +169,49 @@ public class Main extends Service {
|
||||
|
||||
// 清理资源
|
||||
isRunning = false;
|
||||
httpServerRunning = false;
|
||||
|
||||
// 关闭网络执行器
|
||||
networkExecutor.shutdownNow();
|
||||
|
||||
// 关闭UDP socket
|
||||
if (udpSocket != null && !udpSocket.isClosed()) {
|
||||
udpSocket.close();
|
||||
}
|
||||
|
||||
// 关闭HTTP服务器socket
|
||||
try {
|
||||
if (httpServerSocket != null && !httpServerSocket.isClosed()) {
|
||||
httpServerSocket.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "关闭HTTP服务器socket时出错: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 关闭HTTP执行器
|
||||
if (httpExecutor != null) {
|
||||
httpExecutor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化本机IP地址
|
||||
private void initLocalIPs() {
|
||||
private String getIpAddressFromPico() {
|
||||
String ip = "0.0.0.0";
|
||||
try {
|
||||
localIPs.clear();
|
||||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (interfaces.hasMoreElements()) {
|
||||
NetworkInterface networkInterface = interfaces.nextElement();
|
||||
if (networkInterface.isUp() && !networkInterface.isLoopback()) {
|
||||
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
InetAddress address = addresses.nextElement();
|
||||
localIPs.add(address.getHostAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.i(TAG, "本机IP地址: " + localIPs.toString());
|
||||
} catch (SocketException e) {
|
||||
Log.e(TAG, "获取本机IP地址失败: " + e.getMessage());
|
||||
// 尝试通过Pico的系统信息服务获取IP地址
|
||||
ip = toBServiceHelper.getServiceBinder().pbsStateGetDeviceInfo(
|
||||
PBS_SystemInfoEnum.DEVICE_IP, 0);
|
||||
Log.i(TAG, "通过Pico服务获取到IP地址: " + ip);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "通过Pico服务获取IP失败: " + e.getMessage());
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
// 修改 isLocalAddress 方法为 isLocalMessage,增加端口判断
|
||||
private boolean isLocalMessage(InetAddress address, int port) {
|
||||
// 只有当地址是本机IP且端口是8888时才屏蔽
|
||||
return localIPs.contains(address.getHostAddress()) && port == localPort;
|
||||
// return localIPs.contains(address.getHostAddress()) && port == localPort;
|
||||
return localIp.equals(address.getHostAddress()) && port == localPort;
|
||||
}
|
||||
|
||||
// 根据包名获取应用名称
|
||||
@@ -497,6 +511,10 @@ public class Main extends Service {
|
||||
.getLaunchIntentForPackage(packageName);
|
||||
|
||||
if (launchIntent != null) {
|
||||
// 添加传递给目标应用的参数
|
||||
launchIntent.putExtra("service_ip", deviceInfo.ip);
|
||||
launchIntent.putExtra("service_port", httpPort);
|
||||
|
||||
launchIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
launchIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
launchIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
|
||||
@@ -562,20 +580,7 @@ public class Main extends Service {
|
||||
|
||||
private String getDeviceInfoAsJson() {
|
||||
try {
|
||||
// 更新数据
|
||||
StringBuilder ipv4Addresses = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (String ip : localIPs) {
|
||||
// 判断是否为 IPv4 地址(简单判断不包含冒号即为 IPv4)
|
||||
if (!ip.contains(":")) {
|
||||
if (!first) {
|
||||
ipv4Addresses.append(", ");
|
||||
}
|
||||
ipv4Addresses.append(ip);
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
deviceInfo.ip = ipv4Addresses.toString();
|
||||
deviceInfo.ip = getIpAddressFromPico();
|
||||
try {
|
||||
deviceInfo.sn = toBServiceHelper.getServiceBinder().pbsStateGetDeviceInfo(
|
||||
PBS_SystemInfoEnum.EQUIPMENT_SN,
|
||||
|
||||
Reference in New Issue
Block a user