From c5bf501d8b507d1e3e79541e7ac9b45910860b4d Mon Sep 17 00:00:00 2001
From: 陈奇 <1650699704@qq.com>
Date: Wed, 29 May 2019 16:17:07 +0800
Subject: [PATCH] 添加新功能

---
 app/src/main/java/com/moral/yunfushao/activity/BLESearchActivity.java |  126 ++++++++++++++++++++++++++----------------
 1 files changed, 78 insertions(+), 48 deletions(-)

diff --git a/app/src/main/java/com/moral/yunfushao/activity/BLESearchActivity.java b/app/src/main/java/com/moral/yunfushao/activity/BLESearchActivity.java
index 913fd46..977703d 100644
--- a/app/src/main/java/com/moral/yunfushao/activity/BLESearchActivity.java
+++ b/app/src/main/java/com/moral/yunfushao/activity/BLESearchActivity.java
@@ -6,14 +6,17 @@
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.content.pm.PackageManager;
+import android.location.LocationManager;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.provider.Settings;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
-import android.support.v4.app.ActivityCompat;
+import android.support.v13.app.ActivityCompat;
 import android.support.v4.content.ContextCompat;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.View;
 import android.widget.ImageView;
@@ -35,7 +38,6 @@
 import org.greenrobot.eventbus.EventBus;
 
 import java.util.ArrayList;
