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;
|
import safeluck.drive.evaluation.customview.PullListView;
|
import safeluck.drive.evaluation.customview.PullToRefreshLayout;
|
|
/**
|
* DriveJudge
|
* Created by lzw on 2020/9/1. 11:40:50
|
* 邮箱:632393724@qq.com
|
* All Rights Saved! Chongqing AnYun Tech co. LTD
|
*/
|
public class BleScanFragment extends SupportFragment implements AdapterView.OnItemClickListener, PullToRefreshLayout.OnRefreshListener, View.OnClickListener {
|
|
public static int UPGRADE_17_FRONT = 1;
|
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() {
|
// listView 显示
|
if (mWifiList != null) {
|
if (mAdapterWifiList == null) {
|
mAdapterWifiList = new AdapterBleList(mWifiList);
|
mLvWifiList.setAdapter(mAdapterWifiList);
|
} else {
|
mAdapterWifiList.setList(mWifiList);
|
}
|
|
|
}
|
}
|
/**
|
* 初始化列表
|
*/
|
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);
|
|
mPullRefreshLayout = (PullToRefreshLayout) view.findViewById(R.id.pr_refresh);
|
mLvWifiList.setOnItemClickListener(this);
|
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
|
@Override
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
View view = inflater.inflate(R.layout.activity_auto_ble_list,container,false);
|
initView(view);
|
return view;
|
}
|
|
@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 ){
|
|
JSONObject jsonObject = new JSONObject();
|
try {
|
|
jsonObject.put("bluetooth_addr",ble.getAddress());
|
} catch (JSONException e) {
|
e.printStackTrace();
|
}
|
|
String json = jsonObject.toString();
|
Log.i(TAG,json);
|
Toast.makeText(_mActivity, "发送信息...", Toast.LENGTH_SHORT).show();
|
AYSdk.getInstance().sendCmd(Constant.ID_MS_BLUETOOTH_NAME,json);
|
}
|
}
|
}
|
|
@Override
|
public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
|
if (mAdapterWifiList != null) {
|
if (mWifiList != null) {
|
mWifiList.clear();
|
mAdapterWifiList.setList(mWifiList);
|
}
|
} else {
|
Log.d(TAG, "mAdapterWifilist == null");
|
}
|
if (mTvNoPro.getVisibility() != View.GONE) {
|
mTvNoPro.setVisibility(View.GONE);
|
}
|
BluetoothManager.getInstance().startDiscovery();
|
|
}
|
|
|
@Override
|
public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
|
new Handler() {
|
@Override
|
public void handleMessage(Message msg) {
|
mPullRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
|
}
|
}.sendEmptyMessageDelayed(0, 1000);
|
}
|
|
@Override
|
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 != null) {
|
if (device.getName() != null ){
|
|
Log.i(TAG, "device Name: " + device.getName());
|
}else{
|
Log.i(TAG, "device Name: 未知" );
|
}
|
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();
|
}
|
}
|
};
|
//注册蓝牙搜索结果的receiver(receiver, filter);
|
_mActivity.registerReceiver(receiver,filter);
|
}
|
|
@Override
|
public void onStop() {
|
super.onStop();
|
_mActivity.unregisterReceiver(receiver);
|
}
|
}
|