| | |
| | | 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; |
| | |
| | | import org.greenrobot.eventbus.EventBus; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 蓝牙搜索 |
| | |
| | | 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); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | protected void initListener() { |
| | | // iv_search.setOnClickListener(this); |
| | | iv_search.setOnClickListener(this); |
| | | iv_searchpic.setOnClickListener(this); |
| | | } |
| | | |
| | |
| | | public void onServiceConnected(ComponentName name, IBinder service) { |
| | | mBluetoothService = ((BluetoothService.BluetoothBinder) service).getService(); |
| | | mBluetoothService.setScanCallback(callback); |
| | | mBluetoothService.openBlueTooth(); |
| | | // mBluetoothService.scanDevice(); |
| | | } |
| | | |
| | |
| | | |
| | | @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); |
| | | } |
| | |
| | | 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 |
| | |
| | | } |
| | | 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, |
| | |
| | | @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(); |
| | | } |
| | | } |
| | | } |