// Launch IoTRemoteCall server in a separate thread
workerThread = new Thread(new Runnable() {
+ /* TODO: We revert back to HTTP because of new scheme for TLS context in Android 7
+ This disrupts the usual setting for self-signed certificate
+ See this link for more info: https://github.com/owntracks/android/issues/481
public void run() {
iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class,
phoneInfo, iotDevAdd.getDestinationPortNumber(),
IoTDeviceAddress.getLocalHostAddress());
+ }*/
+ public void run() {
+ iotRemCall = new IoTRemoteCall(PhoneInfoInterface.class,
+ phoneInfo, iotDevAdd.getDestinationPortNumber());
}
});
workerThread.start();
// Launch IoTRemoteCall server in a separate thread
workerThread = new Thread(new Runnable() {
+ /* TODO: We revert back to HTTP because of new scheme for TLS context in Android 7
+ This disrupts the usual setting for self-signed certificate
+ See this link for more info: https://github.com/owntracks/android/issues/481
public void run() {
iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class,
weatherInfo, iotDevAdd.getDestinationPortNumber(),
IoTDeviceAddress.getLocalHostAddress());
+ }*/
+ public void run() {
+ iotRemCall = new IoTRemoteCall(WeatherInfoInterface.class,
+ weatherInfo, iotDevAdd.getDestinationPortNumber());
}
});
workerThread.start();
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
-import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.BufferedInputStream;
import java.util.List;
-import java.security.cert.CertificateFactory;
-import java.security.cert.Certificate;
-import java.security.cert.X509Certificate;
-import java.security.KeyStore;
/**
* Created by xubin on 4/26/16.
public class Helper {
private static final int Driver_port = 8000;
private static final String Tag = "http";
- private static final String KEYEXT = ".pem";
HttpClient httpclient;
//Set up
- void setConnection(String destIP) {
-
- httpclient = createClient(destIP);
+ void setConnection() {
+ httpclient = createClient();
}
-
- HttpClient createClient(String destIP) {
+ HttpClient createClient() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.DEFAULT_CONTENT_CHARSET);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), Driver_port));
- schReg.register(new Scheme("https", newSslSocketFactory(destIP), 443));
+ //schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
return new DefaultHttpClient(conMgr, params);
}
-
//Make http request
public void makeRequest(String destIP, List<Object> paramObjects, String methodName) {
- String url = "https://"+ destIP + ":"+Driver_port+ "/"+methodName;
- System.out.println("URL: " + url);
+ String url = "http://"+ destIP + ":"+Driver_port+ "/"+methodName;
InputStream inputStream = null;
String result = "";
try {
HttpPost httpPost = new HttpPost(url);
JSONArray params = new JSONArray();
+
JSONObject parent = new JSONObject();
for (int i = 0; i < paramObjects.size(); i++) {
JSONObject content = new JSONObject();
ex.printStackTrace();
}
}
-
- private SSLSocketFactory newSslSocketFactory(String destIP) {
- try {
- // Load CAs from an InputStream
- CertificateFactory cf = CertificateFactory.getInstance("X.509");
- InputStream caInput = new
- BufferedInputStream(MainActivity.context.getAssets().open(destIP + KEYEXT));
- Certificate ca;
- try {
- ca = cf.generateCertificate(caInput);
- System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
- } finally {
- caInput.close();
- }
- String keyStoreType = KeyStore.getDefaultType();
- KeyStore keyStore = KeyStore.getInstance(keyStoreType);
- keyStore.load(null, null);
- keyStore.setCertificateEntry("ca", ca);
- SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);
- return socketFactory;
- } catch (Exception e) {
- throw new AssertionError(e);
- }
- }
-
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
-import android.content.Context;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
- private EditText gatewayIP;
private EditText inchesPerWeek;
private EditText weatherZipCode;
private EditText daysToWaterOn;
private EditText inchesPerMinute;
private Button submit_button;
- protected static Context context;
private Helper helper = new Helper();
@Override
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- context = getApplicationContext();
- gatewayIP = (EditText) findViewById(R.id.gatewayip);
inchesPerWeek = (EditText) findViewById(R.id.week);
weatherZipCode = (EditText) findViewById(R.id.zip);
daysToWaterOn = (EditText) findViewById(R.id.water);
params.add(Integer.parseInt(daysToWaterOn.getText().toString()));
params.add(Double.parseDouble(inchesPerMinute.getText().toString()));
- String ip = gatewayIP.getText().toString();
+ String ip = getApplicationContext().getResources().getString(R.string.destination_ip);
new MakeRequestTask(params).execute(ip, "getIrrigationInfo");
}
});
}
-
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
@Override
protected Void doInBackground(String... argus) {
if (helper.httpclient == null) {
- helper.setConnection(argus[0]);
+ helper.setConnection();
}
-
helper.makeRequest(argus[0],params, argus[1]);
-
return null;
}
}
android:layout_weight="1"
android:ems="10"
android:inputType="number|numberDecimal"
- android:text="192.168.0.84" />
+ android:text="192.168.2.191" />
</RelativeLayout>
<resources>
<string name="app_name">Irrigation</string>
<string name="action_settings">Settings</string>
- <string name="destination_ip">192.168.2.244</string>
+ <string name="destination_ip">192.168.2.191</string>
</resources>
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:2.3.3'
+ classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
-#Tue Jan 16 09:22:38 PST 2018
-distributionBase=GRADLE_USER_HOME
-distributionPath=wrapper/dists
-zipStoreBase=GRADLE_USER_HOME
-zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
+#Fri Jan 19 14:40:37 PST 2018\r
+distributionBase=GRADLE_USER_HOME\r
+distributionPath=wrapper/dists\r
+zipStoreBase=GRADLE_USER_HOME\r
+zipStorePath=wrapper/dists\r
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip\r
android {
compileSdkVersion 23
- buildToolsVersion '25.0.0'
+ buildToolsVersion '26.0.2'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.example.xub3.speakerlocator"
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
- compile files('Libs/indoor-positioning-1.0.jar')
+ compile files('Libs/indoor-positioning-1.1.jar')
compile 'com.google.android.gms:play-services-appindexing:8.1.0'
+ implementation files('Libs/indoor-positioning-1.1.jar')
}
* Created by xub3 on 4/14/16.
*/
public class Helper {
- private static final int Driver_port = 8000;
- private static final String Tag = "CallReceiver";
- private static final String KEYEXT = ".pem";
- HttpClient httpclient;
- //Set up
- //Set up
- void setConnection(String destIP) {
-
- httpclient = createClient(destIP);
- }
-
- HttpClient createClient(String destIP) {
- HttpParams params = new BasicHttpParams();
- params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
- params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.DEFAULT_CONTENT_CHARSET);
- params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
- params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30 * 1000);
- params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30 * 1000);
-
- SchemeRegistry schReg = new SchemeRegistry();
- schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), Driver_port));
- schReg.register(new Scheme("https", newSslSocketFactory(destIP), 443));
- ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
-
- return new DefaultHttpClient(conMgr, params);
- }
-
- private SSLSocketFactory newSslSocketFactory(String destIP) {
- try {
- // Load CAs from an InputStream
- CertificateFactory cf = CertificateFactory.getInstance("X.509");
- InputStream caInput = new
- BufferedInputStream(MainActivity.context.getAssets().open(destIP + KEYEXT));
- Certificate ca;
- try {
- ca = cf.generateCertificate(caInput);
- System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
- } finally {
- caInput.close();
- }
- String keyStoreType = KeyStore.getDefaultType();
- KeyStore keyStore = KeyStore.getInstance(keyStoreType);
- keyStore.load(null, null);
- keyStore.setCertificateEntry("ca", ca);
- SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);
- return socketFactory;
- } catch (Exception e) {
- throw new AssertionError(e);
- }
- }
-
- //Make http request
- public void makeRequest(String destIP, Object contentStr, String methodName) {
- String url = "https://"+ destIP+":" + Driver_port + "/"+methodName;
- System.out.println("URL: " + url);
-
- InputStream inputStream = null;
- String result = "";
- StringBuilder sb = new StringBuilder();
- try {
- HttpPost httpPost = new HttpPost(url);
- JSONArray params = new JSONArray();
-
- JSONObject content = new JSONObject();
- JSONObject parent = new JSONObject();
- content.put("type", contentStr.getClass().getName());
- content.put("value", contentStr);
- params.put(0,content);
- parent.put("params", params);
- StringEntity se = new StringEntity(parent.toString());
- httpPost.setEntity(se);
- httpPost.setHeader("Accept", "application/json");
- httpPost.setHeader("Content-type", "application/json");
- HttpResponse httpResponse = httpclient.execute(httpPost);
- // 9. receive response as inputStream
- inputStream = httpResponse.getEntity().getContent();
-
- // 10. convert inputstream to string
- if(inputStream != null)
- result = convertInputStreamToString(inputStream);
- else
- result = "Did not work!";
- Log.v(Tag, result);
- } catch (Exception ex) {
- if (ex.getMessage() != null) {
- Log.v(Tag, ex.getMessage());
- }
- ex.printStackTrace();
- }
- }
- private static String convertInputStreamToString(InputStream inputStream) throws IOException{
- BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
- String line = "";
- String result = "";
- while((line = bufferedReader.readLine()) != null)
- result += line;
-
- inputStream.close();
- return result;
- }
+ private static final int Driver_port = 8000;
+ private static final String Tag = "CallReceiver";
+ HttpClient httpclient;
+ //Set up
+ void setConnection() {
+ httpclient = createClient();
+ }
+ HttpClient createClient() {
+ HttpParams params = new BasicHttpParams();
+ params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
+ params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.DEFAULT_CONTENT_CHARSET);
+ params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
+ params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30 * 1000);
+ params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30 * 1000);
+
+ SchemeRegistry schReg = new SchemeRegistry();
+ schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 8000));
+ ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
+
+ return new DefaultHttpClient(conMgr, params);
+ }
+
+ //Make http request
+ public void makeRequest(String destIP, Object contentStr, String methodName) {
+ String url = "http://"+ destIP+":" + Driver_port + "/"+methodName;
+
+
+ InputStream inputStream = null;
+ String result = "";
+ StringBuilder sb = new StringBuilder();
+ try {
+ HttpPost httpPost = new HttpPost(url);
+ JSONArray params = new JSONArray();
+
+ JSONObject content = new JSONObject();
+ JSONObject parent = new JSONObject();
+ content.put("type", contentStr.getClass().getName());
+ content.put("value", contentStr);
+ params.put(0,content);
+ parent.put("params", params);
+ StringEntity se = new StringEntity(parent.toString());
+ httpPost.setEntity(se);
+ httpPost.setHeader("Accept", "application/json");
+ httpPost.setHeader("Content-type", "application/json");
+ HttpResponse httpResponse = httpclient.execute(httpPost);
+ // 9. receive response as inputStream
+ inputStream = httpResponse.getEntity().getContent();
+
+ // 10. convert inputstream to string
+ if(inputStream != null)
+ result = convertInputStreamToString(inputStream);
+ else
+ result = "Did not work!";
+ Log.v(Tag, result);
+ } catch (Exception ex) {
+ if (ex.getMessage() != null) {
+ Log.v(Tag, ex.getMessage());
+ }
+ ex.printStackTrace();
+ }
+ }
+
+ private static String convertInputStreamToString(InputStream inputStream) throws IOException{
+ BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
+ String line = "";
+ String result = "";
+ while((line = bufferedReader.readLine()) != null)
+ result += line;
+
+ inputStream.close();
+ return result;
+
+ }
}
initializePositioning();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
tv = (TextView) findViewById(R.id.hellotext);
-// if (helper.httpclient == null) {
-// helper.setConnection(getApplicationContext().getResources().getString(R.string.gateway_ip));
-// }
+ if (helper.httpclient == null) {
+ helper.setConnection();
+ }
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(Environment.getExternalStorageDirectory(), "positioningPersistence.xml");
try {
positionManager = new PositionManager(file);
- //Log.d("positionManager", "initialized");
+ Log.d("positionManager", "initialized");
//System.out.println("PositionManager: Initialized successfully!");
} catch (PositioningPersistenceException e) {
e.printStackTrace();
<resources>
<string name="app_name">SpeakerLocator</string>
<string name="action_settings">Settings</string>
- <string name="gateway_ip">192.168.0.84</string>
+ <string name="gateway_ip">192.168.2.191</string>
<string name="mac_1">b0:b9:8a:73:69:f4</string>
<string name="mac_2">b0:b9:8a:73:69:f3</string>
<string name="mac_3">00:24:98:9a:92:ef</string>
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:2.3.3'
+ classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
-#Tue Jan 16 09:33:57 PST 2018
-distributionBase=GRADLE_USER_HOME
-distributionPath=wrapper/dists
-zipStoreBase=GRADLE_USER_HOME
-zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
+#Fri Jan 19 15:48:36 PST 2018\r
+distributionBase=GRADLE_USER_HOME\r
+distributionPath=wrapper/dists\r
+zipStoreBase=GRADLE_USER_HOME\r
+zipStorePath=wrapper/dists\r
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip\r
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
-import com.sun.net.httpserver.HttpsServer;
-import com.sun.net.httpserver.HttpsConfigurator;
-import com.sun.net.httpserver.HttpsParameters;
import java.io.BufferedReader;
import java.io.InputStreamReader;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
-import java.security.KeyStore;
-import javax.net.ssl.KeyManagerFactory;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLEngine;
-import javax.net.ssl.SSLParameters;
-import javax.net.ssl.TrustManagerFactory;
import java.lang.Class;
import java.lang.reflect.*;
final private Class _interface;
final private Object _callback;
final private int iPort;
- final private String strAddress;
private static final Logger logger = Logger.getLogger(IoTRemoteCall.class.getName());
/**
* IoTRemoteCall class constants
*/
private final String USER_AGENT = "Mozilla/5.0";
- private final String PASSWORD = "password";
- private final String KEYEXT = ".jks";
- private final String KEYTYPE = "SunX509";
- private final String KEYINSTANCE = "JKS";
/**
* Constructor
*/
- public IoTRemoteCall(Class _interface, Object _callback, int _iPort, String _strAddress) {
+ public IoTRemoteCall(Class _interface, Object _callback, int iPort) {
- this._interface = _interface;
- this._callback = _callback;
- this.iPort = _iPort;
- this.strAddress = _strAddress;
- startHttpsServer();
+ this._interface=_interface;
+ this._callback=_callback;
+ this.iPort=iPort;
+ startHttpServer();
}
/**
* Get Objects from a HTTP request
*/
- private void startHttpsServer() {
+ private void startHttpServer() {
// Run a separate thread as the HTTP server
IncomingMessageHandler imh=new IncomingMessageHandler(_interface, _callback);
try {
- HttpsServer server = HttpsServer.create(new InetSocketAddress(iPort), 0);
- SSLContext sslContext = SSLContext.getInstance("TLS");
-
- // initialise the keystore
- char[] password = PASSWORD.toCharArray();
- KeyStore ks = KeyStore.getInstance(KEYINSTANCE);
- FileInputStream fis = new FileInputStream(strAddress + KEYEXT);
- ks.load(fis, password);
-
- // setup the key manager factory
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYTYPE);
- kmf.init(ks, password);
-
- // setup the trust manager factory
- TrustManagerFactory tmf = TrustManagerFactory.getInstance(KEYTYPE);
- tmf.init(ks);
-
- // setup the HTTPS context and parameters
- sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
- server.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
- public void configure(HttpsParameters params) {
- try {
- // initialise the SSL context
- SSLContext c = SSLContext.getDefault();
- SSLEngine engine = c.createSSLEngine();
- params.setNeedClientAuth(false);
- params.setCipherSuites(engine.getEnabledCipherSuites());
- params.setProtocols(engine.getEnabledProtocols());
-
- // get the default parameters
- SSLParameters defaultSSLParameters = c.getDefaultSSLParameters();
- params.setSSLParameters(defaultSSLParameters);
-
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- });
-
+ HttpServer server = HttpServer.create(new InetSocketAddress(iPort), 0);
// Context name is according to method name, e.g. getRingStatus
Class<?> inter=_interface;
for (Method m:inter.getDeclaredMethods()) {
}
server.setExecutor(null); // creates a default executor
server.start();
- } catch (Exception ex) {
+ } catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println(uri);
try {
String strJSONString = sbResponse.toString();
- System.out.println(strJSONString);
Class[][] cr = new Class[1][];
Object[] params = decodeJSONArray(strJSONString,cr);
+ /* TODO: Uncomment this if we want to do HTTP gateway test
public static void main(String[] args) throws Exception {
Fooimpl fooimp = new Fooimpl();
//IoTRemoteCall iotremcall = new IoTRemoteCall(foo.class, new Fooimpl(), 8000);
new Thread() {
public void run() {
- IoTRemoteCall iotremcall = new IoTRemoteCall(foo.class, fooimp, 8000, "192.168.2.244");
+ IoTRemoteCall iotremcall = new IoTRemoteCall(foo.class, fooimp, 8000);
}
}.start();
System.out.println("server has started!");
// }
//}
- }
+ }*/
}
+