STM32 蓝牙模块对应的 Android 列表页源码梳理

更新日期: 2023-02-09 阅读次数: 985 字数: 553 分类: Android

BlueST SDK

BlueST is a multi-platform library (Android and iOS supported) that permits easy access to the data exported by a Bluetooth Low Energy (BLE) device that implements the BlueST protocol.

相关名词

  • Manager:singleton class 用于启动/停止蓝牙设备扫描。发现节点之后,会通过 Manager.ManagerListener Class 异步通知。
  • Node:代表一个远端蓝牙设备。获取设备信息,读写数据都是通过这个类。建立连接之后,扫描及启用可用特性将被执行(自动执行?)
    • 获取 RSSI:Node.BleConnectionParamUpdateListener class
    • 监听设备状态变化:Node.NodeStateListener class
  • Feature:This class represents data exported by the node.

Activity 代码

STBlueMS_Android/BlueMS/src/main/java/com/st/BlueMS/MainActivity.java

@Override
public void startScanBleActivity(View view) {
    startActivity(new Intent(this, NodeListActivity.class));
}

STBlueMS_Android/BlueMS/src/main/java/com/st/BlueMS/NodeListActivity.java

package com.st.BlueMS;

import com.st.BlueNRG.fwUpgrade.BlueNRGAdvertiseFilter;
import com.st.BlueNRG.fwUpgrade.feature.BlueNRGOTASupport;
import com.st.BlueSTSDK.Features.standardCharacteristics.StdCharToFeatureMap;
import com.st.BlueSTSDK.Node;
import com.st.BlueSTSDK.Utils.BLENodeDefines;
import com.st.BlueSTSDK.Utils.ConnectionOption;
import com.st.BlueSTSDK.Utils.advertise.AdvertiseFilter;
import com.st.BlueSTSDK.gui.fwUpgrade.FwUpgradeActivity;
import com.st.BlueSTSDK.gui.util.SimpleFragmentDialog;
import com.st.STM32WB.fwUpgrade.FwUpgradeSTM32WBActivity;
import com.st.STM32WB.fwUpgrade.feature.STM32OTASupport;
import com.st.STM32WB.p2pDemo.Peer2PeerDemoConfiguration;

/**
 * Activity that show the list of device found by the manager
 */
public class NodeListActivity extends com.st.BlueSTSDK.gui.NodeListActivity {

    @Override
    public boolean displayNode(@NonNull Node n) {
        return true;
    }

    @Override
    protected List<AdvertiseFilter> buildAdvertiseFilter() {
        List<AdvertiseFilter> defaultList =  super.buildAdvertiseFilter();
        defaultList.add(new BlueNRGAdvertiseFilter());
        return defaultList;
    }
  • NodeListActivity 继承自 com.st.BlueSTSDK.gui.NodeListActivity。但是找不到 gui.NodeListActivity 的定义。。。这就诡异了。看来还是得上 Android Studio。上了 IDE 之后,就找到了 BlueSTSDK_Gui_Android\src\main\java\com\st\BlueSTSDK\gui\NodeListActivity.java,实际逻辑参考这里就行。
  • List Activity 中,除了 com.st.BlueSTSDK,还包含了 com.st.BlueNRG 和 com.st.STM32WB 这两个 package。grep 一下,没有找到。。。(对讲 detail activity 倒是没有这两个包)
  • 这部分代码需要浏览一遍

GUI activity 及 adapter 代码都是基于 SDK 实现的。

正式项目完全可以参考 GUI 部分的代码,复制过来,稍加修改。以实现功能,不要引入 GUI 的包依赖。

Layout 代码

sdk 中,并没有 layout 代码。

> tree BlueSTSDK/src/main/res/
BlueSTSDK/src/main/res/
├── menu
│   └── menu_log_feature.xml
├── values
│   ├── LogFeatureActivity.xml
│   ├── NodeScanActivity.xml
│   └── strings.xml
├── values-zh
│   ├── LogFeatureActivity.xml
│   ├── NodeScanActivity.xml
│   └── strings.xml
└── xml
    └── log_file_path.xml

SDK example 中也没有:

> tree BlueSTExample/src/main/res/layout/
BlueSTExample/src/main/res/layout/
├── activity_debug_console.xml
├── activity_demo.xml
├── activity_scan.xml
├── feature_list_item.xml
└── node_view_item.xml

最终在 BlueSTSDK_Gui_Android\src\main\java\com\st\BlueSTSDK\gui\NodeListActivity.java 中找到:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mManager = Manager.getSharedInstance();

    mAdapter = getNodeAdapter();

    setContentView(R.layout.activity_node_list);

    // Set the adapter
    RecyclerView recyclerView = findViewById(android.R.id.list);
    recyclerView.setAdapter(mAdapter);
    int nCol =getResources().getInteger(R.integer.nNodeListColum);
    if(nCol!=1){
        recyclerView.setLayoutManager(new GridLayoutManager(this,nCol));
    }

R.layout.activity_node_list

实际上就是一个 RecyclerView

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swiperRefreshDeviceList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:layoutManager="LinearLayoutManager"
            tools:listitem="@layout/node_list_item"/>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <TextView
        android:id="@+id/swype_to_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_margin="@dimen/text_margin"
        android:text="@string/swipeToUpdate" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_menu_search"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

TODO

  • [x] 相关名词整理
  • [x] 找到扫描附近蓝牙设备的 layout 代码,及 activity 代码

参考

  • https://github.com/STMicroelectronics/BlueSTSDK_Android/tree/master

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式