手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

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

7、listview点击事件OnItemClickListenerOnClickListener

android连接蓝牙设备Bluetoot
android连接蓝牙设备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
© 手抄报圈