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.os.Bundle;
|
import android.os.IBinder;
|
import android.os.Message;
|
import android.support.annotation.NonNull;
|
import android.support.annotation.Nullable;
|
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.content.ContextCompat;
|
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.RecyclerView;
|
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;
|
import java.util.List;
|
|
/**
|
* 蓝牙搜索
|
* 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.isConnected()) {
|
mBluetoothService.closeConnect();
|
}
|
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);
|
}
|
|
@Override
|
protected void initData() {
|
|
}
|
|
@Override
|
protected void processClick(View view) {
|
switch (view.getId()) {
|
case R.id.iv_search:
|
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.scanDevice();
|
}
|
|
@Override
|
public void onServiceDisconnected(ComponentName name) {
|
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) {
|
scanResultList.add(result);
|
adapter.notifyDataSetChanged();
|
}
|
|
@Override
|
public void 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();
|
}
|
Log.d("chonglian","连接失败");
|
mToatUtils.showSingletonToast("连接失败");
|
EventMessage msg = new EventMessage();
|
msg.setType(AppConfig.GET_BLE_DISCONNECT);
|
EventBus.getDefault().postSticky(msg);
|
}
|
|
@Override
|
public void onDisConnected() {
|
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);
|
}
|
|
@Override
|
public void onServicesDiscovered() {
|
Log.d("chonglian","孕妇哨连接成功");
|
EventMessage msg = new EventMessage();
|
msg.setType(AppConfig.GET_BLE_SERVICE);
|
EventBus.getDefault().postSticky(msg);
|
if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
|
progressDialog.dismiss();
|
finish();
|
}
|
|
}
|
};
|
|
@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]);
|
}
|
}
|
}
|
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 (!permissionDeniedList.isEmpty()) {
|
String[] deniedPermissions = permissionDeniedList.toArray(new String[permissionDeniedList.size()]);
|
ActivityCompat.requestPermissions(this, deniedPermissions, 12);
|
}
|
}
|
|
private void onPermissionGranted(String permission) {
|
switch (permission) {
|
case Manifest.permission.ACCESS_COARSE_LOCATION:
|
if (mBluetoothService == null) {
|
bindService();
|
} else {
|
mBluetoothService.scanDevice();
|
}
|
break;
|
}
|
}
|
}
|