|
| 1 | +package com.scrappers.superiorExtendedEngine.LANHelper; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.content.BroadcastReceiver; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.IntentFilter; |
| 8 | +import android.net.wifi.WifiInfo; |
| 9 | +import android.net.wifi.WifiManager; |
| 10 | +import android.os.Build; |
| 11 | +import android.os.Bundle; |
| 12 | +import android.os.StrictMode; |
| 13 | +import android.widget.Toast; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.net.InetAddress; |
| 17 | +import java.net.InetSocketAddress; |
| 18 | +import java.net.ServerSocket; |
| 19 | +import java.net.Socket; |
| 20 | +import java.net.UnknownHostException; |
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import java.nio.ByteOrder; |
| 23 | +import java.util.concurrent.Callable; |
| 24 | +import java.util.concurrent.Executors; |
| 25 | +import java.util.concurrent.ScheduledExecutorService; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +import androidx.annotation.NonNull; |
| 29 | +import androidx.annotation.Nullable; |
| 30 | +import androidx.appcompat.app.AppCompatActivity; |
| 31 | + |
| 32 | +public class LanActivity extends AppCompatActivity { |
| 33 | + private static final int ENABLE_WIFI_REQUEST=1; |
| 34 | + private static final int SCAN_INTENT = 2; |
| 35 | + private WifiManager wifiManager; |
| 36 | + private OnAPScanFinished onAPScanFinished; |
| 37 | + private OnConnectionEstablished onConnectionEstablished; |
| 38 | + public final ScheduledExecutorService executorService= Executors.newScheduledThreadPool(2); |
| 39 | + private final WifiScanBroadCast wifiScanBroadCast= new WifiScanBroadCast(); |
| 40 | + private final LanClientSide lanClientSide=new LanClientSide(); |
| 41 | + private final LanServerSide lanServerSide= new LanServerSide(); |
| 42 | + @Override |
| 43 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 44 | + super.onCreate(savedInstanceState); |
| 45 | + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() |
| 46 | + .permitAll().build(); |
| 47 | + StrictMode.setThreadPolicy(policy); |
| 48 | + } |
| 49 | + |
| 50 | + protected void initializeLAN(){ |
| 51 | + wifiManager= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
| 52 | + /*request ACCESS_FINE_LOCATION permission for over L devices*/ |
| 53 | + if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ){ |
| 54 | + this.requestPermissions(new String[]{Manifest.permission.CHANGE_WIFI_STATE,Manifest.permission.ACCESS_WIFI_STATE |
| 55 | + ,Manifest.permission.ACCESS_FINE_LOCATION}, ENABLE_WIFI_REQUEST); |
| 56 | + }else { |
| 57 | + if(wifiManager.getWifiState()!=WifiManager.WIFI_STATE_ENABLED || !wifiManager.isWifiEnabled()){ |
| 58 | + wifiManager.setWifiEnabled(true); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + protected void startWifiScan(){ |
| 64 | + if(wifiManager==null){ |
| 65 | + return; |
| 66 | + } |
| 67 | + if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ |
| 68 | + /*Listen for discovery events change*/ |
| 69 | + IntentFilter intentFilter = new IntentFilter(); |
| 70 | + intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); |
| 71 | + this.registerReceiver(wifiScanBroadCast, intentFilter); |
| 72 | + wifiManager.startScan(); |
| 73 | + }else{ |
| 74 | + Toast.makeText(getApplicationContext(),"Device doesn't support wifi scan through apps !",Toast.LENGTH_LONG).show(); |
| 75 | + } |
| 76 | + } |
| 77 | + @Override |
| 78 | + protected void onPostCreate(@Nullable Bundle savedInstanceState) { |
| 79 | + super.onPostCreate(savedInstanceState); |
| 80 | + initializeLAN(); |
| 81 | + } |
| 82 | + |
| 83 | + private class WifiScanBroadCast extends BroadcastReceiver { |
| 84 | + @Override |
| 85 | + public void onReceive(Context context, Intent intent) { |
| 86 | + if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ){ |
| 87 | + if(intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false) |
| 88 | + && intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ){ |
| 89 | + if(onAPScanFinished!=null){ |
| 90 | + onAPScanFinished.success(wifiManager.getScanResults()); |
| 91 | + } |
| 92 | + }else{ |
| 93 | + if(onAPScanFinished!=null){ |
| 94 | + onAPScanFinished.failure(wifiManager.getScanResults()); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + public void registerAsClientSide(int port,long delay) throws IOException { |
| 101 | + executorService.schedule(lanClientSide.registerLanClientSide(port),delay, TimeUnit.MILLISECONDS); |
| 102 | + } |
| 103 | + public void registerAsServerSide(int port,int bucketCarries,long delay) throws IOException { |
| 104 | + executorService.schedule(lanServerSide.registerLanServerSide(port,bucketCarries),delay, TimeUnit.MILLISECONDS); |
| 105 | + } |
| 106 | + public WifiManager getWifiManager() { |
| 107 | + return wifiManager; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected void onDestroy() { |
| 112 | + super.onDestroy(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected void onStop() { |
| 117 | + super.onStop(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 122 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 123 | + if(requestCode==ENABLE_WIFI_REQUEST){ |
| 124 | + wifiManager.setWifiEnabled(true); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private class LanClientSide implements Callable<Socket>{ |
| 129 | + private Socket dataSocket; |
| 130 | + private InetSocketAddress socketAddress; |
| 131 | + private int port; |
| 132 | + public Callable<Socket> registerLanClientSide(int port) throws IOException { |
| 133 | +// /*define the Socket address of the current AP*/ |
| 134 | +// InetAddress inetAddress=InetAddress.getLocalHost(); |
| 135 | +// socketAddress=new InetSocketAddress(inetAddress, port); |
| 136 | + this.port=port; |
| 137 | + return this; |
| 138 | + } |
| 139 | + @Override |
| 140 | + public Socket call() { |
| 141 | + unregisterReceiver(wifiScanBroadCast); |
| 142 | + try { |
| 143 | + /*bind to the connected wifi(AP) address*/ |
| 144 | +// dataSocket.bind(socketAddress); |
| 145 | + Toast.makeText(getApplicationContext(),getLocalIpAddress(),Toast.LENGTH_LONG).show(); |
| 146 | + dataSocket =new Socket(getLocalIpAddress(),port); |
| 147 | + //connection-success |
| 148 | + if ( onConnectionEstablished != null ){ |
| 149 | + onConnectionEstablished.connectionSuccess(dataSocket); |
| 150 | + } |
| 151 | + |
| 152 | + } catch (Exception e) { |
| 153 | + e.printStackTrace(); |
| 154 | + //connection-failure |
| 155 | + if(onConnectionEstablished!=null){ |
| 156 | + onConnectionEstablished.connectionFailed(dataSocket); |
| 157 | + } |
| 158 | + } |
| 159 | + return dataSocket; |
| 160 | + } |
| 161 | + private String getLocalIpAddress() throws UnknownHostException { |
| 162 | + WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); |
| 163 | + assert wifiManager != null; |
| 164 | + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
| 165 | + int ipInt = wifiInfo.getIpAddress(); |
| 166 | + return InetAddress.getByAddress(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(ipInt).array()).getHostAddress(); |
| 167 | + } |
| 168 | + public void cancelOperation(){ |
| 169 | + try { |
| 170 | + dataSocket.close(); |
| 171 | + } catch (IOException e) { |
| 172 | + e.printStackTrace(); |
| 173 | + } |
| 174 | + } |
| 175 | + public InetSocketAddress getSocketAddress() { |
| 176 | + return socketAddress; |
| 177 | + } |
| 178 | + public Socket getDataSocket() { |
| 179 | + return dataSocket; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private class LanServerSide implements Callable<ServerSocket>{ |
| 184 | + private ServerSocket serverSocket; |
| 185 | + private Socket dataSocket; |
| 186 | + private boolean cancelOperation=false; |
| 187 | + public Callable<ServerSocket> registerLanServerSide(int port,int bucketCarriers) throws IOException { |
| 188 | + serverSocket=new ServerSocket(port,bucketCarriers); |
| 189 | + return this; |
| 190 | + } |
| 191 | + @Override |
| 192 | + public ServerSocket call() { |
| 193 | + connectWhenRequested(); |
| 194 | + return null; |
| 195 | + } |
| 196 | + private void connectWhenRequested(){ |
| 197 | + do { |
| 198 | + try { |
| 199 | + dataSocket = serverSocket.accept(); |
| 200 | + //connection-success |
| 201 | + if ( onConnectionEstablished != null ){ |
| 202 | + onConnectionEstablished.connectionSuccess(dataSocket); |
| 203 | + } |
| 204 | + } catch (IOException e) { |
| 205 | + e.printStackTrace(); |
| 206 | + //connection-failed |
| 207 | + if ( onConnectionEstablished != null ){ |
| 208 | + onConnectionEstablished.connectionFailed(dataSocket); |
| 209 | + } |
| 210 | + } |
| 211 | + } while (!isCancelOperation()); |
| 212 | + } |
| 213 | + public void cancelOperation(){ |
| 214 | + try { |
| 215 | + cancelOperation=true; |
| 216 | + dataSocket.close(); |
| 217 | + serverSocket.close(); |
| 218 | + } catch (IOException e) { |
| 219 | + e.printStackTrace(); |
| 220 | + } |
| 221 | + } |
| 222 | + public boolean isCancelOperation() { |
| 223 | + return cancelOperation; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + public LanClientSide getLanClientSide() { |
| 228 | + return lanClientSide; |
| 229 | + } |
| 230 | + |
| 231 | + public void setOnConnectionEstablished(OnConnectionEstablished onConnectionEstablished) { |
| 232 | + this.onConnectionEstablished = onConnectionEstablished; |
| 233 | + } |
| 234 | + |
| 235 | + public void setOnAPScanFinished(OnAPScanFinished onAPScanFinished) { |
| 236 | + this.onAPScanFinished = onAPScanFinished; |
| 237 | + } |
| 238 | +} |
0 commit comments