-import java.util.List;
 
 /**
  * ������������
@@ -62,11 +64,15 @@
         bindService();
     }
 
+
     @Override
     protected void onDestroy() {
         super.onDestroy();
-        if (mBluetoothService != null)
+        if (mBluetoothService != null) {
+            mBluetoothService.cancelScan();
             unbindService();
+        }
+
         EventMessage msg = new EventMessage();
         msg.setType(AppConfig.SET_MAIN_CALL);
         EventBus.getDefault().postSticky(msg);
@@ -105,10 +111,8 @@
                     if (MainApp.theApp.bleManager != null) {
                         MainApp.theApp.bleManager.closeBluetoothGatt();
                     }
+                    sendEventMessage(AppConfig.BLUETOOCH_CHANGE_CONNECT);
                     mBluetoothService.connectDevice(scanResultList.get(position));
-                    EventMessage eventMessage = new EventMessage();
-                    eventMessage.setType(AppConfig.BLUETOOCH_CHANGE_CONNECT);
-                    EventBus.getDefault().post(eventMessage);
                 }
             }
 
@@ -121,7 +125,7 @@
 
     @Override
     protected void initListener() {
-//        iv_search.setOnClickListener(this);
+        iv_search.setOnClickListener(this);
         iv_searchpic.setOnClickListener(this);
     }
 
@@ -175,6 +179,7 @@
         public void onServiceConnected(ComponentName name, IBinder service) {
             mBluetoothService = ((BluetoothService.BluetoothBinder) service).getService();
             mBluetoothService.setScanCallback(callback);
+            mBluetoothService.openBlueTooth();
 //            mBluetoothService.scanDevice();
         }
 
@@ -199,12 +204,21 @@
 
         @Override
         public void onScanning(ScanResult result) {
-            scanResultList.add(result);
-            adapter.notifyDataSetChanged();
+            System.out.println("chenqi ScanResule " + result.getDevice().getName());
+            if (!TextUtils.isEmpty(result.getDevice().getName())) {
+                if (result.getDevice().getName().equals("���������") ||
+                        result.getDevice().getName().equals("yunfushao") ||
+                        result.getDevice().getName().equals("7drlb") ||
+                        result.getDevice().getName().contains("FEI-D")) {
+                    scanResultList.add(result);
+                    adapter.notifyDataSetChanged();
+                }
+            }
         }
 
         @Override
         public void onScanComplete() {
+            System.out.println("chenqi onScanComplete ");
             iv_search.setImageResource(R.mipmap.bt_start);
             iv_searchpic.setImageResource(R.mipmap.bg_end);
         }
@@ -222,11 +236,8 @@
             if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
                 progressDialog.dismiss();
             }
-//            Log.d("chonglian","������������");
             mToatUtils.showSingletonToast("������������");
-            EventMessage msg = new EventMessage();
-            msg.setType(AppConfig.GET_BLE_DISCONNECT);
-            EventBus.getDefault().postSticky(msg);
+            sendEventMessage(AppConfig.GET_BLE_DISCONNECT);
         }
 
         @Override
@@ -236,23 +247,26 @@
             }
             Log.d("chonglian", "���������������������");
             mToatUtils.showSingletonToast("���������������������");
-            EventMessage msg = new EventMessage();
-            msg.setType(AppConfig.GET_BLE_DISCONNECT);
-            EventBus.getDefault().postSticky(msg);
+
         }
 
         @Override
         public void onServicesDiscovered() {
             Log.d("chonglian", "���������������������");
-            EventMessage msg = new EventMessage();
-            msg.setType(AppConfig.GET_BLE_SERVICE);
-            EventBus.getDefault().postSticky(msg);
+            sendEventMessage(AppConfig.GET_BLE_SERVICE);
             if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
                 progressDialog.dismiss();
                 finish();
             }
+
         }
     };
+
+    private void sendEventMessage(int type) {
+        EventMessage msg = new EventMessage();
+        msg.setType(type);
+        EventBus.getDefault().postSticky(msg);
+    }
 
     @Override
     public final void onRequestPermissionsResult(int requestCode,
@@ -260,44 +274,60 @@
                                                  @NonNull int[] grantResults) {
         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
         switch (requestCode) {
-            case 12:
-                if (grantResults.length > 0) {
-                    for (int i = 0; i < grantResults.length; i++) {
-                        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
-                            onPermissionGranted(permissions[i]);
-                        }
-                    }
-                }
+            case 200:
+                onPermissionGranted();
                 break;
         }
     }
 
     private void checkPermissions() {
-        String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION};
-        List<String> permissionDeniedList = new ArrayList<>();
-        for (String permission : permissions) {
-            int permissionCheck = ContextCompat.checkSelfPermission(this, permission);
-            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
-                onPermissionGranted(permission);
-            } else {
-                permissionDeniedList.add(permission);
+        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
+                != PackageManager.PERMISSION_GRANTED) {//���������������������
+            //������������������,200������������
+            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 200);
+        } else {
+            if (!isLocationEnable(this)) {
+                Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
+                this.startActivityForResult(locationIntent, 2);
             }
-        }
-        if (!permissionDeniedList.isEmpty()) {
-            String[] deniedPermissions = permissionDeniedList.toArray(new String[permissionDeniedList.size()]);
-            ActivityCompat.requestPermissions(this, deniedPermissions, 12);
+            onPermissionGranted();
         }
     }
 
-    private void onPermissionGranted(String permission) {
-        switch (permission) {
-            case Manifest.permission.ACCESS_COARSE_LOCATION:
-                if (mBluetoothService == null) {
-                    bindService();
-                } else {
-                    mBluetoothService.scanDevice();
-                }
-                break;
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        if (requestCode == 2) {
+            if (isLocationEnable(this)) {
+                //������������������������
+                return;
+            } else {
+                Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
+                this.startActivityForResult(locationIntent, 2);
+                //���������������������������������
+            }
+        } else super.onActivityResult(requestCode, resultCode, data);
+    }
+
+    /**
+     * ������������������������������������������������
+     *
+     * @param context
+     * @return
+     */
+    public static final boolean isLocationEnable(Context context) {
+        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
+        boolean networkProvider = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
+        boolean gpsProvider = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
+        if (networkProvider || gpsProvider) return true;
+        return false;
+    }
+
+
+    private void onPermissionGranted() {
+        if (mBluetoothService == null) {
+            bindService();
+        } else {
+            mBluetoothService.scanDevice();
         }
     }
 }

--
Gitblit v1.8.0