| | |
| | | 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; |
| | |
| | | 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() { |
| | |
| | | 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); |
| | |
| | | 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 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 |
| | |
| | | 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) { |
| | |
| | | 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(); |
| | | } |
| | | } |
| | | }; |
| | | //注册蓝牙搜索结果的receiver(receiver, filter); |
| | | _mActivity.registerReceiver(receiver,filter); |
| | | } |
| | | |
| | | @Override |
| | | public void onStop() { |
| | | super.onStop(); |
| | | _mActivity.unregisterReceiver(receiver); |
| | | } |
| | | } |