When AWSIotMqttManager is connected, i subscribe to shadow topics
mMqttManager.subscribeToTopic("$aws/things/4/shadow/update/delta", AWSIotMqttQos.QOS1, new AWSIotMqttNewMessageCallback() {
#Override
public void onMessageArrived(String topic, byte[] data) {
Log.d("DEBUG", "onMessageArrived: " + topic);
}
});
mMqttManager.subscribeToTopic("$aws/things/4/shadow/get/accepted", AWSIotMqttQos.QOS1, new AWSIotMqttNewMessageCallback() {
#Override
public void onMessageArrived(String topic, byte[] data) {
Log.d("DEBUG", "onMessageArrived: " + topic);
}
});
and then publish an empty message, as stated in docs
mMqttManager.publishString("", "$aws/things/4/shadow/get", AWSIotMqttQos.QOS1);
I get no callback.
When i remove subscription to delta, i got it.
Why? How to to subscribe on two topics on app start?
Related
I am working on an android app (on android studio) where I have successfully implemented the paho mqtt library for a single activity. I have now run into the issue for the case where I need to persist my mqtt client across multiple activities.
Will i need to create a new client for each activity (subscribe to the needed topics again) and pass modified data of the old client through intents to update the new client ? [this seems like a really bad method and I am assuming that there's a more simple straightforward solution that I am missing]
On your paho mqtt class, you can send broadcast message to your activities. Here how I used;
#Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.d("MQTT MESSAGE", "Message arrived: Topic: " + topic + " Message: " + message);
broadcastSendAction("mqttMessage", "Topic: " + topic + " Message: " + message);
}
#Override
public void connectComplete(boolean b, String s) {
Log.d("MQTT MESSAGE", "Connection SUCCESS.");
mqttStatus = true;
broadcastSendAction("mqttConnection", true);
if (!mqttSubs)
subscribeTopic(mSharedPreferences.getString(getResources().getString(R.string.regId), SepMessageUtils.DEFAULT_REGID), client);
}
want to start development with AWS IOT using Android app
I am seeking for example for IOT in android. need to start basic configuration on AWS console and android app. i already tested temperature demo but didn't get any clue from that! need a basic steps on shadow, policy , role. how to configure them step by step and use of cognito.
below getshadow() method is called onCreate , need to update value on real time basis not ony onCreate.
public void getShadows() {
GetShadowTask getControlShadowTask = new GetShadowTask("TemperatureControl");
getControlShadowTask.execute();
}
private class GetShadowTask extends AsyncTask<Void, Void, AsyncTaskResult<String>> {
private final String thingName;
public GetShadowTask(String name) {
thingName = name;
}
#Override
protected AsyncTaskResult<String> doInBackground(Void... voids) {
try {
GetThingShadowRequest getThingShadowRequest = new GetThingShadowRequest()
.withThingName(thingName);
GetThingShadowResult result = iotDataClient.getThingShadow(getThingShadowRequest);
// Toast.makeText(getApplication(),result.getPayload().remaining(),Toast.LENGTH_LONG).show();
byte[] bytes = new byte[result.getPayload().remaining()];
result.getPayload().get(bytes);
String resultString = new String(bytes);
return new AsyncTaskResult<String>(resultString);
} catch (Exception e) {
Log.e("E", "getShadowTask", e);
return new AsyncTaskResult<String>(e);
}
}
#Override
protected void onPostExecute(AsyncTaskResult<String> result) {
if (result.getError() == null) {
JsonParser parser=new JsonParser();
JsonObject jsonObject= (JsonObject) parser.parse(result.getResult());
response=result.getResult();
setPoint=jsonObject.getAsJsonObject("state").getAsJsonObject("reported")
.get("current_date").getAsString();
textView.setText(setPoint);
// Toast.makeText(getApplication(),setPoint,Toast.LENGTH_LONG).show();
Log.i(GetShadowTask.class.getCanonicalName(), result.getResult());
} else {
Log.e(GetShadowTask.class.getCanonicalName(), "getShadowTask", result.getError());
Toast.makeText(getApplication(),result.getError().toString(),Toast.LENGTH_LONG).show();
}
}
}
UPDATE
Thing Shadow
{
"desired": {
"welcome": "aws-iot"
},
"reported": {
"welcome": "aws-iot",
"current_date": "06-Sep-2017 1:26:40 PM"
}
}
AWS has provided a complete Github repo of Android samples. In the samples do the PubSubWebSocket to connect, subscribe and publish the data to the shadow.
If you have a closer look into the PubSubWebSocket example you will find a detailed information on how to to make a thing policy and role. It cannot be more concise and clear than that.
For understanding and using Cognito follow AmazonCognitoAuthDemo example to make the identity pool and use it in the PubSubWebSocket example.
To get a better understanding of roles and Cognito. Please read the AWS documentation.
Update:
In the IoT thing policy did you give appropriate permissions to connect, subscribe and publish. The option can be found in AWS IoT->Security->Policy->Create Policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:*",
"Resource": "arn:aws:iot:us-east-2:293751794947:topic/replaceWithATopic"
}
]
}
The above policy gives all access to the user. Also, make sure your pool which you created is for unauthenticated users.
To get the changes to the shadow type the following in the sample android(WebSocketAwsPubSub) edit box $aws/things/thing_name/shadow/update/accepted
And to publish the data to the shadow type $aws/things/thing_name/shadow/update
Update 2:
Android Code where you will receive the reported messaged. Its suscribing to the device. Its the copy of the snippet from PubSubWebSocketSample.
public void AwsSubscribe(){
final String topic = "$aws/things/D1/shadow/update/accepted";
Log.d(LOG_TAG, "topic = " + topic);
try {
mqttManager.subscribeToTopic(topic, AWSIotMqttQos.QOS0,
new AWSIotMqttNewMessageCallback() {
#Override
public void onMessageArrived(final String topic, final byte[] data) {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
String message = new String(data, "UTF-8");
Log.d(LOG_TAG, "Message arrived:");
Log.d(LOG_TAG, " Topic: " + topic);
Log.d(LOG_TAG, " Message: " + message);
tvLastMessage.setText(message);
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Message encoding error.", e);
}
}
});
}
});
} catch (Exception e) {
Log.e(LOG_TAG, "Subscription error.", e);
}
}
If you want to create a topic, just change the value of this variable final String topic = "YOUR TOPIC" then subscribe to it by using the sample code.
I am publishing and subscribing to messages with Google Nearby and am receiving onFound and onLost properly, but am not receiving any callbacks on onDistanceChanged or onBleSignalChanged. I know that BLE signal changed is based on using BLE for discovery, but is there a similar limitation for onDistanceChanged? The docs don't seem to indicate there is. Here is my message listener below, thanks for any suggestions!
mMessageListener = new MessageListener() {
#Override
public void onFound(Message message) {
String messageAsString = new String(message.getContent());
Log.d(TAG, "Found message: " + messageAsString);
}
#Override
public void onLost(Message message) {
String messageAsString = new String(message.getContent());
Log.d(TAG, "Lost sight of message: " + messageAsString);
}
#Override
public void onDistanceChanged(Message message, Distance distance) {
super.onDistanceChanged(message, distance);
Log.d(TAG, "New distance "+distance.getMeters()+" to message: " + new String(message.getContent()));
}
#Override
public void onBleSignalChanged(Message message, BleSignal bleSignal) {
super.onBleSignalChanged(message, bleSignal);
Log.d(TAG, "New ble signal " + bleSignal.getRssi() + " to message: " + new String(message.getContent()));
}
};
There are two reasons you may not get that callback:
Unfortunately, the distance callback currently only works for BLE beacon messages. We've fixed the docs to say that, but they won't update on the web until the next Google Play Services SDK release (in a couple weeks). We hope to make it work for more messages in the future.
The BLE signal and distance callbacks aren't called for the PendingIntent version of subscribe().
I try to using salut librari (wifip2p library) for Android.
All work fine, discover devices, connect host, connect clients...
But when I need send a message, I have a problem.
I follow Salut tutorial: Salut
I send a message, and in console say me tha tthe message is received.
09-13 13:21:58.633 21798-21906/app.ingenia.marse.ingeniamarse D/Salut: Listening for service data...
09-13 13:21:58.643 21798-21986/app.ingenia.marse.ingeniamarse V/Salut: A device is sending data...
09-13 13:21:58.675 21798-21986/app.ingenia.marse.ingeniamarse D/Salut: Successfully received data.
But I can't read the received message.
The sender function:
public void sendData(){
Message myMessage = new Message();
myMessage.description = "See you on the other side!";
network.sendToAllDevices(myMessage, new SalutCallback() {
#Override
public void call() {
Log.e(TAG, "Oh no! The data failed to send.");
}
});
}
Where I received data:
#Override
public void onDataReceived(Object data) {
Log.d(TAG, "Received network data.");
try
{
Message newMessage = LoganSquare.parse((Message)data, Message.class);
Log.d(TAG, newMessage.description); //See you on the other side!
//Do other stuff with data.
}
catch (IOException ex)
{
Log.e(TAG, "Failed to parse network data.");
}
}
The code don't excecute the log received network data. And I need to change one line:
Message newMessage = LoganSquare.parse((Message)data, Message.class);
//to
Message newMessage = LoganSquare.parse(String.valueOf((Message)data), Message.class);
And the Message.class:
#JsonObject
public class Message{
/*
* Annotate a field that you want sent with the #JsonField marker.
*/
#JsonField
public String description;
/*
* Note that since this field isn't annotated as a
* #JsonField, LoganSquare will ignore it when parsing
* and serializing this class.
*/
public int nonJsonField;
#OnJsonParseComplete
void onParseComplete() {
Log.d("PARSED", "onParseComplete: PARSED!!");
}
/*
* Optional callback method to do something before your
* object serializes.
*/
#OnPreJsonSerialize
void onPreSerialize() {
Log.d("SERIALIZED", "onParseComplete: SERIALIZED!!");
}
}
The two logs in message.class nor they have executed
It is because according to documentation, onDataRecieve() is not executed as data.isEmpty() returns true. Check what i mean from here.
I'm trying to implement a pusher service in my Android app, doesn't have access to the server just copying from an iOS app previous implementation. Everything works fine in connection process but when subscribe to a private channel the authentication fails with:
"com.pusher.client.AuthorizationFailureException: java.io.FileNotFoundException: https://authorization_url"
The implementation goes like this:
HttpAuthorizer authorizer = new HttpAuthorizer(PUSHER_AUTH_URL);
PusherOptions options = new PusherOptions().setEncrypted(true).setWssPort(443).setAuthorizer(authorizer);
pusher = new Pusher(PUSHER_KEY, options);
pusher.connect(new com.pusher.client.connection.ConnectionEventListener() {
#Override
public void onConnectionStateChange(ConnectionStateChange change) {
if (change.getCurrentState() == ConnectionState.CONNECTED) {
Channel channel = pusher.subscribePrivate(PUSH_CHANNEL, new PrivateChannelEventListener() {
#Override
public void onAuthenticationFailure(String s, Exception e) {
Log.w("PUSHER", "Channel subscription authorization failed");
}
#Override
public void onSubscriptionSucceeded(String s) {
Log.w("PUSHER", "Channel subscription authorization succeeded");
}
#Override
public void onEvent(String s, String s2, String s3) {
Log.w("PUSHER", "An event with name " + s2 + " was delivered!!");
}
}, "my-event");
}
}
#Override
public void onError(String message, String code, Exception e) {
Log.w("PUSHER", "There was a problem connecting with code " + code + " and message " + message);
}
}, ConnectionState.ALL);
UPDATE
I'm sure that the problem is with the authentication, there is a function call in iOS version that set some headers to the channel subscription or something like that:
(void)pusher:(PTPusher *)pusher willAuthorizeChannel:(PTPusherChannel *)channel withRequest:(NSMutableURLRequest *)request;
{
[request addAuthorizationHeadersForUser:self.credentials.user];
}
Im trying to figure out where to add the headers in android, try to add it to the authorizer but nothing change:
authorizer.setHeaders(addMapAuthorizationHeaders());
Any idea of what is the equivalent in Android of that iOS function: willAuthorizeChannel??
Ok solved, it was what I thought, the HttpAuthorizer needed a set of headers that you can set directly when creating it like:
HttpAuthorizer authorizer = new HttpAuthorizer(PUSHER_AUTH_URL);
authorizer.setHeaders(MY_AUTH_HEADERS); //a HashMap with the headers
PusherOptions options = new PusherOptions().setEncrypted(true).setWssPort(443).setAuthorizer(authorizer);
pusher = new Pusher(PUSHER_KEY, options);
And with that works fine, in case somebody have a similar problem.
EDIT:
this is how to set the authorization headers. It's a Map set to "Key" "Value" pair for example:
public static HashMap<String, String> getMapAuthorizationHeaders() {
try {
HashMap<String, String> authHeader = new HashMap<>();
authHeader.put("HeaderKey1", "HeaderValue1");
authHeader.put("HeaderKey2", "HeaderValue2");
return authHeader;
} catch (Exception e) {
return null;
}
}
So the pusher config will be like:
authorizer.setHeaders(getMapAuthorizationHeaders());
I've been struggling with this as well... the solution is simple.
First check this out: https://github.com/pusher/pusher-websocket-java/blob/master/src/main/java/com/pusher/client/util/HttpAuthorizer.java
Then implement the abstract interface Authorizer and override the authorize method with your own code and that's it, you get the same thing as on the iOS.
Some snippet to get you started (with a custom constructor):
CustomSocketHttpAuthorizer authorizer = new CustomSocketHttpAuthorizer(ServerComm.API_MAIN_LINK + ServerComm.API_LINK_PUSHER_AUTH, pusherServerAuthTimeStamp, MessageActivity.this);
PusherOptions options = new PusherOptions().setAuthorizer(authorizer).setEncrypted(true);;
clientPusher = new Pusher(ServerComm.PUSHER_CLIENT_KEY, options);
clientPusher.connect(new ConnectionEventListener() .....