最近在做一个安卓应用,其中有一个需求是要求用蓝牙连接打印机实现打印功能。一开始没有一点头绪,网上找了很多资料也找不到有用的数据。所以自己就去研究,最终,功夫不负有心人,顺利的完成了这个功能。下边贴出我写的代码,共有需要的IT哥们参考学习。
我们先看看运行效果图吧。。。
1.这是主界面的效果图
贴上布局文件的代码:bluetooth_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/openBluetooth_tb"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:layout_marginTop="5dp"
android:text="打开蓝牙" />
<Button
android:id="@+id/searchDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/openBluetooth_tb"
android:layout_marginTop="20dp"
android:text="搜索设备" />
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/searchDevices"
android:background="@android:color/darker_gray" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="125dp"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="未配对设备" />
<ListView
android:id="@+id/unbondDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/searchDevices"
android:layout_marginTop="160dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_marginTop="288dp"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="已配对设备" />
<ListView
android:id="@+id/bondDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1" >
</ListView>
</LinearLayout>
<Button
android:id="@+id/return_Bluetooth_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="@+id/searchDevices"
android:layout_alignParentLeft="true"
android:text="返回" />
</RelativeLayout>
从上边的布局文件中不难看出,其中有两个ListView,OK,那下边贴出对应的两个item布局文件
--> 第一个item:unbonddevice_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/device_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="未绑定设备"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
-->第二个item:bonddevice_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/device_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="已绑定设备"
andr