2

I am recently working on a project that requires sending a command from my android mobile app to the android things on my raspberry pi 3. How can I achieve this through a WiFi connection?

I only need to send a String to the device.

  • Take a look at [this](https://stackoverflow.com/a/10388977/6950238) answer of [Moises Jimenez](https://stackoverflow.com/users/1363117/moises-jimenez). – Andrii Omelchenko Aug 01 '18 at 08:10

4 Answers4

2

Using Android Things, you can use the Nearby Messages API, which gives you the ability to communicate to and transfer messages between two Android devices within their apps. Here's a code snippet:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  mMessageListener = new MessageListener() {
    @Override
    public void onFound(Message message) {
        Log.d(TAG, "Found message: " + new String(message.getContent()));
    }

    @Override
    public void onLost(Message message) {
        Log.d(TAG, "Lost sight of message: " + new String(message.getContent()));
    }
  }

  mMessage = new Message("Hello World".getBytes());
}

@Override
public void onStart() {
  super.onStart();
  ...
  Nearby.getMessagesClient(this).publish(mMessage);
  Nearby.getMessagesClient(this).subscribe(mMessageListener);
}

@Override
public void onStop() {
  Nearby.getMessagesClient(this).unpublish(mMessage);
  Nearby.getMessagesClient(this).unsubscribe(mMessageListener);
  ...
  super.onStop();
}
Nick Felker
  • 11,536
  • 1
  • 21
  • 35
  • In the documentation it says that both devices have to be connected to internet. My devices may not be able to be connected to the internet. – mohammad reza sarsarabi Aug 02 '18 at 05:55
  • 1
    Then you may want to use [Nearby Connections](https://developers.google.com/nearby/connections/overview) instead, which [is entirely P2P and works offline](https://developers.google.com/nearby/connections/v11-update) – Nick Felker Aug 02 '18 at 18:45
  • Okay I think I have misunderstood the documentation. I wanted to set up a http server but now I'll try to use this API, I hope it is reliable enough. – mohammad reza sarsarabi Aug 03 '18 at 14:18
2

If one of your devices isn't connected to internet, you could :

Option 1 : use Google Nearby Connections API , the API choose the best way to communicate (eg: Bluetooth, Wifi...).

See https://github.com/googlesamples/android-nearby/tree/master/connections

Option 2 : Use Socket to communicate but your devices need to be on the same network. If they aren't connected to the same network, you can connect them using WIFI P2P.

Sushii
  • 63
  • 5
1

If you use system like Raspbian you can transform your Raspberry into a server.Then, You will have different ways to send your command:

Option 1: Set up an Http server on your raspberry (PHP, NodeJS, JEE, ...) and send command via HTTP Request.

Option 2: Set up a Socket Server on your raspberry (Socket.io, raw socket, ...) and send command via socket client

Option 3 Set up MQTT Server on your raspberry and send command via MQTT client (this last option is the way to go when talking about Internet of Things). Note that the program which receive command should implement MQTT Client as MQTT is based on pub/sub pattern.

maheryhaja
  • 1,617
  • 11
  • 18
  • 2
    Yes, the last way is the IoT way, but technically, the server should not be run on the thing. MQTT is a publish/subscribe protocol which means both the device and the thing are clients and the server (broker) rests somewhere on the cloud. – Hamid Fadishei Aug 01 '18 at 07:29
1

You can use nanoHttpd on Android things and other library such as retrofit or volley on the Android device.

Check out this example for controlling a car via an Http API: https://github.com/plattysoft/IotCar

shalafi
  • 3,926
  • 2
  • 23
  • 27