11package za .co .riggaroo .androidthings .distributedpiano ;
22
3-
43import android .content .Context ;
5- import android .net .ConnectivityManager ;
6- import android .net .NetworkInfo ;
74import android .os .Bundle ;
85import android .support .annotation .NonNull ;
96import android .support .annotation .Nullable ;
129import com .google .android .gms .common .ConnectionResult ;
1310import com .google .android .gms .common .api .GoogleApiClient ;
1411import com .google .android .gms .common .api .ResultCallback ;
15- import com .google .android .gms .common .api .Status ;
1612import com .google .android .gms .nearby .Nearby ;
17- import com .google .android .gms .nearby .connection .AppIdentifier ;
18- import com .google .android .gms .nearby .connection .AppMetadata ;
13+ import com .google .android .gms .nearby .connection .AdvertisingOptions ;
14+ import com .google .android .gms .nearby .connection .ConnectionInfo ;
15+ import com .google .android .gms .nearby .connection .ConnectionLifecycleCallback ;
16+ import com .google .android .gms .nearby .connection .ConnectionResolution ;
1917import com .google .android .gms .nearby .connection .Connections ;
2018import com .google .android .gms .nearby .connection .ConnectionsStatusCodes ;
19+ import com .google .android .gms .nearby .connection .Payload ;
20+ import com .google .android .gms .nearby .connection .PayloadCallback ;
21+ import com .google .android .gms .nearby .connection .PayloadTransferUpdate ;
22+ import com .google .android .gms .nearby .connection .Strategy ;
2123
2224import java .io .IOException ;
2325import java .nio .ByteBuffer ;
24- import java .util .ArrayList ;
25- import java .util .List ;
2626
2727class PianoPresenter implements PianoContract .Presenter , GoogleApiClient .ConnectionCallbacks ,
28- GoogleApiClient .OnConnectionFailedListener , Connections . ConnectionRequestListener , Connections . MessageListener {
28+ GoogleApiClient .OnConnectionFailedListener {
2929
3030 private static final String TAG = "PianoPresenter" ;
3131 private final GoogleApiClient googleApiClient ;
32+ private static final String DEVICE_NAME = "DistributedPiano" ;
3233 private final String serviceId ;
33- private final String packageName ;
3434 private PianoContract .View view ;
35- private ConnectivityManager connectivityManager ;
3635
37- PianoPresenter (Context context , ConnectivityManager connectivityManager , String serviceId ,
38- String packageName ) throws IOException {
36+ private final ConnectionLifecycleCallback connectionLifecycleCallback = new ConnectionLifecycleCallback () {
37+ @ Override
38+ public void onConnectionInitiated (String endpointId , ConnectionInfo connectionInfo ) {
39+ Log .d (TAG , "Connection initiated from " + endpointId + " " + connectionInfo .getEndpointName ());
40+ Nearby .Connections .acceptConnection (googleApiClient , endpointId , payloadCallback );
41+ }
42+
43+ @ Override
44+ public void onConnectionResult (String endpointId , ConnectionResolution connectionResolution ) {
45+ Log .d (TAG , "onConnectionResult" );
46+ if (connectionResolution .getStatus ().isSuccess ()) {
47+ Log .d (TAG , "Endpoint " + endpointId + " connected" );
48+ } else {
49+ Log .w (TAG , "Endpoint " + endpointId + " already connected" );
50+ }
51+ }
52+
53+ @ Override
54+ public void onDisconnected (String endpointId ) {
55+ Log .d (TAG , endpointId + " disconnected" );
56+ }
57+ };
58+
59+ private final PayloadCallback payloadCallback = new PayloadCallback () {
60+ @ Override
61+ public void onPayloadReceived (String endpointId , Payload payload ) {
62+ Log .d (TAG , "onPayloadReceived" );
63+ double frequency = toDouble (payload .asBytes ());
64+ if (frequency == -1 ) {
65+ view .stopPlayingNote ();
66+ return ;
67+ }
68+ view .playNote (frequency );
69+ }
70+
71+ @ Override
72+ public void onPayloadTransferUpdate (String endpointId , PayloadTransferUpdate payloadTransferUpdate ) {
73+ switch (payloadTransferUpdate .getStatus ()) {
74+ case PayloadTransferUpdate .Status .IN_PROGRESS :
75+ Log .d (TAG , "onPayloadTransferUpdate " + payloadTransferUpdate .getBytesTransferred () + " bytes transferred" );
76+ break ;
77+ case PayloadTransferUpdate .Status .SUCCESS :
78+ Log .d (TAG , "onPayloadTransferUpdate completed" );
79+ break ;
80+ case PayloadTransferUpdate .Status .FAILURE :
81+ Log .d (TAG , "onPayloadTransferUpdate failed" );
82+ break ;
83+ }
84+ }
85+ };
86+
87+ PianoPresenter (Context context , String serviceId ) throws IOException {
3988 googleApiClient = new GoogleApiClient .Builder (context ).addConnectionCallbacks (this )
4089 .addOnConnectionFailedListener (this ).addApi (Nearby .CONNECTIONS_API ).build ();
4190 this .serviceId = serviceId ;
42- this .connectivityManager = connectivityManager ;
43- this .packageName = packageName ;
4491 }
4592
4693 @ Override
4794 public void onConnected (@ Nullable final Bundle bundle ) {
4895 Log .d (TAG , "onConnected!" );
4996 startAdvertising ();
50-
5197 }
5298
5399 @ Override
@@ -60,25 +106,11 @@ public void onConnectionFailed(@NonNull final ConnectionResult connectionResult)
60106 Log .d (TAG , "onConnectionFailed" );
61107 }
62108
63- private boolean isConnectedToNetwork () {
64- NetworkInfo info = connectivityManager .getNetworkInfo (ConnectivityManager .TYPE_WIFI );
65- NetworkInfo info1 = connectivityManager .getNetworkInfo (ConnectivityManager .TYPE_ETHERNET );
66-
67- return (info != null && info .isConnectedOrConnecting ()) || (info1 != null && info1 .isConnectedOrConnecting ());
68- }
69-
70-
71109 private void startAdvertising () {
72110 Log .d (TAG , "startAdvertising" );
73- if (!isConnectedToNetwork ()) {
74- Log .d (TAG , "startAdvertising: not connected to WiFi network." );
75- return ;
76- }
77-
78- List <AppIdentifier > appIdentifierList = new ArrayList <>();
79- appIdentifierList .add (new AppIdentifier (packageName ));
80- AppMetadata appMetadata = new AppMetadata (appIdentifierList );
81- Nearby .Connections .startAdvertising (googleApiClient , serviceId , appMetadata , 0L , this )
111+ AdvertisingOptions advertisingOptions = new AdvertisingOptions (Strategy .P2P_CLUSTER );
112+ Nearby .Connections
113+ .startAdvertising (googleApiClient , DEVICE_NAME , serviceId , connectionLifecycleCallback , advertisingOptions )
82114 .setResultCallback (new ResultCallback <Connections .StartAdvertisingResult >() {
83115 @ Override
84116 public void onResult (@ NonNull Connections .StartAdvertisingResult result ) {
@@ -99,44 +131,10 @@ public void onResult(@NonNull Connections.StartAdvertisingResult result) {
99131 });
100132 }
101133
102- @ Override
103- public void onConnectionRequest (final String endpointId , String deviceId , String endpointName , byte [] payload ) {
104- Log .d (TAG , "onConnectionRequest" );
105-
106- Nearby .Connections .acceptConnectionRequest (googleApiClient , endpointId , payload , this )
107- .setResultCallback (new ResultCallback <Status >() {
108- @ Override
109- public void onResult (@ NonNull Status status ) {
110- if (status .isSuccess ()) {
111- Log .d (TAG , "acceptConnectionRequest: SUCCESS" );
112-
113- } else {
114- Log .d (TAG , "acceptConnectionRequest: FAILURE" );
115- }
116- }
117- });
118- }
119-
120- @ Override
121- public void onMessageReceived (final String s , final byte [] bytes , final boolean b ) {
122- Log .d (TAG , "onMessageReceived" );
123- double frequency = toDouble (bytes );
124- if (frequency == -1 ) {
125- view .stopPlayingNote ();
126- return ;
127- }
128- view .playNote (frequency );
129- }
130-
131134 private static double toDouble (byte [] bytes ) {
132135 return ByteBuffer .wrap (bytes ).getDouble ();
133136 }
134137
135- @ Override
136- public void onDisconnected (final String s ) {
137-
138- }
139-
140138 @ Override
141139 public void detachView () {
142140 this .view = null ;
0 commit comments