| | |
| | | import com.moral.yunfushao.ui.PanelView; |
| | | import com.moral.yunfushao.utils.AudioUtils; |
| | | import com.moral.yunfushao.utils.DebugUtils; |
| | | import com.moral.yunfushao.utils.T; |
| | | |
| | | import org.greenrobot.eventbus.EventBus; |
| | | import org.greenrobot.eventbus.Subscribe; |
| | |
| | | |
| | | //心跳时间 |
| | | private int HEARTTIMES = 2; |
| | | //重连时间 |
| | | private int RECONNECTTIMES = 8000; |
| | | |
| | | //是否在本页面 |
| | | private boolean isMySelfPage = false; |
| | | |
| | | //handler管理线程 |
| | | private Handler handler = new Handler(); |
| | | |
| | | |
| | | private Runnable heartRunnable = new Runnable() { |
| | | @Override |
| | |
| | | FragmentMonitor.this.mBluetoothService.write(FragmentMonitor.this.mBluetoothService.getWirteCharacteristic().getService().getUuid().toString(), |
| | | FragmentMonitor.this.mBluetoothService.getWirteCharacteristic().getUuid().toString(), |
| | | BLECommon.BLE_HEART, null); |
| | | Log.d(TGA, "发送心跳"); |
| | | heart(); |
| | | } |
| | | }; |
| | | |
| | | |
| | | private Runnable reconnectRunnable = new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | System.out.println("chenqi 立刻重连"); |
| | | //重连 |
| | | if (FragmentMonitor.this.mBluetoothService == null) { |
| | | if (mBluetoothService == null) { |
| | | return; |
| | | } |
| | | String mac = MainApp.theApp.sharedPreferencesUtil.getBLE(); |
| | |
| | | if (MainApp.theApp.bleManager.isConnectingOrConnected()) { |
| | | return; |
| | | } |
| | | Log.d(TGA, "正在重连"); |
| | | FragmentMonitor.this.mBluetoothService.scanAndConnect5(mac); |
| | | mBluetoothService.scanAndConnect5(mac); |
| | | } |
| | | reconnect(); |
| | | } |
| | | }; |
| | | |
| | |
| | | */ |
| | | @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) |
| | | public void eventComing(EventMessage msg) { |
| | | mBluetoothService = ((MainActivity) getActivity()).getBluetoothService(); |
| | | if (msg.getType() == AppConfig.GET_BLE_SERVICE) { |
| | | resetHandler(); |
| | | pv.setStatusStr("检测中"); |
| | | //发现服务 |
| | | mBluetoothService = ((MainActivity) getActivity()).getBluetoothService(); |
| | | ((MainActivity) getActivity()).resetBLECallBack(); |
| | | if (mBluetoothService != null) { |
| | | BluetoothGatt gatt = mBluetoothService.getGatt(); |
| | |
| | | mBluetoothService.setService(service); |
| | | break; |
| | | } |
| | | } |
| | | //获取特征 |
| | | |
| | | }//获取特征 |
| | | for (BluetoothGattCharacteristic characteristic : mBluetoothService.getService().getCharacteristics()) { |
| | | int charaProp = characteristic.getProperties(); |
| | | if ((charaProp & BluetoothGattCharacteristic.PROPERTY_READ) > 0) { |
| | |
| | | |
| | | @Override |
| | | public void onFailure(final BleException exception) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onInitiatedResult(boolean result) { |
| | | } |
| | | }); |
| | | /** |
| | | * 发送时间校准 |
| | | */ |
| | | // handler.postDelayed(runnable, 5000); |
| | | //重置handler |
| | | resetHandler(); |
| | | //发送心跳 |
| | | heart(); |
| | | } |
| | | } else if (msg.getType() == AppConfig.GET_BLE_DISCONNECT) { |
| | | preLevel = -1; |
| | | showStatus(-1); |
| | | handler.postDelayed(reconnectRunnable, RECONNECTTIMES); |
| | | reconnect(); |
| | | stop(); |
| | | isVirating = false; |
| | | AudioUtils.virateCancle(getActivity()); |
| | | |
| | | } else if (msg.getType() == AppConfig.CHANGE_SETTING) { |
| | | if (timer != null) { |
| | | timer.cancel(); |
| | |
| | | } |
| | | } else if (msg.getType() == AppConfig.BLUETOOCH_CHANGE_CONNECT) { |
| | | showStatus(-1); |
| | | stopReconnect(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Override |
| | | public void onAttach(Context context) { |
| | | super.onAttach(context); |
| | | FragmentMonitor.this.mBluetoothService = ((MainActivity) getActivity()).getBluetoothService(); |
| | | mBluetoothService = ((MainActivity) getActivity()).getBluetoothService(); |
| | | } |
| | | |
| | | @Override |
| | | public void onResume() { |
| | | super.onResume(); |
| | | isMySelfPage = true; |
| | | reconnect(); |
| | | textShowMac(); |
| | | } |
| | | |
| | | @Override |
| | | public void onPause() { |
| | | super.onPause(); |
| | | //暂停的时候不检测 |
| | | T.show("onPause"); |
| | | isMySelfPage = false; |
| | | stopReconnect(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | }); |
| | | showStatus(-1); |
| | | |
| | | } |
| | | |
| | | @Override |
| | |
| | | handler.postDelayed(heartRunnable, 1000 * HEARTTIMES); |
| | | } |
| | | |
| | | /** |
| | | * 开始重连 |
| | | */ |
| | | private void reconnect() { |
| | | handler.postDelayed(reconnectRunnable, RECONNECTTIMES); |
| | | if (isMySelfPage) { |
| | | stopReconnect(); |
| | | //是否是在本页面 |
| | | handler.postDelayed(reconnectRunnable, 0); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 停止重连 |
| | | */ |
| | | private void stopReconnect() { |
| | | handler.removeCallbacks(reconnectRunnable); |
| | | if (mBluetoothService != null) |
| | | mBluetoothService.closeConnect(); |
| | | } |
| | | |
| | | private void resetHandler() { |
| | | handler.removeCallbacks(reconnectRunnable); |
| | | handler.removeCallbacks(heartRunnable); |
| | | HEARTTIMES = 2; |
| | | } |
| | | |
| | | private void submitData() { |
| | |
| | | |
| | | private void textShowMac() { |
| | | if (BuildConfig.BUILD_TYPE.equals(VersionConstants.DEBUG)) { |
| | | final TextView tv_text_mac = (TextView) findView(R.id.tv_text_mac); |
| | | final TextView tv_text_mac = findView(R.id.tv_text_mac); |
| | | tv_desc.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |