Showing posts with label mqtt. Show all posts
Showing posts with label mqtt. Show all posts

Saturday, May 5, 2018

MqttConnectOptions members not recogized by Android Sudio 3.1.1

Leave a Comment

I am writing a straightforward mqtt client. For some strange reason Android studio 3.1.1 can't resolve any member function of MqttConnectOptions.

None of the members functions of MqttConnectOptions are recognized by the compiler.

All the examples I can find on the web use the MqttConnectOptions members. I'm baffled!

What simple thing am I missing?

package com.grayraven.garage;  import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log;  import org.eclipse.paho.android.service.MqttAndroidClient; import org.eclipse.paho.client.mqttv3.IMqttActionListener; import org.eclipse.paho.client.mqttv3.IMqttToken; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException;    public class MainActivity extends AppCompatActivity {      final String TAG = "Garage_Main";     private static Context mContext = GarageApp.getAppContext();      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         mContext = this.getApplicationContext();         setContentView(R.layout.activity_main);     }      final String broker = "10.211.1.127:1883";     final String password = "monkey123";     final String subscriptionTopic = "garage_door";       MqttConnectOptions options = new MqttConnectOptions();     options.setUserName("pi"); //cannot resolve setUserName!!!     options.setPassword(password.toCharArray()); //cannot resolve setPassword!!!!       String clientId = MqttClient.generateClientId();     MqttAndroidClient client = new MqttAndroidClient(mContext, broker,                     clientId);     /// rest of code works so omitted for clarity 

app build.gradle:

