forked from community/device-mgt-plugins
parent
f3f28c544f
commit
32c4a34283
@ -0,0 +1,105 @@
|
||||
package org.wso2.carbon.iot.android.sense.beacon;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.RemoteException;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
import agent.sense.android.iot.carbon.wso2.org.wso2_senseagent.R;
|
||||
|
||||
import org.altbeacon.beacon.BeaconConsumer;
|
||||
import org.altbeacon.beacon.BeaconManager;
|
||||
import org.altbeacon.beacon.MonitorNotifier;
|
||||
import org.altbeacon.beacon.Region;
|
||||
|
||||
public class BeaconDetactorService extends Service implements BeaconConsumer {
|
||||
|
||||
private BeaconManager iBeaconManager = BeaconManager.getInstanceForApplication(this);
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent arg0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
iBeaconManager.bind(this);
|
||||
|
||||
final Handler handler = new Handler();
|
||||
final Runnable runnable = new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
stopSelf();
|
||||
}
|
||||
};
|
||||
handler.postDelayed(runnable, 10000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
iBeaconManager.unbind(this);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBeaconServiceConnect() {
|
||||
iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
|
||||
@Override
|
||||
public void didEnterRegion(Region region) {
|
||||
Log.e("BeaconDetactorService", "didEnterRegion");
|
||||
generateNotification(BeaconDetactorService.this, region.getUniqueId()
|
||||
+ ": just saw this iBeacon for the first time");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didExitRegion(Region region) {
|
||||
Log.e("BeaconDetactorService", "didExitRegion");
|
||||
generateNotification(BeaconDetactorService.this, region.getUniqueId() + ": is no longer visible");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didDetermineStateForRegion(int state, Region region) {
|
||||
Log.e("BeaconDetactorService", "didDetermineStateForRegion:" + state);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
try {
|
||||
iBeaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Issues a notification to inform the user that server has sent a message.
|
||||
*/
|
||||
private static void generateNotification(Context context, String message) {
|
||||
|
||||
Intent launchIntent = new Intent(context, MonitoringActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
|
||||
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(
|
||||
0,
|
||||
new NotificationCompat.Builder(context).setWhen(System.currentTimeMillis())
|
||||
.setSmallIcon(R.drawable.beacon).setTicker(message)
|
||||
.setContentTitle(context.getString(R.string.app_name)).setContentText(message)
|
||||
.setContentIntent(PendingIntent.getActivity(context, 0, launchIntent, 0)).setAutoCancel(true)
|
||||
.build());
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package org.wso2.carbon.iot.android.sense.beacon;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import org.altbeacon.beacon.BeaconConsumer;
|
||||
import org.altbeacon.beacon.BeaconManager;
|
||||
|
||||
public class BeaconServiceUtility {
|
||||
|
||||
private Context context;
|
||||
private PendingIntent pintent;
|
||||
private AlarmManager alarm;
|
||||
private Intent iService;
|
||||
|
||||
public BeaconServiceUtility(Context context) {
|
||||
super();
|
||||
this.context = context;
|
||||
iService = new Intent(context, BeaconDetactorService.class);
|
||||
alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
pintent = PendingIntent.getService(context, 0, iService, 0);
|
||||
}
|
||||
|
||||
public void onStart(BeaconManager iBeaconManager, BeaconConsumer consumer) {
|
||||
|
||||
stopBackgroundScan();
|
||||
iBeaconManager.bind(consumer);
|
||||
|
||||
}
|
||||
|
||||
public void onStop(BeaconManager iBeaconManager, BeaconConsumer consumer) {
|
||||
|
||||
iBeaconManager.unbind(consumer);
|
||||
startBackgroundScan();
|
||||
|
||||
}
|
||||
|
||||
private void stopBackgroundScan() {
|
||||
|
||||
alarm.cancel(pintent);
|
||||
context.stopService(iService);
|
||||
}
|
||||
|
||||
private void startBackgroundScan() {
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.SECOND, 2);
|
||||
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 360000, pintent); // 6*60 * 1000
|
||||
context.startService(iService);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,212 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed 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.Manifest;
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.RemoteException;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.view.LayoutInflater;
|
||||
import android.widget.BaseAdapter;
|
||||
import org.altbeacon.beacon.BeaconConsumer;
|
||||
import org.altbeacon.beacon.BeaconManager;
|
||||
import org.altbeacon.beacon.Beacon;
|
||||
import org.altbeacon.beacon.RangeNotifier;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import org.altbeacon.beacon.BeaconParser;
|
||||
import org.altbeacon.beacon.MonitorNotifier;
|
||||
import org.altbeacon.beacon.Region;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import agent.sense.android.iot.carbon.wso2.org.wso2_senseagent.R;
|
||||
|
||||
public class MonitoringActivity extends Activity implements BeaconConsumer {
|
||||
protected static final String TAG = MonitoringActivity.class.getName();
|
||||
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
|
||||
private ListView list = null;
|
||||
private BeaconAdapter adapter = null;
|
||||
private ArrayList<Beacon> arrayL = new ArrayList<>();
|
||||
private LayoutInflater inflater;
|
||||
|
||||
private BeaconServiceUtility beaconUtill = null;
|
||||
private BeaconManager iBeaconManager = BeaconManager.getInstanceForApplication(this);
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_monitor);
|
||||
beaconUtill = new BeaconServiceUtility(this);
|
||||
list = (ListView) findViewById(R.id.list);
|
||||
adapter = new BeaconAdapter();
|
||||
list.setAdapter(adapter);
|
||||
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
beaconUtill.onStart(iBeaconManager, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
beaconUtill.onStop(iBeaconManager, this);
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBeaconServiceConnect() {
|
||||
|
||||
iBeaconManager.setRangeNotifier(new RangeNotifier() {
|
||||
@Override
|
||||
public void didRangeBeaconsInRegion(Collection<Beacon> iBeacons, Region region) {
|
||||
|
||||
arrayL.clear();
|
||||
arrayL.addAll((ArrayList<Beacon>) iBeacons);
|
||||
// adapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
|
||||
@Override
|
||||
public void didEnterRegion(Region region) {
|
||||
Log.e("BeaconDetactorService", "didEnterRegion");
|
||||
// logStatus("I just saw an iBeacon for the first time!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didExitRegion(Region region) {
|
||||
Log.e("BeaconDetactorService", "didExitRegion");
|
||||
// logStatus("I no longer see an iBeacon");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didDetermineStateForRegion(int state, Region region) {
|
||||
Log.e("BeaconDetactorService", "didDetermineStateForRegion");
|
||||
// logStatus("I have just switched from seeing/not seeing iBeacons: " + state);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
try {
|
||||
iBeaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
iBeaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private class BeaconAdapter extends BaseAdapter {
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
if (arrayL != null && arrayL.size() > 0)
|
||||
return arrayL.size();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Beacon getItem(int arg0) {
|
||||
return arrayL.get(arg0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int arg0) {
|
||||
return arg0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
try {
|
||||
ViewHolder holder;
|
||||
|
||||
if (convertView != null) {
|
||||
holder = (ViewHolder) convertView.getTag();
|
||||
} 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_major.setText("Major: " + arrayL.get(position).getId1());
|
||||
|
||||
holder.beacon_minor.setText(", Minor: " + arrayL.get(position).getId2());
|
||||
|
||||
holder.beacon_proximity.setText("Proximity: " + arrayL.get(position).getId3());
|
||||
|
||||
holder.beacon_rssi.setText(", Rssi: " + arrayL.get(position).getRssi());
|
||||
|
||||
holder.beacon_txpower.setText(", TxPower: " + arrayL.get(position).getTxPower());
|
||||
|
||||
holder.beacon_range.setText("" + arrayL.get(position).getDistance());
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
private class ViewHolder {
|
||||
private TextView beacon_uuid;
|
||||
private TextView beacon_major;
|
||||
private TextView beacon_minor;
|
||||
private TextView beacon_proximity;
|
||||
private TextView beacon_rssi;
|
||||
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);
|
||||
beacon_minor = (TextView) view.findViewById(R.id.BEACON_minor);
|
||||
beacon_proximity = (TextView) view.findViewById(R.id.BEACON_proximity);
|
||||
beacon_rssi = (TextView) view.findViewById(R.id.BEACON_rssi);
|
||||
beacon_txpower = (TextView) view.findViewById(R.id.BEACON_txpower);
|
||||
beacon_range = (TextView) view.findViewById(R.id.BEACON_range);
|
||||
|
||||
view.setTag(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
After Width: | Height: | Size: 23 KiB |
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:state_pressed="false">
|
||||
<shape android:shape="oval">
|
||||
<solid android:color="#fa09ad"/>
|
||||
</shape>
|
||||
</item>
|
||||
<item android:state_pressed="true">
|
||||
<shape android:shape="oval">
|
||||
<solid android:color="#c20586"/>
|
||||
</shape>
|
||||
</item>
|
||||
</selector>
|
After Width: | Height: | Size: 30 KiB |
@ -0,0 +1,22 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:padding="5dip"
|
||||
tools:context=".RangingActivity" >
|
||||
|
||||
<TextView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentTop="true"
|
||||
android:gravity="center_horizontal"
|
||||
android:text="Scanning..." />
|
||||
|
||||
<ListView
|
||||
android:id="@+id/list"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:divider="@android:color/darker_gray"
|
||||
android:dividerHeight="1dip" />
|
||||
|
||||
</RelativeLayout>
|
@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<Button
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="@drawable/round_button"
|
||||
android:gravity="center"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginStart="20dp"/>
|
||||
|
||||
|
||||
<Button
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="@drawable/round_button"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginStart="20dp"
|
||||
android:gravity="center" />
|
||||
|
||||
<Button
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="@drawable/round_button"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginStart="20dp"
|
||||
android:gravity="center" />
|
||||
|
||||
<Button
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="@drawable/round_button"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginStart="20dp"
|
||||
android:gravity="center" />
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,80 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@android:color/white"
|
||||
android:paddingBottom="10dip"
|
||||
android:paddingTop="10dip" >
|
||||
|
||||
<TextView
|
||||
android:id="@+id/BEACON_uuid"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:gravity="left|top" >
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/BEACON_major"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_below="@+id/BEACON_uuid"
|
||||
android:layout_marginTop="5dp"
|
||||
android:gravity="left|top" >
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/BEACON_minor"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/BEACON_uuid"
|
||||
android:layout_marginLeft="10dip"
|
||||
android:layout_marginTop="5dp"
|
||||
android:layout_toRightOf="@+id/BEACON_major"
|
||||
android:gravity="left|top" >
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/BEACON_proximity"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_below="@+id/BEACON_major"
|
||||
android:layout_marginTop="5dp"
|
||||
android:gravity="left|top" >
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/BEACON_rssi"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/BEACON_major"
|
||||
android:layout_marginLeft="10dip"
|
||||
android:layout_marginTop="5dp"
|
||||
android:layout_toRightOf="@+id/BEACON_proximity"
|
||||
android:gravity="left|top" >
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/BEACON_txpower"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/BEACON_major"
|
||||
android:layout_marginLeft="10dip"
|
||||
android:layout_marginTop="5dp"
|
||||
android:layout_toRightOf="@+id/BEACON_rssi"
|
||||
android:gravity="left|top" >
|
||||
</TextView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/BEACON_range"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/BEACON_proximity"
|
||||
android:layout_marginLeft="10dip"
|
||||
android:layout_marginTop="5dp"
|
||||
android:gravity="left|top" >
|
||||
</TextView>
|
||||
|
||||
</RelativeLayout>
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/webView1"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
/>
|
Loading…
Reference in new issue