加入蓝牙扫描界面和功能 并发送蓝牙信息给远程服务进行连接;展示 蓝牙状态
11个文件已修改
1个文件已添加
388 ■■■■ 已修改文件
app/src/main/java/safeluck/drive/evaluation/Constant.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/MainActivity.java 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/adapter/AdapterBleList.java 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/app.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/bean/AYBluetoothDevice.java 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/fragment/BleScanFragment.java 194 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/fragment/BluetoothManager.java 47 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/fragment/SysExamSettingFragment.java 20 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/res/layout/activity_auto_ble_list.xml 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/res/layout/item_wifi_list_auto.xml 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/res/layout/networktoolbar.xml 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/res/values/strings.xml 14 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/Constant.java
@@ -25,6 +25,13 @@
    public static final int FETCH_MAP_INFO = 0x0006;
    //8.推送地图
    public static final int PUSH_MAP_INFO = 0x8006;
    public static final int ID_MS_BLUETOOTH_NAME = 0x8020;
    public static final int ID_SM_BLUETOOTH_BRIEF    = 0x0022;
    public static final int PUSH_ROAD_MAP_INFO = 0x8013;
    //9.请求车辆轮廓信息
app/src/main/java/safeluck/drive/evaluation/MainActivity.java
@@ -73,12 +73,12 @@
    private RTKConfig mRTKConfig;//RTK配置信息
    private ImageView iv_rtk_status;
    private Gson gson = new Gson();
    String[] PERMISSIONS = new String[]{ Manifest.permission.ACCESS_FINE_LOCATION};
    String[] PERMISSIONS = new String[]{ Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE};
    RTKConfigViewModel rtkConfigViewModel;
    public ExamPlatformModel examPlatformModel;
    private TextView tv_sat_num,tv_work_platform,tv_network_time,
            tv_qf;
            tv_qf,tv_ble_status;
    public Button  btn_return;
    public View viewtitle;
@@ -106,6 +106,34 @@
                    e.printStackTrace();
                }
            }
            if (msgCode == Constant.ID_SM_BLUETOOTH_BRIEF){
                if (tv_ble_status != null){
                    try {
                        JSONObject jsonObject = new JSONObject((String) obj);
                        int bleStatus = jsonObject.getInt("bluetooth_stauts");
                        String des ="蓝牙关闭";
                        switch (bleStatus){
                            case 0:
                                des ="蓝牙关闭";
                                break;
                            case 1:
                                des ="蓝牙打开";
                                break;
                            case 2:
                                des ="蓝牙未连接";
                                break;
                            case 3:
                                des ="蓝牙连接";
                                break;
                        }
                        tv_ble_status.setText(des);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };
@@ -131,6 +159,7 @@
        tv_work_platform = findViewById(R.id.platform);
        tv_sat_num = findViewById(R.id.tv_sat_num_instatus);
        tv_ble_status = findViewById(R.id.tv_ble_status);
        tv_qf = findViewById(R.id.tv_qf);
        iv_rtk_status.getDrawable().setLevel(0);
        tv_network_time = findViewById(R.id.network_time);
app/src/main/java/safeluck/drive/evaluation/adapter/AdapterBleList.java
@@ -1,6 +1,7 @@
package safeluck.drive.evaluation.adapter;
import android.net.wifi.ScanResult;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -36,7 +37,7 @@
    }
    @Override
    public Object getItem(int position) {
    public AYBluetoothDevice getItem(int position) {
        if(mList != null && mList.size() > position){
            return mList.get(position);
        }else{
@@ -56,14 +57,21 @@
            holder = new ViewHolder();
            holder.mTvShow = (TextView) convertView.findViewById(R.id.tv_auto_wifi);
            holder.mTvShow_mac = (TextView) convertView.findViewById(R.id.tv_auto_mac);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mTvShow.setText(mList.get(position).SSID);
        String name = getItem(position).getBleDevice().getName();
        if (TextUtils.isEmpty(name)){
            name = "未知";
        }
        holder.mTvShow.setText(name);
        holder.mTvShow_mac.setText(getItem(position).getBleDevice().getAddress());
        return convertView;
    }
    class ViewHolder{
        public TextView mTvShow;
        public TextView mTvShow_mac;
    }
}
app/src/main/java/safeluck/drive/evaluation/app.java
@@ -56,6 +56,7 @@
import safeluck.drive.evaluation.bean.ExamPlatformData;
import safeluck.drive.evaluation.bean.RemoteRouteCollect;
import safeluck.drive.evaluation.bean.RouteCollect;
import safeluck.drive.evaluation.cEventCenter.CEvent;
import safeluck.drive.evaluation.cEventCenter.CEventCenter;
import safeluck.drive.evaluation.im.MessageProcessor;
import safeluck.drive.evaluation.platformMessage.AttachInfo;
@@ -264,6 +265,9 @@
            lastStr = strConent;
        }
        switch (cmd) {
            case Constant.ID_SM_BLUETOOTH_BRIEF:
                CEventCenter.dispatchEvent(Constant.BIND_CONNECT_RTK_TOPIC,cmd,0,json);
                break;
            case Constant.NDK_START:
app/src/main/java/safeluck/drive/evaluation/bean/AYBluetoothDevice.java
@@ -1,5 +1,7 @@
package safeluck.drive.evaluation.bean;
import android.bluetooth.BluetoothDevice;
/**
 * DriveJudge
 * Created by lzw on 2020/9/1. 13:44:50
@@ -7,9 +9,18 @@
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class AYBluetoothDevice {
    public String SSID;
    public AYBluetoothDevice(String s) {
        this.SSID= s;
    private  BluetoothDevice bleDevice;
    public AYBluetoothDevice(BluetoothDevice s) {
        this.bleDevice= s;
    }
    public BluetoothDevice getBleDevice() {
        return bleDevice;
    }
    public void setBleDevice(BluetoothDevice bleDevice) {
        this.bleDevice = bleDevice;
    }
}
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();
                }
            }
        };
        //注册蓝牙搜索结果的receiver(receiver, filter);
        _mActivity.registerReceiver(receiver,filter);
    }
    @Override
    public void onStop() {
        super.onStop();
        _mActivity.unregisterReceiver(receiver);
    }
}
app/src/main/java/safeluck/drive/evaluation/fragment/BluetoothManager.java
New file
@@ -0,0 +1,47 @@
package safeluck.drive.evaluation.fragment;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import safeluck.drive.evaluation.app;
import safeluck.drive.evaluation.bean.AYBluetoothDevice;
/**
 * DriveJudge
 * Created by lzw on 2020/11/17. 13:51:16
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class BluetoothManager {
    private static final BluetoothManager ourInstance = new BluetoothManager();
    private Context mContext;
    public static BluetoothManager getInstance() {
        return ourInstance;
    }
    AYBluetoothDevice ayBluetoothDevice;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothManager() {
        this.mContext = app.getAppContext();
        android.bluetooth.BluetoothManager manager = (android.bluetooth.BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = manager.getAdapter();
    }
    public boolean isBluetoothEnable(){
        return mBluetoothAdapter.isEnabled();
    }
    public void startDiscovery(){
        if (mBluetoothAdapter.isDiscovering()){
            mBluetoothAdapter.cancelDiscovery();
        }
        mBluetoothAdapter.startDiscovery();
    }
    public void cancleDiscovery(){
        mBluetoothAdapter.cancelDiscovery();
    }
}
app/src/main/java/safeluck/drive/evaluation/fragment/SysExamSettingFragment.java
@@ -46,6 +46,9 @@
        return view;
    }
    private void initView(View view) {
        mRecy = (RecyclerView) view.findViewById(R.id.recy);
        ((MainActivity)_mActivity).btn_return.setVisibility(View.VISIBLE);
@@ -57,10 +60,21 @@
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                Toast.makeText(_mActivity, position+"", Toast.LENGTH_SHORT).show();
                if (getParentFragment() instanceof  BaseSettingFragment){
                    ((BaseSettingFragment)getParentFragment()).start(ShopFragment.newInstance(items.get(position)));
                switch (position){
                    case 0:
                        if (getParentFragment() instanceof  BaseSettingFragment){
                            ((BaseSettingFragment)getParentFragment()).start(ShopFragment.newInstance(items.get(position)));
                        }
                        break;
                    case 1:
                        Toast.makeText(_mActivity, "蓝牙扫描", Toast.LENGTH_SHORT).show();
                        if (getParentFragment() instanceof  BaseSettingFragment){
                            ((BaseSettingFragment)getParentFragment()).start( BleScanFragment.newInstance());
                        }
                        break;
                }
            }
        });
app/src/main/res/layout/activity_auto_ble_list.xml
@@ -1,11 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_f6f8f8"
    android:orientation="vertical">
    <include layout="@layout/toolbar"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_bground"
        android:orientation="vertical"
        >
@@ -15,8 +22,8 @@
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginStart="15dp"
        android:text="@string/auto_act_to_select"
        android:textColor="@color/color_939393"
        android:text="@string/auto_act_to_refresh"
        android:textColor="#ffffffff"
        android:textSize="15sp" />
    <TextView
@@ -27,7 +34,7 @@
        android:gravity="center_vertical"
        android:paddingEnd="15dp"
        android:paddingStart="15dp"
        android:text="未检测到双享号Pro设备"
        android:text="未检测到蓝牙设备"
        android:textColor="@color/color_f76260"
        android:textSize="16sp"
        android:visibility="gone" />
@@ -54,4 +61,5 @@
        <include layout="@layout/view_load_more" />
    </safeluck.drive.evaluation.customview.PullToRefreshLayout>
</LinearLayout>
</LinearLayout>
app/src/main/res/layout/item_wifi_list_auto.xml
@@ -11,9 +11,19 @@
        android:paddingStart="15dp"
        android:paddingEnd="15dp"
        android:text="LIFE-2E78"
        android:textSize="16sp"
        android:textSize="18sp"
        android:gravity="center_vertical"
        android:id="@+id/tv_auto_wifi"
        android:textColor="@color/color_000000"/>    <TextView
        android:background="@color/color_ffffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="15dp"
        android:paddingEnd="15dp"
        android:text="4E:34:23:1A:25"
        android:textSize="14sp"
        android:gravity="center_vertical"
        android:id="@+id/tv_auto_mac"
        android:textColor="@color/color_000000"/>
    <View
        android:layout_width="wrap_content"
app/src/main/res/layout/networktoolbar.xml
@@ -40,6 +40,14 @@
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tv_ble_status"
                android:singleLine="true"
                android:layout_marginLeft="30dp"
                android:textColor="@android:color/white"
                android:text="蓝牙未连接"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:id="@+id/network_time"
                android:singleLine="true"
app/src/main/res/values/strings.xml
@@ -76,11 +76,11 @@
<string-array name="array_large_item_menu">
<!--    <item>灯光设置</item>-->
    <item>规则设置</item>
<!--    <item>扣分设置</item>
    <item>指令设置</item>
    <item>系统设置</item>
    <item>教学系统设置</item>
    <item>档位学习</item>-->
  <item>蓝牙扫描</item>
    <!--    <item>指令设置</item>
      <item>系统设置</item>
      <item>教学系统设置</item>
      <item>档位学习</item>-->
</string-array>
@@ -90,7 +90,7 @@
    <!-- 刷新 -->
    <string name="pull_to_refresh">下拉刷新</string>
    <string name="release_to_refresh">释放立即刷新</string>
    <string name="refreshing">正在刷新...</string>
    <string name="refreshing">正在扫描...</string>
    <string name="refresh_fail">刷新失败</string>
    <string name="pullup_to_load">上拉加载更多</string>
    <string name="release_to_load">释放立即加载</string>
@@ -100,6 +100,6 @@
    <string name="auto_act_to_select">请选择需要连接的蓝牙设备</string>
    <string name="auto_act_to_refresh">请尝试下拉刷新</string>
    <string name="auto_act_to_refresh">请尝试下拉刷新,进行扫描周围蓝牙设备</string>
</resources>