安卓手机连接蓝牙打印机实现打印功能

本文介绍了一个安卓应用如何通过蓝牙连接打印机实现打印功能。作者详细分享了自己的研发过程,并提供了关键代码片段,包括布局文件、事件处理类和权限设置,旨在帮助其他开发者参考学习。
摘要由CSDN通过智能技术生成

最近在做一个安卓应用,其中有一个需求是要求用蓝牙连接打印机实现打印功能。一开始没有一点头绪,网上找了很多资料也找不到有用的数据。所以自己就去研究,最终,功夫不负有心人,顺利的完成了这个功能。下边贴出我写的代码,共有需要的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
  • 31
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 95
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 95
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值