From cd1cf2bf44b9cb23408c21fac24a7e7285d1bf76 Mon Sep 17 00:00:00 2001 From: Waruna Samarsinghe Date: Tue, 28 Jun 2016 16:04:59 +0530 Subject: [PATCH] Beacon Scaning --- .../sense/beacon/BeaconDetactorService.java | 17 ++++ .../sense/beacon/BeaconScanedData.java | 80 +++++++++++++++++++ .../sense/beacon/BeaconServiceUtility.java | 18 ++++- .../sense/beacon/MonitoringActivity.java | 48 ++++++++--- .../data/publisher/DataPublisherService.java | 16 ++++ .../android/sense/data/publisher/Event.java | 55 +++++++++++++ .../event/streams/SenseDataCollector.java | 2 + .../android/sense/util/SenseDataHolder.java | 14 +++- .../src/main/res/layout/activity_monitor.xml | 3 +- .../layout/app_bar_activity_select_sensor.xml | 14 ++-- 10 files changed, 247 insertions(+), 20 deletions(-) create mode 100644 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconScanedData.java diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconDetactorService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconDetactorService.java index 727559ac7..c3c66a52b 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconDetactorService.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconDetactorService.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.wso2.carbon.iot.android.sense.beacon; import android.app.NotificationManager; diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconScanedData.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconScanedData.java new file mode 100644 index 000000000..e33e3f8e2 --- /dev/null +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconScanedData.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.iot.android.sense.beacon; + +import java.util.Date; + + +public class BeaconScanedData { + + + private int beaconMajor;// Major + private int beaconMinor;//Minor + private String beaconProximity;// Proximity + private int beaconUuid;// Uuid + private long timestamp;// Timestamp + + BeaconScanedData(int beaconMajor, int beaconMinor,String beaconProximity,int beaconUuid) { + this.beaconMajor = beaconMajor; + this.beaconMinor = beaconMinor; + this.beaconProximity = beaconProximity; + this.beaconUuid = beaconUuid; + timestamp = new Date().getTime(); + + } + + public int getBeaconMajor() { + return beaconMajor; + } + + public void setBeaconMajor(int beaconMajor) { + this.beaconMajor = beaconMajor; + } + + public int getBeaconMinor() { + return beaconMinor; + } + + public void setBeaconMinor(int beaconMinor) { + this.beaconMinor = beaconMinor; + } + + public String getBeaconProximity() { + return beaconProximity; + } + + public void setBeaconProximity(String beaconProximity) { + this.beaconProximity = beaconProximity; + } + + public int getBeaconUuid() { + return beaconUuid; + } + + public void setBeaconUuid(int beaconUuid) { + this.beaconUuid = beaconUuid; + } + + public long getTimeStamp() { + return timestamp; + } + + public void setTimeStamp(long timeStamp) { + timestamp = timeStamp; + } +} diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconServiceUtility.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconServiceUtility.java index 7c0c82887..1a4271b5e 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconServiceUtility.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/BeaconServiceUtility.java @@ -8,7 +8,23 @@ import android.content.Context; import android.content.Intent; import org.altbeacon.beacon.BeaconConsumer; import org.altbeacon.beacon.BeaconManager; - +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ public class BeaconServiceUtility { private Context context; diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/MonitoringActivity.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/MonitoringActivity.java index 89ffcd3a9..b95512f4a 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/MonitoringActivity.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/MonitoringActivity.java @@ -41,6 +41,10 @@ import android.widget.TextView; import org.altbeacon.beacon.BeaconParser; import org.altbeacon.beacon.MonitorNotifier; import org.altbeacon.beacon.Region; +import org.wso2.carbon.iot.android.sense.beacon.BeaconScanedData; +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import agent.sense.android.iot.carbon.wso2.org.wso2_senseagent.R; @@ -56,6 +60,8 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { private BeaconServiceUtility beaconUtill = null; private BeaconManager iBeaconManager = BeaconManager.getInstanceForApplication(this); + BeaconScanedData beaconData; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -66,6 +72,15 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { list.setAdapter(adapter); inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + iBeaconManager.setRangeNotifier(new RangeNotifier() { + @Override + public void didRangeBeaconsInRegion(Collection beacons, Region region) { + for(Beacon beacon : beacons) { + Log.d(TAG, "UUID:" + beacon.getId1() + ", major:" + beacon.getId2() + ", minor:" + beacon.getId3() + ", Distance:" + beacon.getDistance() + ",RSSI" + beacon.getRssi() + ", TxPower" + beacon.getTxPower()); + } + } + }); + } @Override @@ -85,16 +100,22 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { super.onStop(); } + @Override public void onBeaconServiceConnect() { iBeaconManager.setRangeNotifier(new RangeNotifier() { @Override public void didRangeBeaconsInRegion(Collection iBeacons, Region region) { + for (Beacon beacon: iBeacons) { + Log.i(TAG, "This beacon has identifiers:"+beacon.getId1()+", "+beacon.getId2()+", "+beacon.getId3()); - arrayL.clear(); - arrayL.addAll((ArrayList) iBeacons); - // adapter.notifyDataSetChanged(); + + } + +// arrayL.clear(); +// arrayL.addAll((ArrayList) iBeacons); +// adapter.notifyDataSetChanged(); } }); @@ -163,21 +184,26 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { } else { holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null)); } - if (arrayL.get(position).getId1() != null) - holder.beacon_uuid.setText("UUID: " + arrayL.get(position).getServiceUuid()); + holder.beacon_uuid.setText("UUID: " + arrayL.get(position).getId1().toString().toUpperCase()); - holder.beacon_major.setText("Major: " + arrayL.get(position).getId1()); + holder.beacon_major.setText("Major: " + arrayL.get(position).getId2()); - holder.beacon_minor.setText(", Minor: " + arrayL.get(position).getId2()); + holder.beacon_minor.setText(" Minor: " + arrayL.get(position).getId3()); - holder.beacon_proximity.setText("Proximity: " + arrayL.get(position).getId3()); + double proximity = arrayL.get(position).getDistance(); + holder.beacon_proximity.setText("Proximity: " + (new BigDecimal(proximity).setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue())); - holder.beacon_rssi.setText(", Rssi: " + arrayL.get(position).getRssi()); + holder.beacon_rssi.setText(" Rssi: " + arrayL.get(position).getRssi()); - holder.beacon_txpower.setText(", TxPower: " + arrayL.get(position).getTxPower()); + holder.beacon_txpower.setText(" TxPower: " + arrayL.get(position).getTxPower()); holder.beacon_range.setText("" + arrayL.get(position).getDistance()); + beaconData = new BeaconScanedData(arrayL.get(position).getId2().toInt(), arrayL.get(position).getId3().toInt(),holder.beacon_uuid.toString(),arrayL.get(position).getRssi()); + SenseDataHolder.getBeaconScanedDataHolder().add(beaconData); + + + } catch (Exception e) { e.printStackTrace(); } @@ -194,6 +220,7 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { private TextView beacon_txpower; private TextView beacon_range; + public ViewHolder(View view) { beacon_uuid = (TextView) view.findViewById(R.id.BEACON_uuid); beacon_major = (TextView) view.findViewById(R.id.BEACON_major); @@ -203,6 +230,7 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { beacon_txpower = (TextView) view.findViewById(R.id.BEACON_txpower); beacon_range = (TextView) view.findViewById(R.id.BEACON_range); + view.setTag(this); } } diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java index 055bf13e1..f3b6e06de 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.java @@ -24,6 +24,7 @@ import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.wso2.carbon.iot.android.sense.beacon.BeaconScanedData; import org.wso2.carbon.iot.android.sense.data.publisher.mqtt.AndroidSenseMQTTHandler; import org.wso2.carbon.iot.android.sense.data.publisher.mqtt.transport.MQTTTransportHandler; import org.wso2.carbon.iot.android.sense.data.publisher.mqtt.transport.TransportHandlerException; @@ -148,6 +149,21 @@ public class DataPublisherService extends Service { } SenseDataHolder.resetSpeedDataHolder(); + //retrieve speed data. + List beaconDataMap = SenseDataHolder.getBeaconScanedDataHolder(); + if (!speedDataMap.isEmpty()) { + for (BeaconScanedData beaconData : beaconDataMap) { + Event event = new Event(); + event.setBeaconMajor(beaconData.getBeaconMajor()); + event.setBeaconMinor(beaconData.getBeaconMinor()); + event.setBeaconProximity(beaconData.getBeaconProximity()); + event.setBeaconUuid(beaconData.getBeaconUuid()); + + events.add(event); + } + } + SenseDataHolder.resetSpeedDataHolder(); + //retrieve words ProcessWords.cleanAndPushToWordMap(); List wordDatMap = SenseDataHolder.getWordDataHolder(); diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.java index 562bafc55..cbc627c24 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.java @@ -28,6 +28,12 @@ public class Event { private static float speed; private String turn; public static final float SPEED_LIMIT = 60; + private int beaconMajor; + private int beaconMinor; + private int beaconUuid; + private String beaconProximity; + + private int getBattery() { return battery; @@ -178,6 +184,9 @@ public class Event { return speed; } + + + public void setTurns(String turn) { this.type = "turn"; @@ -192,6 +201,46 @@ public class Event { return turn; } + public void setBeaconMajor(int beaconMajor) { + this.type = "beaconMajor"; + this.beaconMajor = beaconMajor; + } + + public int getBeaconMajor() { + this.type = "beaconMajor"; + return beaconMajor; + } + + public void setBeaconMinor(int beaconMinor) { + this.type = "beaconMinor"; + this.beaconMinor = beaconMinor; + } + + public int getBeaconMinor() { + this.type = "beaconMinor"; + return beaconMinor; + } + + public void setBeaconUuid(int beaconUuid) { + this.type = "beaconUuid"; + this.beaconUuid = beaconUuid; + } + + public int getBeaconUuid() { + this.type = "beaconUuid"; + return beaconUuid; + } + + public void setBeaconProximity(String beaconProximity) { + this.type = "beaconProximity"; + this.beaconProximity = beaconProximity; + } + + public String getBeaconProximity() { + this.type = "beaconProximity"; + return beaconProximity; + } + public JSONObject getEvent() throws JSONException { JSONObject jsonEvent = new JSONObject(); JSONObject jsonMetaData = new JSONObject(); @@ -218,6 +267,12 @@ public class Event { jsonPayloadData.put("speed_limit", getSpeed()); //} + //Beacon + jsonPayloadData.put("beacon_major", getBeaconMajor()); + jsonPayloadData.put("beacon_minor", getBeaconMinor()); + jsonPayloadData.put("beacon_proximity", getBeaconProximity()); + jsonPayloadData.put("beacon_uuid", getBeaconUuid()); + //turn jsonPayloadData.put("turn_way", getTurns()); //magnetic diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/SenseDataCollector.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/SenseDataCollector.java index 41a28a0ae..4899b29b8 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/SenseDataCollector.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/SenseDataCollector.java @@ -18,6 +18,7 @@ import android.content.Context; import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationDataReader; import org.wso2.carbon.iot.android.sense.event.streams.Sensor.SensorDataReader; import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedDataReader; +import org.wso2.carbon.iot.android.sense.beacon.MonitoringActivity; /** * This class triggered by service to collect the sensor data. @@ -39,6 +40,7 @@ public class SenseDataCollector { case SPEED: dr = new SpeedDataReader(ctx); break; + } if (dr != null) { Thread DataCollector = new Thread(dr); diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataHolder.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataHolder.java index 2d6868849..1d33225b9 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataHolder.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataHolder.java @@ -13,6 +13,7 @@ */ package org.wso2.carbon.iot.android.sense.util; +import org.wso2.carbon.iot.android.sense.beacon.BeaconScanedData; import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationData; import org.wso2.carbon.iot.android.sense.event.streams.Sensor.SensorData; import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedData; @@ -33,7 +34,7 @@ public class SenseDataHolder { private static List locationDataHolder; private static List wordDataHolder; private static List speedDataHolder; - + private static List beaconScanedDataHolder; //LocationData gps; @@ -80,6 +81,13 @@ public class SenseDataHolder { return speedDataHolder; } + public static List getBeaconScanedDataHolder(){ + if(beaconScanedDataHolder == null){ + beaconScanedDataHolder = new CopyOnWriteArrayList<>(); + } + return beaconScanedDataHolder; + } + public static void resetSensorDataHolder(){ sensorDataHolder = null; } @@ -100,5 +108,9 @@ public class SenseDataHolder { speedDataHolder = null; } + public static void resetBeaconScanedDataHolder() { + beaconScanedDataHolder = null; + } + } diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/res/layout/activity_monitor.xml b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/res/layout/activity_monitor.xml index 52e1b4566..b10638db6 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/res/layout/activity_monitor.xml +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/res/layout/activity_monitor.xml @@ -2,8 +2,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:padding="5dip" - tools:context=".RangingActivity" > + android:padding="5dip" > + android:src="@drawable/beacon"/> + android:src="@android:drawable/ic_input_add"/> + android:src="@drawable/mic"/> + +