Quellcode durchsuchen

<增加定位功能,定时扫描蓝牙信标获取信息上传服务器>

weizhengliang vor 5 Jahren
Ursprung
Commit
2a1911cbe5

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>

+ 3 - 0
app/build.gradle

@@ -67,4 +67,7 @@ dependencies {
     implementation 'com.google.code.gson:gson:2.8.5'
 
     implementation 'com.alibaba:fastjson:1.2.46'
+
+    //ibeancon
+    implementation 'org.altbeacon:android-beacon-library:2.13+'
 }

+ 6 - 0
app/src/main/AndroidManifest.xml

@@ -14,6 +14,12 @@
 
     <uses-feature android:name="android.hardware.camera" />
     <uses-feature android:name="android.hardware.camera.autofocus" />
+    <uses-feature android:name="android.hardware.bluetooth_le"
+        android:required="true"/>
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+    <uses-permission android:name="android.permission.BLUETOOTH"/>
+    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
 
     <uses-permission android:name="android.permission.VIBRATE" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />

+ 110 - 2
app/src/main/java/com/wdkl/nursewatch/presentation/ui/activities/HomeActivity.java

@@ -1,10 +1,12 @@
 package com.wdkl.nursewatch.presentation.ui.activities;
 
 import android.Manifest;
+import android.bluetooth.BluetoothAdapter;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.graphics.Color;
+import android.location.LocationManager;
 import android.os.Build;
 import android.os.Handler;
 import android.os.Message;
@@ -15,6 +17,7 @@ import android.support.v4.content.ContextCompat;
 import android.telephony.PhoneStateListener;
 import android.telephony.SignalStrength;
 import android.telephony.TelephonyManager;
+import android.util.Log;
 import android.view.View;
 import android.widget.FrameLayout;
 import android.widget.LinearLayout;
@@ -37,14 +40,23 @@ import com.wdkl.nursewatch.utils.TimeUtil;
 import com.wdkl.nursewatch.utils.ToastUtil;
 import com.wdkl.nursewatch.utils.WebsocketUtil;
 
+import org.altbeacon.beacon.Beacon;
+import org.altbeacon.beacon.BeaconConsumer;
+import org.altbeacon.beacon.BeaconManager;
+import org.altbeacon.beacon.RangeNotifier;
+import org.altbeacon.beacon.Region;
 import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
 import butterknife.Bind;
 
-public class HomeActivity extends BaseActivity implements View.OnClickListener {
+public class HomeActivity extends BaseActivity implements View.OnClickListener, BeaconConsumer {
 
     @Bind(R.id.tv_home_times)
     TextView tv_home_times;
@@ -77,6 +89,16 @@ public class HomeActivity extends BaseActivity implements View.OnClickListener {
 
     List<MessageEntity> callList;
 
+    //ibeancon
+    private static final int REQUEST_ENABLE_BT = 1;
+    private static final int REQUEST_ENABLE_LOCATION = 1;
+    private static final long DEFAULT_FOREGROUND_SCAN_PERIOD = 2 * 1000L;
+    private static final long DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD = 10 * 1000L;
+    private BeaconManager beaconManager;
+    private List<Beacon> beaconList = new ArrayList<>();
+    private boolean isEnabledBT = false;
+    private boolean isEnabledLocation = false;
+
     @Override
     public int getLayoutId() {
         return R.layout.activity_home;
@@ -90,6 +112,12 @@ public class HomeActivity extends BaseActivity implements View.OnClickListener {
         if (PhoneUtils.isWatch(this)) {
             battery_view.setVisibility(View.VISIBLE);
             tv_home_network_power.setVisibility(View.VISIBLE);
+            checkBT();
+            checkLocation();
+            Log.d("wzl", "bt: " + isEnabledBT + ", location: " + isEnabledLocation);
+            if (isEnabledBT && isEnabledLocation) {
+                initBeancon();
+            }
         } else {
             battery_view.setVisibility(View.INVISIBLE);
             tv_home_network_power.setVisibility(View.INVISIBLE);
@@ -106,7 +134,9 @@ public class HomeActivity extends BaseActivity implements View.OnClickListener {
 
     }
 
-    String[] allPermissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.CHANGE_NETWORK_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
+    String[] allPermissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.CHANGE_NETWORK_STATE,
+                                           Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
+                                           Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
 
     public void applyPermission() {
         if (Build.VERSION.SDK_INT >= 23) {
@@ -148,6 +178,83 @@ public class HomeActivity extends BaseActivity implements View.OnClickListener {
 
     }
 
+    private void checkBT() {
+        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+        if (bluetoothAdapter != null) {
+            if (bluetoothAdapter.isEnabled()) {
+                isEnabledBT = true;
+            } else {
+                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
+                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
+            }
+        }
+    }
+
+    private void checkLocation() {
+        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
+        boolean gps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
+        boolean network = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
+        if (gps || network) {
+            isEnabledLocation = true;
+        } else {
+            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
+            startActivityForResult(intent, REQUEST_ENABLE_LOCATION);
+        }
+    }
+
+    private void initBeancon() {
+        beaconManager = BeaconManager.getInstanceForApplication(this);
+        beaconManager.setForegroundScanPeriod(DEFAULT_FOREGROUND_SCAN_PERIOD);
+        beaconManager.setForegroundBetweenScanPeriod(DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD);
+        beaconManager.bind(this);
+    }
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK) {
+            isEnabledBT = true;
+        }
+        if (requestCode == REQUEST_ENABLE_LOCATION && resultCode == RESULT_OK) {
+            isEnabledLocation = true;
+        }
+
+        if (isEnabledBT && isEnabledLocation) {
+            initBeancon();
+        }
+    }
+
+    @Override
+    public void onBeaconServiceConnect() {
+        beaconManager.removeAllRangeNotifiers();
+        beaconManager.addRangeNotifier(new RangeNotifier() {
+            @Override
+            public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
+                Log.d("wzl", "scan beancon result: " + collection.size());
+                if (collection.size() > 0) {
+                    beaconList.clear();
+                    beaconList.addAll(collection);
+                    if (beaconList.size() > 1) {
+                        Collections.sort(beaconList, new Comparator<Beacon>() {
+                            @Override
+                            public int compare(Beacon beacon, Beacon t1) {
+                                return (beacon.getDistance() + "").compareTo(t1.getDistance() + "");
+                            }
+                        });
+                    }
+                    //蓝牙信标信息上传服务器
+                    //...
+                }
+            }
+        });
+
+        try {
+            beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
     final int TIME_WHAT = 1000;
 
     public Handler handler = new Handler() {
@@ -229,6 +336,7 @@ public class HomeActivity extends BaseActivity implements View.OnClickListener {
         if (null != batteryBroadcastReceiver) {
             unregisterReceiver(batteryBroadcastReceiver);
         }
+        beaconManager.unbind(this);
     }
 
     @Override