Skip to content

Commit 0c4d773

Browse files
committed
version for the video
1 parent 6b43834 commit 0c4d773

File tree

10 files changed

+298
-176
lines changed

10 files changed

+298
-176
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
4141
<uses-permission android:name="android.permission.INTERNET" />
4242
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
43+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
4344

4445
<!-- ASAPService needs to work with Bluetooth -->
4546
<uses-permission android:name="android.permission.BLUETOOTH" />

app/src/main/java/net/sharksystem/asap/android/apps/ASAPAndroidPeer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ private ASAPAndroidPeer(Collection<CharSequence> supportedFormats,
227227
this.requiredPermissions.add(Manifest.permission.BLUETOOTH);
228228
this.requiredPermissions.add(Manifest.permission.BLUETOOTH_ADMIN);
229229
this.requiredPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
230+
this.requiredPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
230231

231232
// check for write permissions
232233
Log.d(this.getLogStart(), "ask for required permissions");

app/src/main/java/net/sharksystem/asap/android/bluetooth/BluetoothEngine.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import android.content.IntentFilter;
1010
import android.util.Log;
1111

12-
import net.sharksystem.asap.ASAPEncounterManager;
1312
import net.sharksystem.asap.ASAPException;
1413
import net.sharksystem.asap.EncounterConnectionType;
1514
import net.sharksystem.asap.android.service.MacLayerEngine;
@@ -455,7 +454,7 @@ void handleBTSocket(BluetoothSocket socket, boolean initiator) throws IOExceptio
455454
StreamPair streamPair = StreamPairImpl.getStreamPairWithEndpointAddress(
456455
socket.getInputStream(), socket.getOutputStream(), remoteMacAddress);
457456

458-
this.getAsapService().getASAPEncounterManager().handleEncounter(
457+
this.getASAPService().getASAPEncounterManager().handleEncounter(
459458
streamPair, EncounterConnectionType.AD_HOC_LAYER_2_NETWORK, initiator);
460459
}
461460

app/src/main/java/net/sharksystem/asap/android/service/ASAPService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ void startWifiDirect() {
258258

259259
void stopWifiDirect() {
260260
Log.d("ASAPService", "stop wifi p2p");
261-
WifiP2PEngine ASAPWifiP2PEngine = WifiP2PEngine.getASAPWifiP2PEngine();
261+
WifiP2PEngine ASAPWifiP2PEngine =
262+
WifiP2PEngine.getASAPWifiP2PEngine(this, this);
262263
if(ASAPWifiP2PEngine != null) {
263264
ASAPWifiP2PEngine.stop();
264265
}

app/src/main/java/net/sharksystem/asap/android/service/MacLayerEngine.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package net.sharksystem.asap.android.service;
22

33
import android.content.Context;
4-
import android.net.wifi.WifiInfo;
5-
import android.net.wifi.WifiManager;
4+
import android.content.pm.PackageManager;
65
import android.util.Log;
76

7+
import androidx.core.app.ActivityCompat;
8+
89
import net.sharksystem.asap.ASAPException;
910
import net.sharksystem.asap.protocol.ASAPConnection;
1011

@@ -49,7 +50,7 @@ protected Context getContext() {
4950
return this.context;
5051
}
5152

52-
protected ASAPService getAsapService() {
53+
public ASAPService getASAPService() {
5354
return this.asapService;
5455
}
5556

@@ -70,6 +71,18 @@ private String getLogStart() {
7071
return "ASAPMacLayerEngine";
7172
}
7273

74+
public boolean permissionCheck(String requiredPermission) {
75+
if (ActivityCompat.checkSelfPermission(this.asapService, requiredPermission)
76+
!= PackageManager.PERMISSION_GRANTED) {
77+
Log.e(this.getLogStart(),
78+
"failure: required permission not granted \n" +
79+
"check Android ASAP peer init process and \n" +
80+
"check manifest");
81+
return false;
82+
}
83+
return true;
84+
}
85+
7386
/**
7487
* TODO: do we need this? I doubt it (thsc)
7588
* It can be called to check whether open connection are still running. It's a good idea for
@@ -151,7 +164,7 @@ protected void launchASAPConnection(
151164
// TestConnectionHandler testConnectionHandler = new TestConnectionHandler(this.is, this.os);
152165
// testConnectionHandler.start();
153166
this.asapConnections.put(address,
154-
this.getAsapService().getASAPPeer().handleConnection(inputStream, outputStream));
167+
this.getASAPService().getASAPPeer().handleConnection(inputStream, outputStream));
155168

156169
} catch (IOException | ASAPException e) {
157170
Log.d(this.getLogStart(), "while launching asap connection: " + e.getLocalizedMessage());

app/src/main/java/net/sharksystem/asap/android/wifidirect/WifiDirectBroadcastReceiver.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package net.sharksystem.asap.android.wifidirect;
22

3+
import android.Manifest;
4+
import android.annotation.SuppressLint;
35
import android.content.BroadcastReceiver;
46
import android.content.Context;
57
import android.content.Intent;
8+
import android.content.pm.PackageManager;
69
import android.net.NetworkInfo;
710
import android.net.wifi.p2p.WifiP2pDevice;
811
import android.net.wifi.p2p.WifiP2pManager;
912
import android.util.Log;
1013

14+
import androidx.core.app.ActivityCompat;
15+
1116
class WifiDirectBroadcastReceiver extends BroadcastReceiver {
1217
// https://developer.android.com/guide/topics/connectivity/wifip2p#java
1318

@@ -16,25 +21,26 @@ class WifiDirectBroadcastReceiver extends BroadcastReceiver {
1621
private final Context context;
1722
private final WifiP2pManager.PeerListListener peerListListener;
1823
private final WifiP2pManager.ConnectionInfoListener connectionInfoListener;
19-
private final WifiP2PEngine aaspWifiP2PEngine;
24+
private final WifiP2PEngine asapWifiP2PEngine;
2025

2126
public WifiDirectBroadcastReceiver(
22-
WifiP2PEngine aaspWifiP2PEngine,
27+
WifiP2PEngine asapWifiP2PEngine,
2328
WifiP2pManager manager,
2429
WifiP2pManager.Channel channel,
2530
Context context,
2631
WifiP2pManager.PeerListListener peerListListener,
2732
WifiP2pManager.ConnectionInfoListener connectionInfoListener
2833
) {
2934
super();
30-
this.aaspWifiP2PEngine = aaspWifiP2PEngine;
35+
this.asapWifiP2PEngine = asapWifiP2PEngine;
3136
this.mManager = manager;
3237
this.mChannel = channel;
3338
this.context = context;
3439
this.peerListListener = peerListListener;
3540
this.connectionInfoListener = connectionInfoListener;
3641
}
3742

43+
@SuppressLint("MissingPermission")
3844
@Override
3945
public void onReceive(Context context, Intent intent) {
4046
// https://developer.android.com/guide/topics/connectivity/wifip2p#java
@@ -49,19 +55,22 @@ public void onReceive(Context context, Intent intent) {
4955
Log.d("Wifi_BR", "BL: wifi p2p enabled");
5056

5157
// discoverPeers peers
52-
this.aaspWifiP2PEngine.discoverPeers();
58+
this.asapWifiP2PEngine.discoverPeers();
5359
} else {
5460
// Wi-Fi P2P is not enabled
55-
Log.d("Wifi_BR","BL: wifi p2p not enabled");
61+
Log.d("Wifi_BR", "BL: wifi p2p not enabled");
5662
}
5763

5864
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
5965
// Call WifiP2pManager.requestPeers() to get a list of current peers
6066
// that event is a result of a previous discoverPeers peers
6167

6268
Log.d("Wifi_BR", "p2p peers changed");
63-
// call for a list of peers
6469
if (mManager != null) {
70+
if(!this.asapWifiP2PEngine.permissionCheck(Manifest.permission.ACCESS_FINE_LOCATION)) {
71+
return;
72+
}
73+
// ask for a list of new peers - send to PeerListListener
6574
mManager.requestPeers(mChannel, this.peerListListener);
6675
}
6776

@@ -94,4 +103,8 @@ public void onReceive(Context context, Intent intent) {
94103
// TODO do something useful with that information
95104
}
96105
}
106+
107+
private String getLogStart() {
108+
return this.getClass().getSimpleName();
109+
}
97110
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.sharksystem.asap.android.wifidirect;
2+
3+
import android.net.wifi.p2p.WifiP2pManager;
4+
5+
public class WifiP2PChannelListener implements WifiP2pManager.ChannelListener {
6+
private final WifiP2PEngine wifiP2PEngine;
7+
8+
public WifiP2PChannelListener(WifiP2PEngine wifiP2PEngine) {
9+
this.wifiP2PEngine = wifiP2PEngine;
10+
}
11+
12+
@Override
13+
public void onChannelDisconnected() {
14+
15+
}
16+
}

0 commit comments

Comments
 (0)