apply plugin: 'com.android.application'  android {     compileSdkVersion 26     defaultConfig {         applicationId "com.grayraven.garage"         minSdkVersion 22         targetSdkVersion 26         versionCode 1         versionName "1.0"         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"     }     buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'         }     } }  repositories {     maven {         url "https://repo.eclipse.org/content/repositories/paho-releases/"     } }  dependencies {     implementation fileTree(dir: 'libs', include: ['*.jar'])     implementation 'com.android.support:appcompat-v7:26.1.0'     implementation 'com.android.support.constraint:constraint-layout:1.1.0'     testImplementation 'junit:junit:4.12'     androidTestImplementation 'com.android.support.test:runner:1.0.1'     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'     implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {         exclude module: 'support-v4'     } } 

2 Answers

Answers 1

Your code to import MQTT dependencies for Android are not correct. Please try this one.

Step 1: Remove these lines from your build.gradle file

repositories {     maven {         url "https://repo.eclipse.org/content/repositories/paho-releases/"     } }  implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {         exclude module: 'support-v4'     } 

Step 2: Add these lines to build.gradle file of your Android Studio project. This is top-level build.gradle file not the one in app forder where you declare all dependencies of your project.

repositories {     maven {         url "https://repo.eclipse.org/content/repositories/paho-snapshots/"     } } 

Step 3: Add these lines to build.gradle file of your Android app

dependencies {     compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'     compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' } 

Finally Clean, Sync then Build your project.

Answers 2

It was just a rookie mistake, I was trying to set the options outside the code section of the main activity.

Never mind.

Read More

Friday, October 14, 2016

AWS MQTT on OSX

Leave a Comment

In the OSX app here, I can use AWS MQTT with iOS9, but when I try the same with OSX10.11, I get this error:

CFNetwork SSLHandshake failed (-9829)

Error -9828 is defined as

errSSLPeerCertUnknown = -9829, /* unknown certificate */

My OSX info.plist is

<key>NSAppTransportSecurity</key>     <dict>             <key>NSAllowsArbitraryLoads</key>             <true/>             <key>NSExceptionDomains</key>             <dict>                     <key>amazonaws.com</key>                     <dict>                             <key>NSExceptionRequiresForwardSecrecy</key>                             <false/>                             <key>NSExceptionAllowsInsecureHTTPLoads</key>                             <true/>                             <key>NSThirdPartyExceptionMinimumTLSVersion</key>                             <string>TLSv1.0</string>                             <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>                             <false/>                             <key>NSIncludesSubdomains</key>                             <true/>                     </dict>                     <key>amazonaws.com.cn</key>                     <dict>                             <key>NSExceptionRequiresForwardSecrecy</key>                             <false/>                             <key>NSExceptionAllowsInsecureHTTPLoads</key>                             <true/>                             <key>NSThirdPartyExceptionMinimumTLSVersion</key>                             <string>TLSv1.0</string>                             <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>                             <false/>                             <key>NSIncludesSubdomains</key>                             <true/>                     </dict>             </dict>     </dict> 

The p12 (from "openssl pkcs12 -info -in awsiot-identity.p12") is:

MAC Iteration 2048 MAC verified OK PKCS7 Encrypted data: pbeWithSHA1And40BitRC2-CBC, Iteration 2048 Certificate bag Bag Attributes     localKeyID: 5F 80 DC 6E AB F1 98 6A AA FC 0B 7B 04 F9 0E 66 99 E9 86 4F  subject=/CN=AWS IoT Certificate issuer=/OU=Amazon Web Services O=Amazon.com Inc. L=Seattle ST=Washington C=US -----BEGIN CERTIFICATE----- MIIDWTCCAkGgAwIBAgIUJQgfGjmoboOQ7eJo+NTRs5wr8KMwDQYJKoZIhvcNAQEL BQAwTTFLMEkGA1UECwxCQW1hem9uIFdlYiBTZXJ2aWNlcyBPPUFtYXpvbi5jb20g SW5jLiBMPVNlYXR0bGUgU1Q9V2FzaGluZ3RvbiBDPVVTMB4XDTE2MDcyNTA2NDU0 NloXDTQ5MTIzMTIzNTk1OVowHjEcMBoGA1UEAwwTQVdTIElvVCBDZXJ0aWZpY2F0 ZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK57RCK3ppDk22TPA+97 2coZeo36lJpZ9M/0l6xHyeQiiVZMKGrkP3S+ej4Dgd+q4gviB2g5dc9T6jMwRSA8 qkdadxspSmEtCCwdFY3poVOpsD7Z0s3lVwBSgTiztfQo15yTyIjhkS0gS9tBg1sI xIJoYuxXEHkoJKHum8yaluL71jYLxdmp5YHGVHZ55ussZUrWuE4ut4EbHJ8+Ef+z caJtJB6YMEeKpKMvZ0vrb+jHytD6s7K20SnfTvEHsXNwWIfwXsxmqkG9KHT7q9Dd XlaeKiP0tWE/8ObOPk1W7xT9HTAvkrveJIEFYhMcfi0yTtxm9CyEG0p36yor2HAK T/UCAwEAAaNgMF4wHwYDVR0jBBgwFoAU8Kei7lBQZkzRV3if5sWxgF9WtM8wHQYD VR0OBBYEFM7oRgS5iXeFPcI4pzY/0BQCCE3mMAwGA1UdEwEB/wQCMAAwDgYDVR0P AQH/BAQDAgeAMA0GCSqGSIb3DQEBCwUAA4IBAQBDzKiP+Gldz4RUe0QyMcYDWS0V /3PeJTRjoD7IxUOO9czCZoCX46dxJkP1ijzuuqneaEPK7OUQxoHepqPdlbsycXv3 i/Ty649c/d2dizYqO2iM+6M+xdDLYPBmEAD4aQ9Qj8TpnC5OCSdqGq9XCFLTnz4j icx2lYS3COdfZbKs9KQG7dkPK7CWSjHHy21Ftz0zBx7wj5v+2lNbcHCFmYn9+lYg Jw1zUR/rGqTcQZHGUvgv3Mfp8xWtHDFhYAKnwGbhIxCanOM6An+yzEwLUEvkQ81Q Lzv/yReCVHO4M0+JTW4Fu6BWEaTThPzdN3kQbIzJsViIL9Q6dfAXlvepkHr4 -----END CERTIFICATE----- PKCS7 Data Shrouded Keybag: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2048 Bag Attributes     localKeyID: 5F 80 DC 6E AB F1 98 6A AA FC 0B 7B 04 F9 0E 66 99 E9 86 4F  Key Attributes: <No Attributes> 

Why does OSX fail but iOS succeed?

2 Answers

Answers 1

It is failing because of an SSL handshake issue. It is detecting an invalid certificate.

A similar issue was reported and resolved here, referencing the same error code. That issue was traced to an identity mismatch, due to multiple identities in the p12 file.

In that case, there were two certificates in the p12 files, but the code was only reading the first one.

I suggest dumping the contents of the .p12 file, and confirming the certificates(s). Post them here to review.

Answers 2

you missing signing CA on your OS X for the certificate, depends on how your created your certificate, you need to import the CA into your keychain. refer here

Read More

Tuesday, March 15, 2016

Enabling MQTT Service in Background in Hybrid Applications for iOS environment

Leave a Comment

I'm using MobileFirst Platform 7.1 with Paho MQTT (Internet of Things), I'm trying to implement a Hybrid Application with mqtt for Android and iOS.

I have tested the app in Android and it is working as expected, when it is in the foreground and background,

While in iOS, it does not work as expected. It works correctly in the foreground, but not in the background. I was testing using the iPhone Simulator of XCode and iPhone 6.

I found a project which implemented with MobileFirst and MQTT. from https://developer.ibm.com/mobilefirstplatform/documentation/integration/mq-telemetry-transport/

i am just adding host and port number and adding android,ios environment.but same problem facing android working iOS not.

at this moment what can i do ?

UPDATE:

in iOS, it works correctly foreground.but when i send background the "MQTT client" connection lost form broker.

this method fire :

this.client.onConnectionLost = function() {         alert("Connection lost!");     }; 

on the other hand , android , windows it work perfectly for foreground and background

For example :

I have a room there have a PIR sensor. it work like publisher . my application user is a subscriber .

when user out the room that that time she open application,subscribe and send application background.

anytime someone entire the room that time , she will be get a notification.

1 Answers

Answers 1

Details on how to run in the background on iOS can be found here. This link also lists the actions that Apple allows to run in the background, if your app does not meet those criteria then it is likely to get bumped from the app store.

The list also shows which UIBackgroundModes to place in your Info.plist to flag that your app needs background access.

Read More