package com.moral.yunfushao.activity;
|
|
import android.Manifest;
|
import android.content.ComponentName;
|
import android.content.Context;
|
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.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 android.widget.LinearLayout;
|
|
import com.clj.fastble.data.ScanResult;
|
import com.moral.andbrickslib.baseadapter.headandfooter.DividerItemDecoration;
|
import com.moral.andbrickslib.baseadapter.recyclerview.MultiItemTypeAdapter;
|
import com.moral.andbrickslib.utils.ActivityManager;
|
import com.moral.yunfushao.MainApp;
|
import com.moral.yunfushao.R;
|
import com.moral.yunfushao.adapter.BleDeviceAdapter;
|
import com.moral.yunfushao.base.BaseActivity;
|
import com.moral.yunfushao.ble.BluetoothService;
|
import com.moral.yunfushao.common.AppConfig;
|
import com.moral.yunfushao.imageload.ImageLoader;
|
import com.moral.yunfushao.model.EventMessage;
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import java.util.ArrayList;
|
|
/**
|
* 蓝牙搜索
|
* Created by haijiang on 2017/7/5.
|
*/
|
|
public class BLESearchActivity extends BaseActivity {
|
private ImageView iv_searchpic, iv_search;
|
private LinearLayout ll_tishi;
|
private RecyclerView rv_ble;
|
private BluetoothService mBluetoothService;
|
private boolean isScan = false;
|
private ArrayList<ScanResult> scanResultList = new ArrayList<>();
|
private BleDeviceAdapter adapter;
|
|
@Override
|
protected void getBundleExtras(Bundle extras) {
|
|
}
|
|
@Override
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
bindService();
|
}
|
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
if (mBluetoothService != null)
|
unbindService();
|
EventMessage msg = new EventMessage();
|
msg.setType(AppConfig.SET_MAIN_CALL);
|
EventBus.getDefault().postSticky(msg);
|
}
|
|
@Override
|
protected int getLayoutId() {
|
return R.layout.ac_ble_search_layout;
|
}
|
|
@Override
|
protected void initViews() {
|
initTopBar();
|
tv_title.setText("设备搜索");
|
iv_searchpic = findView(R.id.iv_searchpic);
|
iv_search = findView(R.id.iv_search);
|
ll_tishi = findView(R.id.ll_tishi);
|
rv_ble = findView(R.id.rv_ble);
|
|
adapter = new BleDeviceAdapter(rv_ble, R.layout.list_item_deviceble, scanResultList);
|
DividerItemDecoration line = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
|
rv_ble.addItemDecoration(line);
|
rv_ble.setLayoutManager(new LinearLayoutManager(rv_ble.getContext()));
|
rv_ble.setAdapter(adapter);
|
adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
|
@Override
|
public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
|
/**
|
* 连接蓝牙设备
|
*/
|
if (mBluetoothService != null) {
|
mBluetoothService.cancelScan();
|
/**
|
* 如果连接状态,断开重新连接
|
*/
|
if (MainApp.theApp.bleManager != null) {
|
MainApp.theApp.bleManager.closeBluetoothGatt();
|
}
|
sendEventMessage(AppConfig.BLUETOOCH_CHANGE_CONNECT);
|
mBluetoothService.connectDevice(scanResultList.get(position));
|
}
|
}
|
|
@Override
|
public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
|
return false;
|
}
|
});
|
}
|
|
@Override
|
protected void initListener() {
|
iv_search.setOnClickListener(this);
|
iv_searchpic.setOnClickListener(this);
|
}
|
|
@Override
|
protected void initData() {
|
|
}
|
|
@Override
|
protected void processClick(View view) {
|
switch (view.getId()) {
|
case R.id.iv_searchpic:
|
mBluetoothService.openBlueTooth();
|
if (isScan) {
|
if (mBluetoothService != null) {
|
mBluetoothService.cancelScan();
|
}
|
iv_search.setImageResource(R.mipmap.bt_start);
|
iv_searchpic.setImageResource(R.mipmap.bg_end);
|
} else {
|
ImageLoader.setGIFByUrl(this, R.mipmap.bg_searching, iv_searchpic);
|
iv_search.setImageResource(R.mipmap.bt_stop);
|
checkPermissions();
|
}
|
isScan = !isScan;
|
break;
|
case R.id.tv_left:
|
finish();
|
break;
|
}
|
|
}
|
|
@Override
|
protected void onErrorPageClick() {
|
|
}
|
|
private void bindService() {
|
Intent bindIntent = new Intent(this, BluetoothService.class);
|
this.bindService(bindIntent, mFhrSCon, Context.BIND_AUTO_CREATE);
|
}
|
|
private void unbindService() {
|
// mBluetoothService.setScanCallback(null);
|
this.unbindService(mFhrSCon);
|
}
|
|
private ServiceConnection mFhrSCon = new ServiceConnection() {
|
@Override
|
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) {
|
if (mBluetoothService != null) {
|
mBluetoothService.cancelScan();
|
mBluetoothService.closeConnect();
|
mBluetoothService = null;
|
}
|
}
|
};
|
|
private BluetoothService.Callback callback = new BluetoothService.Callback() {
|
@Override
|
public void onStartScan() {
|
scanResultList.clear();
|
adapter.notifyDataSetChanged();
|
ll_tishi.setVisibility(View.GONE);
|
rv_ble.setVisibility(View.VISIBLE);
|
}
|
|
@Override
|
public void onScanning(ScanResult result) {
|
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);
|
}
|
|
@Override
|
public void onConnecting() {
|
if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
|
progressDialog.setTitleText("正在连接...");
|
progressDialog.show();
|
}
|
}
|
|
@Override
|
public void onConnectFail() {
|
if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
|
progressDialog.dismiss();
|
}
|
mToatUtils.showSingletonToast("连接失败");
|
sendEventMessage(AppConfig.GET_BLE_DISCONNECT);
|
}
|
|
@Override
|
public void onDisConnected() {
|
if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
|
progressDialog.dismiss();
|
}
|
Log.d("chonglian", "孕妇哨连接断开");
|
mToatUtils.showSingletonToast("孕妇哨连接断开");
|
|
}
|
|
@Override
|
public void onServicesDiscovered() {
|
Log.d("chonglian", "孕妇哨连接成功");
|
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 String[] permissions,
|
@NonNull int[] grantResults) {
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
switch (requestCode) {
|
case 200:
|
onPermissionGranted();
|
break;
|
}
|
}
|
|
private void checkPermissions() {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
!= PackageManager.PERMISSION_GRANTED) {//未开启定位权限
|
//开启定位权限,200是标识码
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
|
} else {
|
if (!isLocationEnable(this)){
|
Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
this.startActivityForResult(locationIntent, 2);
|
}
|
onPermissionGranted();
|
}
|
}
|
|
@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();
|
}
|
}
|
}
|