沈斌
2018-05-16 6c88cd89b3f3d6d140f6eb0cec20e2f57a7ff563
app/src/main/java/com/moral/yunfushao/activity/BLESearchActivity.java
@@ -14,6 +14,7 @@
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;
@@ -21,6 +22,8 @@
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;
@@ -64,6 +67,9 @@
        super.onDestroy();
        if (mBluetoothService != null)
            unbindService();
        EventMessage msg = new EventMessage();
        msg.setType(AppConfig.SET_MAIN_CALL);
        EventBus.getDefault().postSticky(msg);
    }
    @Override
@@ -93,6 +99,12 @@
                 */
                if (mBluetoothService != null) {
                    mBluetoothService.cancelScan();
                    /**
                     * 如果连接状态,断开重新连接
                     */
                    if (MainApp.theApp.bleManager != null) {
                        MainApp.theApp.bleManager.closeBluetoothGatt();
                    }
                    mBluetoothService.connectDevice(scanResultList.get(position));
                }
            }
@@ -149,6 +161,7 @@
    }
    private void unbindService() {
//        mBluetoothService.setScanCallback(null);
        this.unbindService(mFhrSCon);
    }
@@ -162,7 +175,11 @@
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBluetoothService = null;
            if (mBluetoothService != null) {
                mBluetoothService.cancelScan();
                mBluetoothService.closeConnect();
                mBluetoothService = null;
            }
        }
    };
@@ -189,29 +206,47 @@
        @Override
        public void onConnecting() {
            progressDialog.setTitleText("正在连接...");
            progressDialog.show();
            if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
                progressDialog.setTitleText("正在连接...");
                progressDialog.show();
            }
        }
        @Override
        public void onConnectFail() {
            progressDialog.dismiss();
            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() {
            progressDialog.dismiss();
            mToatUtils.showSingletonToast("连接断开");
            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() {
            progressDialog.dismiss();
            Log.d("chonglian","孕妇哨连接成功");
            EventMessage msg = new EventMessage();
            msg.setType(AppConfig.GET_BLE_SERVICE);
            EventBus.getDefault().post(msg);
            finish();
            EventBus.getDefault().postSticky(msg);
            if (ActivityManager.getActivityManager().isActivityExist(BLESearchActivity.this.getClass().getName())) {
                progressDialog.dismiss();
                finish();
            }
        }
    };