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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 package org.iotivity.service.easysetup;
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;
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;
41 import java.io.IOException;
43 public class BLEActivity extends Activity {
44 WiFiProvConfig mWiFiProvConfig;
45 BLEOnBoardingConfig bleOnBoardingConfig;
46 EasySetupService mEasySetupService;
47 EnrolleeDeviceFactory mDeviceFactory;
48 EnrolleeDevice mDevice;
50 private final int REQUEST_ENABLE_BT = 1;
53 protected void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
55 setContentView(R.layout.activity_ble);
57 mDeviceFactory = EnrolleeDeviceFactory.newInstance(getApplicationContext());
59 mDevice = mDeviceFactory.newEnrolleeDevice(getOnBoardingWifiConfig(), getEnrollerWifiConfig());
61 EasySetupStatus easySetupStatus = new EasySetupStatus() {
63 public void onFinished(EnrolleeDevice enrolleeDevice) {
64 final String msg = mDevice.isSetupSuccessful() ? "Device configured successfully" : "Device configuration failed";
65 runOnUiThread(new Runnable() {
68 Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
75 public void onProgress(EnrolleeState enrolleeState) {
76 runOnUiThread(new Runnable() {
79 Toast.makeText(getApplicationContext(), "Device state changed", Toast.LENGTH_SHORT).show();
86 mEasySetupService = EasySetupService.getInstance(getApplicationContext(), easySetupStatus);
90 public WiFiProvConfig getEnrollerWifiConfig() {
91 // SET the wifi credentials here
92 mWiFiProvConfig = new WiFiProvConfig("linksysy", "12345678");
93 return mWiFiProvConfig;
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");
101 return bleOnBoardingConfig;
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);
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();
121 //bluetooth is enabled, now start the setup of enrollee devices
122 mEasySetupService.startSetup(mDevice);
123 } catch (IOException e) {
126 super.onActivityResult(requestCode, resultCode, data);
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();
136 //noinspection SimplifiableIfStatement
137 if (id == R.id.action_settings) {
141 return super.onOptionsItemSelected(item);
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();
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);
155 //IF bluetooth is directly enabled it will directly start the setup of enrollee devices
156 mEasySetupService.startSetup(mDevice);
157 } catch (IOException e) {