android连接蓝牙设备Bluetoot

2025-11-06 15:37:02

1、先展示代码结构

android连接蓝牙设备Bluetoot

2、连接蓝牙类

类名:MainActivity(有点偷懒,没有起表意的类名。)

android连接蓝牙设备Bluetoot

3、要声明的控件和变量等

android连接蓝牙设备Bluetoot

4、在onCreate声明控件

android连接蓝牙设备Bluetoot

5、ToogleButton设置开关状态

声明一个组件愿意接收

IntentFilter intent = new IntentFilter();

android连接蓝牙设备Bluetoot

6、BroadcastReceiver广播接收器

android连接蓝牙设备Bluetoot

android连接蓝牙设备Bluetoot

7、listview点击事件

OnItemClickListener

OnClickListener

android连接蓝牙设备Bluetoot

android连接蓝牙设备Bluetoot

8、蓝牙连接

android连接蓝牙设备Bluetoot

9、退出消耗页面是的onDestroy()

android连接蓝牙设备Bluetoot

10、布局结构图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.bluetooth_connection.MainActivity" >

    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >

        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="1"            android:orientation="horizontal" >

            <Button                android:id="@+id/btnSearch"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:text="搜索"                android:layout_weight="1" />

            <Button                android:id="@+id/btnExit"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:text="退出"                android:layout_weight="1" />

            <Button                android:id="@+id/btnDis"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:text="开启蓝牙"                android:layout_weight="1" />        </LinearLayout>

        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="1"            android:orientation="vertical" >

            <ToggleButton                android:id="@+id/tbtnSwitch"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_gravity="center_horizontal"                android:textOff="关闭蓝牙"                android:textOn="开启蓝牙" />        </LinearLayout>

        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="0.3"            android:orientation="vertical" >

            <ListView                android:id="@+id/lvDevices"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:background="#f1f1f1"                android:cacheColorHint="#ff333333"                android:fadingEdge="none"                android:scrollbars="none" >            </ListView>        </LinearLayout>    </LinearLayout>

</RelativeLayout>

android连接蓝牙设备Bluetoot

11、界面效果

android连接蓝牙设备Bluetoot

12、例子如下

android连接蓝牙设备Bluetoot

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