1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 package org.iotivity.service.easysetup;
23 import java.io.IOException;
25 import org.iotivity.service.easysetup.core.BleConnection;
26 import org.iotivity.service.easysetup.core.EasySetupService;
27 import org.iotivity.service.easysetup.core.EasySetupStatus;
28 import org.iotivity.service.easysetup.core.EnrolleeDevice;
29 import org.iotivity.service.easysetup.core.EnrolleeState;
30 import org.iotivity.service.easysetup.impl.BLEOnBoardingConfig;
31 import org.iotivity.service.easysetup.impl.EnrolleeDeviceFactory;
32 import org.iotivity.service.easysetup.impl.WiFiProvConfig;
34 import android.app.Activity;
35 import android.bluetooth.BluetoothAdapter;
36 import android.bluetooth.BluetoothManager;
37 import android.content.Context;
38 import android.content.Intent;
39 import android.os.Bundle;
40 import android.os.Handler;
41 import android.os.Message;
42 import android.util.Log;
43 import android.view.View;
44 import android.view.View.OnClickListener;
45 import android.widget.Button;
46 import android.widget.EditText;
47 import android.widget.ProgressBar;
48 import android.widget.TextView;
49 import android.widget.Toast;
51 public class BLEActivity extends Activity {
54 /*Status to update the UI */
55 public static final int SUCCESS = 0;
56 public static final int FAILED = 1;
57 public static final int STATE_CHANGED = 2;
59 EditText mEnrolleeSsidText;
60 EditText mmEnrolleePasswordPassText;
63 TextView mDeviceNameTextView;
64 TextView mDeviceMacTextView;
65 TextView mDeviceUuidTextView;
67 TextView mResultTextView;
68 ProgressBar mProgressbar;
71 Handler mHandler = new ThreadHandler();
74 * Objects to be instantiated by the programmer
76 WiFiProvConfig mWiFiProvConfig;
77 BLEOnBoardingConfig mBleOnBoardingConfig;
78 EasySetupService mEasySetupService;
79 EnrolleeDeviceFactory mDeviceFactory;
80 EnrolleeDevice mDevice;
82 private final int REQUEST_ENABLE_BT = 1;
85 protected void onCreate(Bundle savedInstanceState) {
86 super.onCreate(savedInstanceState);
87 setContentView(R.layout.activity_ble);
89 /* Initialize widgets to get user input for target network's SSID & password*/
90 mEnrolleeSsidText = (EditText) findViewById(R.id.enrolleeSsid);
91 mmEnrolleePasswordPassText = (EditText) findViewById(R.id.enrolleePass);
92 mDeviceNameTextView = (TextView) findViewById(R.id.devicename);
93 mDeviceMacTextView = (TextView) findViewById(R.id.hardAddr);
94 mDeviceUuidTextView = (TextView) findViewById(R.id.uuid);
95 mResultTextView = (TextView) findViewById(R.id.status);
96 mProgressbar = (ProgressBar) findViewById(R.id.progressBar);
97 mStartButton = (Button) findViewById(R.id.startSetup);
98 mStopButton = (Button) findViewById(R.id.stopSetup);
100 //default SSID and password
101 mEnrolleeSsidText.setText("hub2.4G");
102 mmEnrolleePasswordPassText.setText("11112222");
104 /* Create Easy Setup Service instance*/
105 mEasySetupService = EasySetupService.getInstance(getApplicationContext(),
106 new EasySetupStatus() {
109 public void onFinished(final EnrolleeDevice enrolledevice) {
110 Log.i("BleActivity", "onFinished() is received " + enrolledevice
111 .isSetupSuccessful());
112 if (enrolledevice.isSetupSuccessful()) {
113 mHandler.sendEmptyMessage(SUCCESS);
115 mHandler.sendEmptyMessage(FAILED);
120 public void onProgress(EnrolleeState state) {
121 Log.i("MainActivity", "onProgress() is received ");
122 mHandler.sendEmptyMessage(STATE_CHANGED);
127 /* Create EnrolleeDevice Factory instance*/
128 mDeviceFactory = EnrolleeDeviceFactory.newInstance(getApplicationContext());
130 addListnerforStartES();
131 addListenerForStopES();
136 public void addListnerforStartES() {
138 mStartButton.setOnClickListener(new OnClickListener() {
140 public void onClick(View v) {
141 mProgressbar.setVisibility(View.VISIBLE);
142 mProgressbar.setIndeterminate(true);
143 mStartButton.setEnabled(false);
144 mResultTextView.setText(R.string.running);
145 mStopButton.setEnabled(true);
151 public void addListenerForStopES() {
152 mStopButton = (Button) findViewById(R.id.stopSetup);
154 mStopButton.setOnClickListener(new OnClickListener() {
156 public void onClick(View arg0) {
157 mStartButton.setEnabled(true);
158 mStopButton.setEnabled(false);
159 mResultTextView.setText(R.string.stopped);
160 mProgressbar.setIndeterminate(false);
161 mProgressbar.setVisibility(View.INVISIBLE);
162 mEasySetupService.stopSetup(mDevice);
168 public WiFiProvConfig getEnrollerWifiConfig() {
169 /* Provide the credentials for the Mediator Soft AP to be connected by Enrollee*/
170 mWiFiProvConfig = new WiFiProvConfig(mEnrolleeSsidText.getText().toString(),
171 mmEnrolleePasswordPassText.getText().toString());
172 return mWiFiProvConfig;
175 public BLEOnBoardingConfig getOnBoardingWifiConfig() {
176 // Set the uuid of the OIC device here
177 mBleOnBoardingConfig = new BLEOnBoardingConfig();
179 set the uuid of the OIC-device, so that the Easysetup API can find the device
181 mBleOnBoardingConfig.setUuid("ade3d529-c784-4f63-a987-eb69f70ee816");
183 return mBleOnBoardingConfig;
187 public void onDestroy() {
189 /*Reset the Easy setup process*/
190 if (mEasySetupService != null) {
191 mEasySetupService.finish();
195 public void start() {
196 //This function starts the bluetooth adpater so that the easysetup can start scanning for BLE devices
197 final BluetoothManager bluetoothManager =
198 (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
199 BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
201 if (!mBluetoothAdapter.isEnabled()) {
202 //Bluetooth is disabled, enable it
203 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
204 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
206 //IF bluetooth is directly enabled it will directly start the setup of enrollee devices
208 /* Create a device using Factory instance*/
209 mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(),
210 getEnrollerWifiConfig());
211 mEasySetupService.startSetup(mDevice);
212 } catch (IOException e) {
218 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
219 // User chose not to enable Bluetooth.
220 if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
221 Log.e("error bluetooth", "Bluetooth not enabled..Try again");
222 Toast.makeText(BLEActivity.this, "Bluetooth not enabled..Try again", Toast.LENGTH_SHORT).show();
225 //bluetooth is enabled, now start the setup of enrollee devices
226 /* Create a device using Factory instance*/
227 mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(),
228 getEnrollerWifiConfig());
229 mEasySetupService.startSetup(mDevice);
230 } catch (IOException e) {
233 super.onActivityResult(requestCode, resultCode, data);
237 class ThreadHandler extends Handler {
239 public void handleMessage(Message msg) {
244 mProgressbar.setIndeterminate(false);
245 mStopButton.setEnabled(false);
246 mStartButton.setEnabled(true);
247 mProgressbar.setVisibility(View.INVISIBLE);
248 String resultMsg = "Device configured successfully";
249 mResultTextView.setText(R.string.success);
251 /* Update device information on the Ui */
252 BleConnection connection = (BleConnection) mDevice
254 mDeviceNameTextView.setText(connection.getmDeviceName());
255 mDeviceMacTextView.setText(connection.getMacaddress());
256 mDeviceUuidTextView.setText(connection.getmServiceUUID());
257 Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show();
262 mProgressbar.setIndeterminate(false);
263 mStopButton.setEnabled(false);
264 mStartButton.setEnabled(true);
265 mProgressbar.setVisibility(View.INVISIBLE);
266 String resultMsg = "Device configuration failed";
267 mResultTextView.setText(R.string.failed);
268 Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show();
272 case STATE_CHANGED: {
273 String resultMsg = "Device state changed";
274 Toast.makeText(getApplicationContext(), resultMsg, Toast.LENGTH_SHORT).show();