LocalNetwork


Source link: https://github.com/itsJoKr/LocalNetwork

LocalNetwork

Local network library for connecting Android devices on WiFi. One device serves as a server, others are clients. Library takes care of discovery of server by clients. Library uses services and separate threads with sockets to handle communication.

Wi-Fi Aware

In new O Developer Preview, Android team created something called Wi-Fi aware, and you might want to check that out: https://developer.android.com/preview/features/wifi-aware.html

This library is nowhere near done. But you can get it from jcenter if you want:

compile 'com.github.jokr:localnet:0.1'

Usage

There are two main parts of this library: server and client. One device should be server and others should be clients. Server uses LocalServer class, and clients LocalClient class.

There are two phases: discovery and session. In discovery phase client devices discover server on wifi.

Discovery | Client

Client should start LocalClient instance and call connect(). You will probably want to setDiscoveryReceiver(*DiscoveryStatusReceiver impl*) then you can get notified if server is discovered successfully. You will be also notified when server starts session.

public interface DiscoveryStatusReceiver {

public void onDiscoveryTimeout();

public void onServerDiscovered();

public void onSessionStart();

  
}

Discovery | Server

Server should start LocalServer instance and call init(). From there it is listening for clients to discover it.

localServer = new LocalServer(getContext());
 localServer.init();

You can receive information when client connects by providing receiver in localServer.setReceiver method.

 @Override
  public void onClientConnected(Payload<?> payload) {

// do something
  
}

You will see a lot of Payload<?> through this library. It is basically a way for library to send any your object. Make your own class implements Serializable and pack it with Payload: new Payload<YourClass>(new YourClass(...)).

Once you are ready to end discovery and start session call localServer.setSession(*LocalSesion impl) and discovery phase ends and session starts.

LocalSession class

Main part of session is LocalSession class. This is abstract class you should extend and implement all session/game logic here. This code will run in service. Main methods to override are:

 @Override
  public void onCreate(Bundle bundle, ConnectedClients connectedClients){

// session is created
  
}

@Override
  public void onReceiveMessage(long recipientId, Payload payload) {

// received message from client
  
}

@Override
  public void onEvent(Payload<?> payload) {

// received event from LocalServer instance
  
}

From here you can call sendMessage(long recipientId, Payload<?> payload) to send message to client, or sendBroadcastMessage(Payload<?> payload) to send message to all clients. You can also send message to your LocalServer instance and then possibly show it on UI with sendUiEvent(Payload<?> payload)

Session | Server

To start a session, just call localServer.setSession(*LocalSesion impl*) and provide implementation as descibed above. Just like you can send events from your LocalSession to LocalServer, you can send events other way - from LocalServer to your LocalSession implementation by calling localServer.sendLocalSessionEvent(Payload<?>) and this will call onEvent method in your LocalSession.

Session | Client

Client can send messages to server by calling localClient.sendSessionMessage(Payload<?>) and receive messages from server by setting receiver:

localClient.setReceiver(new LocalClient.MessageReceiver() {

 @Override

 public void onMessageReceived(Payload<?> payload) {

  // Payload has object you've sent from LocalSession

 
}

}
);

Resources

Load custom fonts easily just via layout parameters or styles. Uses static Typeface objects, load once - memory efficient. No more textView.setTypeface() calls!

Extends StatedFragment and save state in onSaveState(Bundle outState) and restore state in onRestoreState(Bundle savedInstanceState).

An Android Logger with a lot of options for debugging that helps a lot to developers. You can easily add logs everywhere in your APP and manage showing them by limit them by Codes or by their Class name. It also will show you that logs was called from which file and which line, you also can see which class and which line called your current method.

A color picker activity optimized for Android Wear. Handy for watch face settings.

This project aims to provide a simple and customizable pull to refresh implementation.

This project is an implementation of the User Profile Interface Animation.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes