Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from qwales1/discovery-change-events
Browse files Browse the repository at this point in the history
Adds discovery change events
  • Loading branch information
devtronic committed Dec 8, 2019
2 parents d9526f2 + f50e78e commit f958165
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
// Handle discovered peers
}));
_subscriptions.add(FlutterP2p.wifiEvents.discoveryChange.listen((change) {
// Handle discovery state changes
}));
FlutterP2p.register(); // Register to the native events which are send to the streams above
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class FlutterP2pPlugin(private val registrar: Registrar
private const val CH_PEERS_CHANGE = "bc/peers-change"
private const val CH_CON_CHANGE = "bc/connection-change"
private const val CH_DEVICE_CHANGE = "bc/this-device-change"
private const val CH_DISCOVERY_CHANGE = "bc/discovery-change"
private const val CH_SOCKET_READ = "socket/read"
val config: Config = Config()

Expand All @@ -73,6 +74,7 @@ class FlutterP2pPlugin(private val registrar: Registrar
eventPool.register(CH_CON_CHANGE)
eventPool.register(CH_DEVICE_CHANGE)
eventPool.register(CH_SOCKET_READ)
eventPool.register(CH_DISCOVERY_CHANGE)

socketPool = SocketPool(eventPool.getHandler(CH_SOCKET_READ))
}
Expand All @@ -87,6 +89,8 @@ class FlutterP2pPlugin(private val registrar: Registrar
addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
// Indicates this device'base details have changed.
addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
// Indicates the state of peer discovery has changed
addAction(WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION)
}
}

Expand Down Expand Up @@ -136,7 +140,8 @@ class FlutterP2pPlugin(private val registrar: Registrar
eventPool.getHandler(CH_STATE_CHANGE).sink,
eventPool.getHandler(CH_PEERS_CHANGE).sink,
eventPool.getHandler(CH_CON_CHANGE).sink,
eventPool.getHandler(CH_DEVICE_CHANGE).sink
eventPool.getHandler(CH_DEVICE_CHANGE).sink,
eventPool.getHandler(CH_DISCOVERY_CHANGE).sink
)
registrar.context().registerReceiver(receiver, intentFilter)
result.success(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package de.mintware.flutter_p2p.utility
import android.net.NetworkInfo
import android.net.wifi.p2p.WifiP2pDevice
import android.net.wifi.p2p.WifiP2pInfo
import android.net.wifi.p2p.WifiP2pManager
import com.google.protobuf.ByteString
import de.mintware.flutter_p2p.Protos

Expand All @@ -24,6 +25,12 @@ class ProtoHelper {
.build()
}

fun create(discoveryState: Int) : Protos.DiscoveryStateChange {
return Protos.DiscoveryStateChange.newBuilder()
.setIsDiscovering(discoveryState != WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED)
.build();
}

fun create(device: WifiP2pDevice): Protos.WifiP2pDevice {
return Protos.WifiP2pDevice.newBuilder()
.setWpsPbcSupported(device.wpsPbcSupported())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class WiFiDirectBroadcastReceiver(private val manager: WifiP2pManager,
private val stateChangedSink: EventChannel.EventSink?,
peersChangedSink: EventChannel.EventSink?,
private val connectionChangedSink: EventChannel.EventSink?,
private val thisDeviceChangedSink: EventChannel.EventSink?
private val thisDeviceChangedSink: EventChannel.EventSink?,
private val discoveryChangedSink: EventChannel.EventSink?
) : BroadcastReceiver() {

private val peerListListener = WiFiDirectPeerListListener(peersChangedSink)
Expand All @@ -41,6 +42,7 @@ class WiFiDirectBroadcastReceiver(private val manager: WifiP2pManager,
WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION -> onPeersChanged()
WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION -> onConnectionChanged(intent)
WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> onThisDeviceChanged(intent)
WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION -> onDiscoveryChanged(intent)
}
}

Expand Down Expand Up @@ -92,4 +94,10 @@ class WiFiDirectBroadcastReceiver(private val manager: WifiP2pManager,
val dev: Protos.WifiP2pDevice = ProtoHelper.create(device)
thisDeviceChangedSink?.success(dev.toByteArray())
}

private fun onDiscoveryChanged(intent: Intent) {
val discoveryState = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE, WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED);
val stateChange: Protos.DiscoveryStateChange = ProtoHelper.create(discoveryState);
discoveryChangedSink?.success(stateChange.toByteArray());
}
}
4 changes: 4 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
"deviceChange: ${change.deviceName} / ${change.deviceAddress} / ${change.primaryDeviceType} / ${change.secondaryDeviceType} ${change.isGroupOwner ? 'GO' : '-GO'}");
}));

