小Demo是搜索蓝牙设备显示到ListView中的,布局和代码都简单清楚明了。
1.首先创建XML
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.yipai.scanandcontrol.MainActivity">
<TextView
android:id="@+id/blueName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="已连接的设备名称" />
<Button
android:id="@+id/scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="搜索蓝牙设备" />
<ListView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
2.创建ListView中的Item布局
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"