Enrollee support for BLE
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / android / EasySetup / app / src / main / java / org / iotivity / service / easysetup / BLEActivity.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 package org.iotivity.service.easysetup;
21
22 import android.app.Activity;
23 import android.bluetooth.BluetoothAdapter;
24 import android.bluetooth.BluetoothManager;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.view.Menu;
30 import android.view.MenuItem;
31 import android.widget.Toast;
32
33 import org.iotivity.service.easysetup.core.EasySetupService;
34 import org.iotivity.service.easysetup.core.EasySetupStatus;
35 import org.iotivity.service.easysetup.core.EnrolleeDevice;
36 import org.iotivity.service.easysetup.core.EnrolleeState;
37 import org.iotivity.service.easysetup.impl.BLEOnBoardingConfig;
38 import org.iotivity.service.easysetup.impl.EnrolleeDeviceFactory;
39 import org.iotivity.service.easysetup.impl.WiFiProvConfig;
40
41 import java.io.IOException;
42
43 public class BLEActivity extends Activity {
44     WiFiProvConfig mWiFiProvConfig;
45     BLEOnBoardingConfig bleOnBoardingConfig;
46     EasySetupService mEasySetupService;
47     EnrolleeDeviceFactory mDeviceFactory;
48     EnrolleeDevice mDevice;
49
50     private final int REQUEST_ENABLE_BT = 1;
51
52     @Override
53     protected void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         setContentView(R.layout.activity_ble);
56
57         mDeviceFactory = EnrolleeDeviceFactory.newInstance(getApplicationContext());
58
59         mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(), getEnrollerWifiConfig());
60
61         EasySetupStatus easySetupStatus = new EasySetupStatus() {
62             @Override
63             public void onFinished(EnrolleeDevice enrolleeDevice) {
64                 final String msg = mDevice.isSetupSuccessful() ? "Device configured successfully" : "Device configuration failed";
65                 runOnUiThread(new Runnable() {
66                     @Override
67                     public void run() {
68                         Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
69                     }
70                 });
71
72             }
73
74             @Override
75             public void onProgress(EnrolleeState enrolleeState) {
76                 runOnUiThread(new Runnable() {
77                     @Override
78                     public void run() {
79                         Toast.makeText(getApplicationContext(), "Device state changed", Toast.LENGTH_SHORT).show();
80                     }
81                 });
82
83
84             }
85         };
86         mEasySetupService = EasySetupService.getInstance(getApplicationContext(), easySetupStatus);
87         start();
88     }
89
90     public WiFiProvConfig getEnrollerWifiConfig() {
91         // SET the wifi credentials here
92         mWiFiProvConfig = new WiFiProvConfig("linksysy", "12345678");
93         return mWiFiProvConfig;
94     }
95
96     public BLEOnBoardingConfig getOnBoardingWifiConfig() {
97         // Set the uuid of the OIC device here
98         bleOnBoardingConfig = new BLEOnBoardingConfig();
99         bleOnBoardingConfig.setUuid("ade3d529-c784-4f63-a987-eb69f70ee816");
100
101         return bleOnBoardingConfig;
102     }
103
104
105     @Override
106     public boolean onCreateOptionsMenu(Menu menu) {
107         // Inflate the menu; this adds items to the action bar if it is present.
108         getMenuInflater().inflate(R.menu.menu_ble, menu);
109         return true;
110     }
111
112     @Override
113     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
114         // User chose not to enable Bluetooth.
115         if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
116             Log.e("error bluetooth", "Bluetooth not enabled..Try again");
117             Toast.makeText(BLEActivity.this, "Bluetooth not enabled..Try again", Toast.LENGTH_SHORT).show();
118             finish();
119             return;
120         } else try {
121             //bluetooth is  enabled, now start the setup of enrollee devices
122             mEasySetupService.startSetup(mDevice);
123         } catch (IOException e) {
124             e.printStackTrace();
125         }
126         super.onActivityResult(requestCode, resultCode, data);
127     }
128
129     @Override
130     public boolean onOptionsItemSelected(MenuItem item) {
131         // Handle action bar item clicks here. The action bar will
132         // automatically handle clicks on the Home/Up button, so long
133         // as you specify a parent activity in AndroidManifest.xml.
134         int id = item.getItemId();
135
136         //noinspection SimplifiableIfStatement
137         if (id == R.id.action_settings) {
138             return true;
139         }
140
141         return super.onOptionsItemSelected(item);
142     }
143
144     public void start() {
145         //This function starts the bluetooth adpater so that the easysetup can start scanning for BLE devices
146         final BluetoothManager bluetoothManager =
147                 (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
148         BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
149
150         if (!mBluetoothAdapter.isEnabled()) {
151             //Bluetooth is disabled, enable it
152             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
153             startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
154         } else try {
155             //IF bluetooth is directly enabled it will directly start the setup of enrollee devices
156             mEasySetupService.startSetup(mDevice);
157         } catch (IOException e) {
158             e.printStackTrace();
159         }
160     }
161 }