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 | 135 ++++++++++++++++++++++++++++----------------
1 files changed, 86 insertions(+), 49 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 6d0d0c4..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,15 +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.os.Message;
+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;
@@ -36,7 +38,6 @@
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
-import java.util.List;
/**
* ������������
@@ -63,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);
@@ -106,6 +111,7 @@
if (MainApp.theApp.bleManager != null) {
MainApp.theApp.bleManager.closeBluetoothGatt();
}
+ sendEventMessage(AppConfig.BLUETOOCH_CHANGE_CONNECT);
mBluetoothService.connectDevice(scanResultList.get(position));
}
}
@@ -120,6 +126,7 @@
@Override
protected void initListener() {
iv_search.setOnClickListener(this);
+ iv_searchpic.setOnClickListener(this);
}
@Override
@@ -130,7 +137,8 @@
@Override
protected void processClick(View view) {
switch (view.getId()) {
- case R.id.iv_search:
+ case R.id.iv_searchpic:
+ mBluetoothService.openBlueTooth();
if (isScan) {
if (mBluetoothService != null) {
mBluetoothService.cancelScan();
@@ -171,12 +179,17 @@
public void onServiceConnected(ComponentName name, IBinder service) {
mBluetoothService = ((BluetoothService.BluetoothBinder) service).getService();
mBluetoothService.setScanCallback(callback);
+ mBluetoothService.openBlueTooth();
// mBluetoothService.scanDevice();
}
@Override
public void onServiceDisconnected(ComponentName name) {
- mBluetoothService = null;
+ if (mBluetoothService != null) {
+ mBluetoothService.cancelScan();
+ mBluetoothService.closeConnect();
+ mBluetoothService = null;
+ }
}
};
@@ -191,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);
}
@@ -214,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
@@ -226,19 +245,15 @@
if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
progressDialog.dismiss();
}
- Log.d("chonglian","���������������������");
+ 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);
+ Log.d("chonglian", "���������������������");
+ sendEventMessage(AppConfig.GET_BLE_SERVICE);
if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
progressDialog.dismiss();
finish();
@@ -247,50 +262,72 @@
}
};
+ private void sendEventMessage(int type) {
+ EventMessage msg = new EventMessage();
+ msg.setType(type);
+ EventBus.getDefault().postSticky(msg);
+ }
+
@Override
public final void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@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