Skip to content

Commit

Permalink
Merge pull request #2 from ThomasDem14/master
Browse files Browse the repository at this point in the history
Bug fixes found during my Master thesis + New method
  • Loading branch information
thai-dinh authored May 9, 2022
2 parents 2404a5f + e977d57 commit a2f074a
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 75 deletions.
11 changes: 11 additions & 0 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.1.1))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=C\:/Program Files/Eclipse Adoptium/jdk-17.0.2.8-hotspot
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 30
compileSdkVersion 31

defaultConfig {
minSdkVersion 21
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AdhocPlugin implements FlutterPlugin, MethodCallHandler {
private BluetoothManager bluetoothManager;
private GattServerManager gattServerManager;

private WifiAdHocManager WifiAdHocManager;
private WifiAdHocManager wifiAdHocManager;

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
Expand All @@ -41,16 +41,17 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
(BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

// Attach this plugin to the Flutter environment
methodChannel = new MethodChannel(messenger, METHOD_NAME);
methodChannel.setMethodCallHandler(this);
this.methodChannel = new MethodChannel(this.messenger, METHOD_NAME);
this.methodChannel.setMethodCallHandler(this);

// GattServerManager and BleManager (BLE)
gattServerManager = new GattServerManager(context);
bleManager = new BleManager();
this.gattServerManager = new GattServerManager(this.context);
this.gattServerManager.setupEventChannel(this.messenger);
this.bleManager = new BleManager();

// WifiAdHocManager (Wi-Fi Direct)
WifiAdHocManager = new WifiAdHocManager(context);
WifiAdHocManager.initMethodCallHandler(messenger);
this.wifiAdHocManager = new WifiAdHocManager(this.context);
this.wifiAdHocManager.initMethodCallHandler(this.messenger);
}

@Override
Expand All @@ -67,7 +68,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
break;
case "openGattServer":
gattServerManager.openGattServer(bluetoothManager, context);
gattServerManager.setupEventChannel(messenger);
break;
case "closeGattServer":
gattServerManager.closeGattServer();
Expand Down Expand Up @@ -123,7 +123,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
bleManager.stopAdvertise();
gattServerManager.closeGattServer();
WifiAdHocManager.close();
wifiAdHocManager.close();
methodChannel.setMethodCallHandler(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* Class managing the peripheral role in Bluetooth Low Energy.
*/
public class BleManager {
private static final String TAG = "[AdHocPlugin][BleManager]";
private static final String TAG = "[AdHocPlugin][Ble]";

private final BluetoothAdapter bluetoothAdapter;

private BluetoothLeAdvertiser bluetoothLeAdvertiser;
private boolean verbose;
private String initialName;
private final String initialName;

/**
* Default constructor
Expand All @@ -33,11 +33,11 @@ public BleManager() {
}

// Interface callback for notification about the discovery mode (advertisement)
private AdvertiseCallback advertiseCallback = new AdvertiseCallback() {
private final AdvertiseCallback advertiseCallback = new AdvertiseCallback() {
@Override
public void onStartFailure (int errorCode) {
if (verbose)
Log.d(TAG, "Start advertise failure: " + Integer.toString(errorCode));
Log.d(TAG, "Start advertise failure: " + errorCode);
super.onStartFailure(errorCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* Class managing the Gatt server used by Bluetooth Low Energy.
*/
public class GattServerManager {
private static final String TAG = "[AdHocPlugin][GattServer]";
private static final String TAG = "[AdHocPlugin][Gatt]";
private static final String EVENT_NAME = "ad.hoc.lib/ble.event.channel";

// Constants for communication with the Flutter platform barrier
Expand All @@ -46,13 +46,13 @@ public class GattServerManager {
private static final byte ANDROID_MTU = 126;

private boolean verbose;
private Context context;
private final Context context;

private BluetoothGattServer gattServer;
private BluetoothManager bluetoothManager;

private HashMap<String, HashMap<Integer, ByteArrayOutputStream>> data;
private HashMap<String, BluetoothDevice> mapMacDevice;
private final HashMap<String, HashMap<Integer, ByteArrayOutputStream>> data;
private final HashMap<String, BluetoothDevice> mapMacDevice;

private EventChannel eventChannel;
private MainThreadEventSink eventSink;
Expand Down Expand Up @@ -274,7 +274,7 @@ public void onReceive(Context context, Intent intent) {
};

// Interface callback for events related to the Gatt server
private BluetoothGattServerCallback bluetoothGattServerCallback = new BluetoothGattServerCallback() {
private final BluetoothGattServerCallback bluetoothGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onCharacteristicWriteRequest(
BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
Expand All @@ -285,7 +285,7 @@ public void onCharacteristicWriteRequest(
}

String mac = device.getAddress();
Integer id = new Integer(value[0]);
Integer id = (int) value[0];
byte flag = value[1];

HashMap<Integer, ByteArrayOutputStream> buffer = data.get(mac);
Expand All @@ -294,10 +294,10 @@ public void onCharacteristicWriteRequest(
byteBuffer = new ByteArrayOutputStream();
}

// Process the fragmentated data by removing the flags from data
// Process the fragmented data by removing the flags from data
try {
byteBuffer.write(Arrays.copyOfRange(value, 2, value.length));
} catch (IOException exception) {
} catch (IOException ignored) {

}

Expand Down Expand Up @@ -375,9 +375,9 @@ public void onMtuChanged(BluetoothDevice device, int mtu) {
* NOTE: The solution has been borrowed from:
* https://github.com/flutter/flutter/issues/34993
*/
private class MainThreadEventSink implements EventSink {
private EventSink eventSink;
private Handler handler;
private static class MainThreadEventSink implements EventSink {
private final EventSink eventSink;
private final Handler handler;

/**
* Default constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.WifiManager;
import android.util.Log;
import androidx.annotation.NonNull;

Expand All @@ -27,9 +26,6 @@
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import static android.net.wifi.p2p.WifiP2pManager.BUSY;
import static android.net.wifi.p2p.WifiP2pManager.ERROR;
Expand All @@ -44,21 +40,21 @@
* https://github.com/gaulthiergain/AdHocLib
*/
public class WifiAdHocManager implements MethodCallHandler {
private static final String TAG = "[AdHocPlugin][WifiAdHocManager]";
private static final String TAG = "[AdHocPlugin][Wifi]";
private static final String METHOD_NAME = "ad.hoc.lib/wifi.method.channel";
private static final String EVENT_NAME = "ad.hoc.lib/wifi.event.channel";

private boolean verbose;
private boolean registered;
private Channel channel;
private Context context;
private final Channel channel;
private final Context context;
private MethodChannel methodChannel;
private EventChannel eventChannel;
private EventSink eventSink;
private String initialName;
private String currentAdapterName;
private WifiBroadcastReceiver receiver;
private WifiP2pManager wifiP2pManager;
private final WifiP2pManager wifiP2pManager;

/**
* Default constructor
Expand Down Expand Up @@ -88,8 +84,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
result.success(isWifiEnabled());
break;
case "currentName":
final String currentName = call.arguments();
currentAdapterName = currentName;
currentAdapterName = call.arguments();
break;
case "updateDeviceName":
final String name = call.arguments();
Expand Down Expand Up @@ -154,7 +149,7 @@ public void onCancel(Object arguments) {
}

/**
* Method allowing to release the ressources used.
* Method allowing to release the resources used.
*/
public void close() {
if (verbose) Log.d(TAG, "close()");
Expand Down Expand Up @@ -236,22 +231,15 @@ private boolean updateDeviceName(String name) {
try {
Method method = wifiP2pManager.getClass().getMethod(
"setDeviceName",
new Class[] {
channel.getClass(),
String.class,
WifiP2pManager.ActionListener.class
}
);
WifiP2pManager.ActionListener.class);

method.invoke(wifiP2pManager, channel, name, null);
currentAdapterName = name;

return true;
} catch (IllegalAccessException e) {
return false;
} catch (InvocationTargetException e) {
return false;
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
return false;
}
}
Expand All @@ -260,15 +248,13 @@ private boolean updateDeviceName(String name) {
* Method allowing to reset the name of the device Wi-Fi adapter.
*/
private boolean resetDeviceName() {
return (initialName != null) ? updateDeviceName(initialName) : false;
return initialName != null && updateDeviceName(initialName);
}

/**
* Method allowing to retrieve the MAC address of the Wi-Fi Direct adapter.
*
* @return String value representing the MAC address.
*
* @throws SocketException if an I/O error occurs.
* @return String value representing the MAC address, empty String if an I/O error occurs.
*/
private String getMacAddress() {
try {
Expand All @@ -294,7 +280,7 @@ private String getMacAddress() {
}
} catch (SocketException exception) {
if (verbose)
Log.d(TAG, "Error while fetching MAC address" + exception.toString());
Log.d(TAG, "Error while fetching MAC address" + exception);
}

return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.Network;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pInfo;
Expand All @@ -26,7 +22,7 @@
* Class defining a BroadcastReceiver that notifies of Wi-Fi Direct events.
*/
public class WifiBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "[AdHocPlugin][BroadcastReceiver]";
private static final String TAG = "[AdHocPlugin][BR]";

// Constants for communication with the Flutter platform barrier
private static final byte ANDROID_DISCOVERY = 120;
Expand All @@ -35,9 +31,9 @@ public class WifiBroadcastReceiver extends BroadcastReceiver {
private static final byte ANDROID_CHANGES = 123;

private boolean verbose;
private Channel channel;
private EventSink eventSink;
private WifiP2pManager wifiP2pManager;
private final Channel channel;
private final EventSink eventSink;
private final WifiP2pManager wifiP2pManager;

/**
* Default constructor
Expand Down Expand Up @@ -110,8 +106,8 @@ public void onReceive(Context context, Intent intent) {
case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION: {
if (verbose) Log.d(TAG, "onReceive(): THIS_DEVICE_CHANGED");
// Device information changed
WifiP2pDevice wifiP2pDevice =
(WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
WifiP2pDevice wifiP2pDevice =
intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);

mapInfoValue.put("type", ANDROID_CHANGES);
mapInfoValue.put("name", wifiP2pDevice.deviceName);
Expand All @@ -128,7 +124,7 @@ public void onReceive(Context context, Intent intent) {
}

// Interface for callback invocation when the peer list is available
private PeerListListener peerListListener = new PeerListListener() {
private final PeerListListener peerListListener = new PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
if (verbose) Log.d(TAG, "onPeersAvailable()");
Expand All @@ -152,7 +148,7 @@ public void onPeersAvailable(WifiP2pDeviceList peerList) {
};

// Interface for callback invocation when the connection info is available
private WifiP2pManager.ConnectionInfoListener connectionInfoListener = new WifiP2pManager.ConnectionInfoListener() {
private final WifiP2pManager.ConnectionInfoListener connectionInfoListener = new WifiP2pManager.ConnectionInfoListener() {
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
if (verbose) Log.d(TAG, "onConnectionInfoAvailable()");
Expand Down
4 changes: 2 additions & 2 deletions example/android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.1.1))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
java.home=C\:/Program Files/Eclipse Adoptium/jdk-17.0.2.8-hotspot
jvm.arguments=
offline.mode=false
override.workspace.settings=true
Expand Down
Loading

0 comments on commit a2f074a

Please sign in to comment.