yy1717
2021-04-13 5d6bac072277e62113bedbe6ab608ea88866cd3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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);
    }
}