_subscriptions.add(FlutterP2p.wifiEvents.discoveryChange.listen((change) {
print("discoveryStateChange: ${change.isDiscovering}");
}));

_subscriptions.add(FlutterP2p.wifiEvents.peersChange.listen((change) {
print("peersChange: ${change.devices.length}");
change.devices.forEach((device) {
Expand Down
15 changes: 15 additions & 0 deletions lib/broadcast_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ class WiFiDirectBroadcastReceiver {
static EventChannel _thisDeviceChangeChannel =
EventChannel("$_channelBase/bc/this-device-change");

static EventChannel _discoveryChangeChannel =
EventChannel("$_channelBase/bc/discovery-change");

static Stream<StateChange> _stateChangeStream;
static Stream<WifiP2pDeviceList> _peersChangeStream;
static Stream<ConnectionChange> _connectionChangeStream;
static Stream<WifiP2pDevice> _thisDeviceChangeStream;
static Stream<DiscoveryStateChange> _discoveryChangeStream;

Stream<StateChange> get stateChange {
if (_stateChangeStream == null) {
Expand Down Expand Up @@ -69,4 +73,15 @@ class WiFiDirectBroadcastReceiver {

return _thisDeviceChangeStream;
}

Stream<DiscoveryStateChange> get discoveryChange {
if (_discoveryChangeStream == null) {
_discoveryChangeStream = _discoveryChangeChannel
.receiveBroadcastStream()
.map<DiscoveryStateChange>(
(src) => DiscoveryStateChange.fromBuffer(src));
}

return _discoveryChangeStream;
}
}
42 changes: 42 additions & 0 deletions lib/gen/protos/protos.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,45 @@ class SocketMessage extends $pb.GeneratedMessage {
@$pb.TagNumber(3)
void clearData() => clearField(3);
}

class DiscoveryStateChange extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i =
$pb.BuilderInfo('DiscoveryStateChange', createEmptyInstance: create)
..aOB(1, 'isDiscovering', protoName: 'isDiscovering')
..hasRequiredFields = false;

DiscoveryStateChange._() : super();
factory DiscoveryStateChange() => create();
factory DiscoveryStateChange.fromBuffer($core.List<$core.int> i,
[$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) =>
create()..mergeFromBuffer(i, r);
factory DiscoveryStateChange.fromJson($core.String i,
[$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) =>
create()..mergeFromJson(i, r);
DiscoveryStateChange clone() =>
DiscoveryStateChange()..mergeFromMessage(this);
DiscoveryStateChange copyWith(void Function(DiscoveryStateChange) updates) =>
super.copyWith((message) => updates(message as DiscoveryStateChange));
$pb.BuilderInfo get info_ => _i;
@$core.pragma('dart2js:noInline')
static DiscoveryStateChange create() => DiscoveryStateChange._();
DiscoveryStateChange createEmptyInstance() => create();
static $pb.PbList<DiscoveryStateChange> createRepeated() =>
$pb.PbList<DiscoveryStateChange>();
@$core.pragma('dart2js:noInline')
static DiscoveryStateChange getDefault() => _defaultInstance ??=
$pb.GeneratedMessage.$_defaultFor<DiscoveryStateChange>(create);
static DiscoveryStateChange _defaultInstance;

@$pb.TagNumber(1)
$core.bool get isDiscovering => $_getBF(0);
@$pb.TagNumber(1)
set isDiscovering($core.bool v) {
$_setBool(0, v);
}

@$pb.TagNumber(1)
$core.bool hasIsDiscovering() => $_has(0);
@$pb.TagNumber(1)
void clearIsDiscovering() => clearField(1);
}
7 changes: 7 additions & 0 deletions lib/gen/protos/protos.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,10 @@ const SocketMessage$json = const {
const {'1': 'data', '3': 3, '4': 1, '5': 12, '10': 'data'},
],
};

const DiscoveryStateChange$json = const {
'1': 'DiscoveryStateChange',
'2': const [
const {'1': 'isDiscovering', '3': 1, '4': 1, '5': 8, '10': 'isDiscovering'},
],
};
4 changes: 4 additions & 0 deletions protos/protos.proto
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ message SocketMessage {
int32 port = 1;
int32 dataAvailable = 2; // The number of bytes which are still available
bytes data = 3; // The data
}

message DiscoveryStateChange {
bool isDiscovering = 1;
}

0 comments on commit f958165

Please sign in to comment.