an蟠校盯昂droid连接蓝牙设备,可以试着两个手机进行蓝牙连接测试。复杂的连接其他蓝牙设备,我们就不说了。(因为实现通信需要发送对应的指令集)
工具/原料
编译工具:Eclipse
方法/步骤
1、先展示代码结构
2、连接蓝牙类类名:MainActivity(有点偷懒,没有起表意的类名。)
3、要声明的控件和变量等
4、在onCreate声明控件
5、ToogleButton设置开关状态声明一个组件愿意接收IntentFil隋茚粟胫terintent=newIntentFilter();
6、BroadcastReceiver广播接收器
7、listview点击事件OnItemClickListenerOnClickListener
8、蓝牙连接
9、退出消耗页面是的onDestroy()
10、布局结构图<RelativeLay艺皱麾酪outxmlns: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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/btnSearch"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="搜索"android:layout_weight="1"/><Buttonandroid:id="@+id/btnExit"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="退出"android:layout_weight="1"/><Buttonandroid:id="@+id/btnDis"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="开启蓝牙"android:layout_weight="1"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:orientation="vertical"><ToggleButtonandroid:id="@+id/tbtnSwitch"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_gravity="center_horizontal"android:textOff="关闭蓝牙"android:textOn="开启蓝牙"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="0.3"android:orientation="vertical"><ListViewandroid: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>
11、界面效果
12、例子如下