android连接蓝牙设备Bluetoot

 时间:2024-10-13 13:20:52

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连接蓝牙设备Bluetootandroid连接蓝牙设备Bluetoot

7、listview点击事件OnItemClickListenerOnClickListener

android连接蓝牙设备Bluetootandroid连接蓝牙设备Bluetoot

8、蓝牙连接

android连接蓝牙设备Bluetoot

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

android连接蓝牙设备Bluetoot

10、布局结构图<RelativeLayout xmlns:android="http://schemas.andro足毂忍珩id.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
  • windows11扫雷游戏在哪
  • 怎么解决校园网络又慢又卡的问题
  • win11录屏快捷键是什么
  • 如何让校园网网速变得更快
  • win11怎么显示文件扩展名
  • 热门搜索
    光遇雨林怎么过 怎么取消页眉页脚 中大网校怎么样 房间潮湿怎么办 阿沁是怎么成为网红的 优秀团员主要事迹怎么写 鱼是怎么睡觉的 老是干咳怎么办 咬肌怎么变小 假性近视怎么恢复