From c2fa86445e278305b967dbb3f90762986933ea4d Mon Sep 17 00:00:00 2001 From: Vinil Jain Date: Wed, 23 Sep 2015 10:14:41 +0530 Subject: [PATCH] Modified android mediator UI for BLE onBoarding Change-Id: I9e8fe6d3a3b3e49d3e16881b38d9e6683a1844da Signed-off-by: Vinil Jain Reviewed-on: https://gerrit.iotivity.org/gerrit/2973 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../iotivity/service/easysetup/BLEActivity.java | 263 +++++++++++++++------ .../app/src/main/res/layout/activity_ble.xml | 194 ++++++++++++++- .../service/easysetup/core/BleConnection.java | 8 + .../impl/EnrolleeDeviceBLEOnBoarding.java | 9 +- .../service/easysetup/mediator/ble/BLEManager.java | 5 +- .../sdk/mediator/android/jni/jni_easy_setup.cpp | 180 +++++++------- .../sdk/mediator/src/ble_provisioning.cpp | 4 +- 7 files changed, 498 insertions(+), 165 deletions(-) diff --git a/service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/java/org/iotivity/service/easysetup/BLEActivity.java b/service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/java/org/iotivity/service/easysetup/BLEActivity.java index 4ccbb42..be40bac 100644 --- a/service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/java/org/iotivity/service/easysetup/BLEActivity.java +++ b/service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/java/org/iotivity/service/easysetup/BLEActivity.java @@ -17,19 +17,12 @@ // limitations under the License. // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + package org.iotivity.service.easysetup; -import android.app.Activity; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothManager; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.Toast; +import java.io.IOException; +import org.iotivity.service.easysetup.core.BleConnection; import org.iotivity.service.easysetup.core.EasySetupService; import org.iotivity.service.easysetup.core.EasySetupStatus; import org.iotivity.service.easysetup.core.EnrolleeDevice; @@ -38,11 +31,50 @@ import org.iotivity.service.easysetup.impl.BLEOnBoardingConfig; import org.iotivity.service.easysetup.impl.EnrolleeDeviceFactory; import org.iotivity.service.easysetup.impl.WiFiProvConfig; -import java.io.IOException; +import android.app.Activity; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothManager; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; public class BLEActivity extends Activity { + + + /*Status to update the UI */ + public static final int SUCCESS = 0; + public static final int FAILED = 1; + public static final int STATE_CHANGED = 2; + + EditText mEnrolleeSsidText; + EditText mmEnrolleePasswordPassText; + + + TextView mDeviceNameTextView; + TextView mDeviceMacTextView; + TextView mDeviceUuidTextView; + + TextView mResultTextView; + ProgressBar mProgressbar; + Button mStartButton; + Button mStopButton; + Handler mHandler = new ThreadHandler(); + + /** + * Objects to be instantiated by the programmer + */ WiFiProvConfig mWiFiProvConfig; - BLEOnBoardingConfig bleOnBoardingConfig; + BLEOnBoardingConfig mBleOnBoardingConfig; EasySetupService mEasySetupService; EnrolleeDeviceFactory mDeviceFactory; EnrolleeDevice mDevice; @@ -54,59 +86,132 @@ public class BLEActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ble); - mDeviceFactory = EnrolleeDeviceFactory.newInstance(getApplicationContext()); + /* Initialize widgets to get user input for target network's SSID & password*/ + mEnrolleeSsidText = (EditText) findViewById(R.id.enrolleeSsid); + mmEnrolleePasswordPassText = (EditText) findViewById(R.id.enrolleePass); + mDeviceNameTextView = (TextView) findViewById(R.id.devicename); + mDeviceMacTextView = (TextView) findViewById(R.id.hardAddr); + mDeviceUuidTextView = (TextView) findViewById(R.id.uuid); + mResultTextView = (TextView) findViewById(R.id.status); + mProgressbar = (ProgressBar) findViewById(R.id.progressBar); + mStartButton = (Button) findViewById(R.id.startSetup); + mStopButton = (Button) findViewById(R.id.stopSetup); - mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(), getEnrollerWifiConfig()); + //default SSID and password + mEnrolleeSsidText.setText("hub2.4G"); + mmEnrolleePasswordPassText.setText("11112222"); + + /* Create Easy Setup Service instance*/ + mEasySetupService = EasySetupService.getInstance(getApplicationContext(), + new EasySetupStatus() { - EasySetupStatus easySetupStatus = new EasySetupStatus() { - @Override - public void onFinished(EnrolleeDevice enrolleeDevice) { - final String msg = mDevice.isSetupSuccessful() ? "Device configured successfully" : "Device configuration failed"; - runOnUiThread(new Runnable() { @Override - public void run() { - Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); + public void onFinished(final EnrolleeDevice enrolledevice) { + Log.i("BleActivity", "onFinished() is received " + enrolledevice + .isSetupSuccessful()); + if (enrolledevice.isSetupSuccessful()) { + mHandler.sendEmptyMessage(SUCCESS); + } else { + mHandler.sendEmptyMessage(FAILED); + } } - }); - } - - @Override - public void onProgress(EnrolleeState enrolleeState) { - runOnUiThread(new Runnable() { @Override - public void run() { - Toast.makeText(getApplicationContext(), "Device state changed", Toast.LENGTH_SHORT).show(); + public void onProgress(EnrolleeState state) { + Log.i("MainActivity", "onProgress() is received "); + mHandler.sendEmptyMessage(STATE_CHANGED); } + }); + /* Create EnrolleeDevice Factory instance*/ + mDeviceFactory = EnrolleeDeviceFactory.newInstance(getApplicationContext()); + + addListnerforStartES(); + addListenerForStopES(); + + } + + public void addListnerforStartES() { + + mStartButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + mProgressbar.setVisibility(View.VISIBLE); + mProgressbar.setIndeterminate(true); + mStartButton.setEnabled(false); + mResultTextView.setText(R.string.running); + mStopButton.setEnabled(true); + start(); } - }; - mEasySetupService = EasySetupService.getInstance(getApplicationContext(), easySetupStatus); - start(); + }); } + public void addListenerForStopES() { + mStopButton = (Button) findViewById(R.id.stopSetup); + + mStopButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View arg0) { + mStartButton.setEnabled(true); + mStopButton.setEnabled(false); + mResultTextView.setText(R.string.stopped); + mProgressbar.setIndeterminate(false); + mProgressbar.setVisibility(View.INVISIBLE); + mEasySetupService.stopSetup(mDevice); + } + }); + } + + public WiFiProvConfig getEnrollerWifiConfig() { - // SET the wifi credentials here - mWiFiProvConfig = new WiFiProvConfig("linksysy", "12345678"); + /* Provide the credentials for the Mediator Soft AP to be connected by Enrollee*/ + mWiFiProvConfig = new WiFiProvConfig(mEnrolleeSsidText.getText().toString(), + mmEnrolleePasswordPassText.getText().toString()); return mWiFiProvConfig; } public BLEOnBoardingConfig getOnBoardingWifiConfig() { // Set the uuid of the OIC device here - bleOnBoardingConfig = new BLEOnBoardingConfig(); - bleOnBoardingConfig.setUuid("ade3d529-c784-4f63-a987-eb69f70ee816"); + mBleOnBoardingConfig = new BLEOnBoardingConfig(); + /* + set the uuid of the OIC-device, so that the Easysetup API can find the device + */ + mBleOnBoardingConfig.setUuid("ade3d529-c784-4f63-a987-eb69f70ee816"); - return bleOnBoardingConfig; + return mBleOnBoardingConfig; } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_ble, menu); - return true; + public void onDestroy() { + super.onDestroy(); + /*Reset the Easy setup process*/ + if (mEasySetupService != null) { + mEasySetupService.finish(); + } + } + + public void start() { + //This function starts the bluetooth adpater so that the easysetup can start scanning for BLE devices + final BluetoothManager bluetoothManager = + (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); + BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter(); + + if (!mBluetoothAdapter.isEnabled()) { + //Bluetooth is disabled, enable it + Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); + } else try { + //IF bluetooth is directly enabled it will directly start the setup of enrollee devices + + /* Create a device using Factory instance*/ + mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(), + getEnrollerWifiConfig()); + mEasySetupService.startSetup(mDevice); + } catch (IOException e) { + e.printStackTrace(); + } } @Override @@ -115,10 +220,12 @@ public class BLEActivity extends Activity { if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) { Log.e("error bluetooth", "Bluetooth not enabled..Try again"); Toast.makeText(BLEActivity.this, "Bluetooth not enabled..Try again", Toast.LENGTH_SHORT).show(); - finish(); return; } else try { //bluetooth is enabled, now start the setup of enrollee devices + /* Create a device using Factory instance*/ + mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(), + getEnrollerWifiConfig()); mEasySetupService.startSetup(mDevice); } catch (IOException e) { e.printStackTrace(); @@ -126,36 +233,52 @@ public class BLEActivity extends Activity { super.onActivityResult(requestCode, resultCode, data); } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - return super.onOptionsItemSelected(item); - } + class ThreadHandler extends Handler { + @Override + public void handleMessage(Message msg) { + + switch (msg.what) { + case SUCCESS: { + + mProgressbar.setIndeterminate(false); + mStopButton.setEnabled(false); + mStartButton.setEnabled(true); + mProgressbar.setVisibility(View.INVISIBLE); + String resultMsg = "Device configured successfully"; + mResultTextView.setText(R.string.success); + + /* Update device information on the Ui */ + BleConnection connection = (BleConnection) mDevice + .getConnection(); + mDeviceNameTextView.setText(connection.getmDeviceName()); + mDeviceMacTextView.setText(connection.getMacaddress()); + mDeviceUuidTextView.setText(connection.getmServiceUUID()); + Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show(); + break; + } + case FAILED: { + + mProgressbar.setIndeterminate(false); + mStopButton.setEnabled(false); + mStartButton.setEnabled(true); + mProgressbar.setVisibility(View.INVISIBLE); + String resultMsg = "Device configuration failed"; + mResultTextView.setText(R.string.failed); + Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show(); + break; + } + + case STATE_CHANGED: { + String resultMsg = "Device state changed"; + Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show(); + break; + } + + } - public void start() { - //This function starts the bluetooth adpater so that the easysetup can start scanning for BLE devices - final BluetoothManager bluetoothManager = - (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); - BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter(); - if (!mBluetoothAdapter.isEnabled()) { - //Bluetooth is disabled, enable it - Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); - } else try { - //IF bluetooth is directly enabled it will directly start the setup of enrollee devices - mEasySetupService.startSetup(mDevice); - } catch (IOException e) { - e.printStackTrace(); } } + } diff --git a/service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/res/layout/activity_ble.xml b/service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/res/layout/activity_ble.xml index 033f9bc..c60ab01 100644 --- a/service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/res/layout/activity_ble.xml +++ b/service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/res/layout/activity_ble.xml @@ -1,18 +1,204 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + android:layout_gravity="center_horizontal" + android:layout_marginTop="5dp" + android:text="Device information" + android:textAppearance="?android:attr/textAppearanceMedium" /> + + + + + + + + + + + + + + + + + + + + + + +