From da23e781a4ed4c37e3772d02e8000d9b006d01b9 Mon Sep 17 00:00:00 2001
From: endian11 <Dana_Lee1016@126.com>
Date: 星期二, 24 十一月 2020 17:35:49 +0800
Subject: [PATCH] 加入蓝牙扫描界面和功能 并发送蓝牙信息给远程服务进行连接;展示 蓝牙状态
---
app/src/main/java/safeluck/drive/evaluation/fragment/BleScanFragment.java | 194 ++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 162 insertions(+), 32 deletions(-)
diff --git a/app/src/main/java/safeluck/drive/evaluation/fragment/BleScanFragment.java b/app/src/main/java/safeluck/drive/evaluation/fragment/BleScanFragment.java
index 223fc8b..0e77220 100644
--- a/app/src/main/java/safeluck/drive/evaluation/fragment/BleScanFragment.java
+++ b/app/src/main/java/safeluck/drive/evaluation/fragment/BleScanFragment.java
@@ -1,25 +1,42 @@
package safeluck.drive.evaluation.fragment;
+import android.app.Activity;
+import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
+import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
+import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
+import androidx.appcompat.widget.Toolbar;
+
+import com.anyun.exam.lib.AYSdk;
+
+import org.json.JSONException;
+import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
+import java.util.Set;
import me.yokeyword.fragmentation.SupportFragment;
+import safeluck.drive.evaluation.Constant;
+import safeluck.drive.evaluation.MainActivity;
import safeluck.drive.evaluation.R;
import safeluck.drive.evaluation.adapter.AdapterBleList;
import safeluck.drive.evaluation.bean.AYBluetoothDevice;
@@ -38,17 +55,25 @@
public static int UPGRADE_17_AFTER = 0;
+
+
+
private List<AYBluetoothDevice> mWifiList = new ArrayList<>();
private PullListView mLvWifiList;
private TextView mTvNoPro;
private final static String TAG = BleScanFragment.class.getSimpleName();
-
+ private Toolbar mToolbar;
private PullToRefreshLayout mPullRefreshLayout;
private String mSelectSsid;
private TextView mTvNotify;
private AdapterBleList mAdapterWifiList;
public static BleScanFragment newInstantce(){
return new BleScanFragment();
+ }
+
+ public static BleScanFragment newInstance() {
+ BleScanFragment bleScanFragment = new BleScanFragment();
+ return bleScanFragment;
}
private void initListView() {
@@ -61,23 +86,26 @@
mAdapterWifiList.setList(mWifiList);
}
- if (mWifiList.isEmpty()) {
- mTvNotify.setClickable(true);
- Spanned spanned = Html.fromHtml(getString(R.string.auto_act_to_refresh) + "<font color=\'#00ADEF\'>浣跨敤甯姪</font>");
- mTvNotify.setText(spanned);
- mTvNoPro.setVisibility(View.VISIBLE);
- } else {
- mTvNotify.setClickable(false);
- mTvNoPro.setVisibility(View.GONE);
- mTvNotify.setText(getString(R.string.auto_act_to_select));
- }
}
}
/**
* 鍒濆鍖栧垪琛�
*/
private void initView(View view) {
+
+ mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
+ mToolbar.setTitle("钃濈墮鎵弿");
+ if (HomeFragment.class.getSimpleName().equals(getTopFragment().getClass().getSimpleName())){
+ ((MainActivity)_mActivity).viewtitle.setVisibility(View.VISIBLE);
+ ((MainActivity)_mActivity).btn_return.setVisibility(View.VISIBLE);
+
+ }else{
+ ((MainActivity)_mActivity).viewtitle.setVisibility(View.GONE);
+ ((MainActivity)_mActivity).btn_return.setVisibility(View.GONE);
+ }
+ initToolbarNav(mToolbar);
+
mLvWifiList = (PullListView) view.findViewById(R.id.lv_wifi_list);
mTvNotify = (TextView) view.findViewById(R.id.tv_notify);
mTvNoPro = (TextView)view. findViewById(R.id.tv_no_pro);
@@ -87,8 +115,25 @@
mPullRefreshLayout.setOnRefreshListener(this);
mTvNotify.setOnClickListener(this);
+ initListView();
+ }
+ protected void initToolbarNav(Toolbar toolbar) {
+ toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
+ toolbar.setNavigationOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ pop();
+ }
+ });
+ }
+
+ @Override
+ public void onDetach() {
+ super.onDetach();
+ ((MainActivity)_mActivity).viewtitle.setVisibility(View.VISIBLE);
+ ((MainActivity)_mActivity).btn_return.setVisibility(View.VISIBLE);
}
@Nullable
@@ -101,7 +146,27 @@
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+ AYBluetoothDevice by =mWifiList.get(position);
+ if (by != null){
+ BluetoothDevice ble = by.getBleDevice();
+ if (ble!=null ){
+ String name = ble.getName();
+ if (TextUtils.isEmpty(name)){
+ name = "鏈煡";
+ }
+ JSONObject jsonObject = new JSONObject();
+ try {
+ jsonObject.put("bluetooth_name",name);
+ jsonObject.put("bluetooth_addr",ble.getAddress());
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ String json = jsonObject.toString();
+ Log.i(TAG,json);
+ AYSdk.getInstance().sendCmd(Constant.ID_MS_BLUETOOTH_NAME,json);
+ }
+ }
}
@Override
@@ -117,29 +182,10 @@
if (mTvNoPro.getVisibility() != View.GONE) {
mTvNoPro.setVisibility(View.GONE);
}
- new Handler() {
- @Override
- public void handleMessage(Message msg) {
- // 鍗冧竾鍒繕浜嗗憡璇夋帶浠跺埛鏂板畬姣曚簡鍝︼紒
- mPullRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
-
- addDatas();
- if (mWifiList != null) {
- initListView();
- } else {
-
- }
- Log.e(TAG, "onResume");
- }
- }.sendEmptyMessageDelayed(0, 1000);
- }
-
- private void addDatas() {
- for (int i = 0; i < 5; i++) {
- mWifiList.add(new AYBluetoothDevice("123"+i));
- }
+ BluetoothManager.getInstance().startDiscovery();
}
+
@Override
public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
@@ -155,4 +201,88 @@
public void onClick(View v) {
}
+private IntentFilter filter;
+ private BroadcastReceiver receiver;
+ @Override
+ public void onResume() {
+ super.onResume();
+ Log.i(TAG,"onResume");
+//骞挎挱杩囨护
+ filter = new IntentFilter();
+ filter.addAction(BluetoothDevice.ACTION_FOUND);
+ filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
+ filter.setPriority(Integer.MAX_VALUE);//璁剧疆骞挎挱鐨勪紭鍏堢骇鏈�澶�
+
+ receiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ try{
+ String action = intent.getAction();
+ Log.e(TAG,"鎺ユ敹鍒拌摑鐗欏箍鎾紒" + "action = " + action);
+ switch (action) {
+ case BluetoothDevice.ACTION_FOUND:
+ final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+ if (device.getName() != null && device != null) {
+ Log.i(TAG, "device Name: " + device.getName());
+ Log.i(TAG, "device Addr: " + device.getAddress());
+
+ _mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ Log.e(TAG, "寮�濮嬫坊鍔犺澶囷紒");
+ mWifiList.add(new AYBluetoothDevice(device));
+ mAdapterWifiList.notifyDataSetChanged();
+ }
+ });
+ }
+ break;
+ case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
+ System.out.println("ACTION_DISCOVERY_FINISHED");
+ Log.e(TAG, "鎵弿瀹屾垚锛�");
+ new Handler() {
+ @Override
+ public void handleMessage(Message msg) {
+ // 鍗冧竾鍒繕浜嗗憡璇夋帶浠跺埛鏂板畬姣曚簡鍝︼紒
+ mPullRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
+
+
+
+ }
+ }.sendEmptyMessageDelayed(0, 100);
+// if (mActivity instanceof Activity) {
+// mActivity.runOnUiThread(new Runnable() {
+// @Override
+// public void run() {
+// Log.e(TAG, "finished 寮�濮嬫坊鍔犺澶囷紒");
+// Set<BluetoothDevice> paired = adapter.getBondedDevices();
+// Log.e(TAG, "paired = " + paired);
+// if (paired != null && paired.size() > 0) {
+// for (BluetoothDevice bonded : paired) {
+// if (mlist.indexOf(bonded) == -1) {
+// mlist.add(bonded);
+// deviceAdapter.notifyDataSetChanged();
+// }
+// }
+// }
+// Log.e(TAG, "鎵弿瀹屾垚鍚庣殑list: " + mlist);
+// }
+// });
+// }
+// Log.e(TAG, "鎵弿瀹屾垚鍚庣殑list: " + mlist);
+ break;
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ }
+ };
+ //娉ㄥ唽钃濈墮鎼滅储缁撴灉鐨剅eceiver(receiver, filter);
+ _mActivity.registerReceiver(receiver,filter);
+ }
+
+ @Override
+ public void onStop() {
+ super.onStop();
+ _mActivity.unregisterReceiver(receiver);
+ }
}
--
Gitblit v1.8.0