当前位置:首页 > 话题广场 > 问答专区 > 科技

关于淘宝热敏打印机怎么打印机,你需要知道这些蓝牙热敏打印机安卓源代码分析

最近在研究Android的蓝牙打印机程序,中间遇到了很多坑。请在这里分享我的经验。

先来看打印过程:

1、申请必要的权限

2、确定本机有蓝牙硬件,并且打开蓝牙适配器

3、开通广播接收功能,用于发现蓝牙设备回传的周围蓝牙设备

4、发送蓝牙发现信号广播,周边设备收到这个信号后,会回传自己的mac地址和设备名

5、通过广播收到周边蓝牙设备mac,通过mac地址,连接到指定的蓝牙打印机,获取socket。

6、通过socket发送GB2312编码的字符串,打印完成。

热敏打印机一般都自带字库,所以直接发送文字给打印机就可以打印出想要的东西,如果要打印其它字体、图片、二维码和条形码,只能通过ESC指令,把这些换算成坐标,通过写点的方式打印。

ESC打印控制指令,可以对文字大小、对齐进行各种控制,大家可以网上搜索具体指令。

部分源代码来源于网上整理。

请使用手机真机测试,并且打开需要的各种权限,特别是打开手机上的位置获取权限,因为这个我在手机上没有打开,一直没有收到设备广播数据,折腾了很久。

下面看源代码

AndroidMani

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="; xmlns:tools="; package="com.exam;> <uses-permission android:name="android.; /> <uses-permission android:name="android.; /> <uses-permission android:name="android.; /> <!-- 蓝牙--> <uses-permission android:name="android.;/> <uses-permission android:name="android.;/> <uses-permission android:name="android.; /> <uses-permission android:name="android.;/> <!-- 仅在支持BLE(即蓝牙4.0)的设备上运行--> <uses-permission android:name="android.; android:required="true"/> <!-- 如果Android 6.0蓝牙搜索不到设备,需要补充下面两个权限--> <uses-permission android:name="android.;/> <uses-permission android:name="android.;/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style;> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.in; /> <category android:name="android.in; /> </intent-filter> </activity> </application> </manifest>

MainAc

注意那个UUID,不可以随便修改,不然无法连接上打印机,我这里省略了显示蓝牙设备列表的程序,找到打印就直接使用了。

package com.exam; import androidx.anno; import androidx.a; import android.anno; import android.blue; import android.blue; import android.blue; import android.blue; import android.con; import android.con; import android.con; import android.conFilter; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.u; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.IOException; import java.io.OutputStream; import java.u; public class MainActivity extends AppCompatActivity { private Button btn_bt_open; private Button btn_bt_start; private TextView txtinfo; private BluetoothAdapter mBluetooth; private int mOpenCode = 0x01; private BluetoothDevice deviceprint; //通过广播,接收周边蓝牙设备信息 private BroadcastReceiver discoveryReceiver = new BroadcastReceiver() { @SuppressLint("MissingPermission") @Override public void onReceive(Context context, Intent intent) { String action = in(); Log.i("myapp", "action:" + action); //获得已经搜索到的蓝牙设备 if )) {//发现新的蓝牙设备 BluetoothDevice device = in); Log.i("myapp", "Find:" + device.getAddress()); Log.i("myapp", "Find:" + device.getName()); if ().equals("99:32:88:A6:19:FE")) { //找到打印机直接使用 deviceprint = mBlue("99:32:88:A6:19:FE"); mBlue(); } } } }; @Override protected void onStart() { (); //需要过滤多个动作,则调用IntentFilter对象的addAction添加新动作 IntentFilter discoveryFilter = new IntentFilter(); Log.i("myapp", "Reg discoverReceiver"); di); //注册蓝牙设备搜索的广播接收器 Intent intent = registerReceiver(discoveryReceiver, discoveryFilter); } @Override protected void onStop() { (); //注销蓝牙设备搜索的广播接收器 unregisterReceiver(discoveryReceiver); } @SuppressLint("MissingPermission") private void beginDiscovery() { //如果当前不是正在搜索,则开始新的搜索任务 if () != true) { Log.i("myapp", "Start find BlueTooth"); mBlue();//开始扫描周围的蓝牙设备 } } @SuppressLint("MissingPermission") private void startBluetooth() { //弹出是否允许扫描蓝牙设备的选择对话框 Intent intent = new Inten); startActivityForResult(intent, mOpenCode); } BluetoothSocket socket; @SuppressLint("MissingPermission") private void printtxt() { if (deviceprint == null) return; if ()) mBlue();//关闭扫描 UUID uuid= UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//不可以修改,这个值对应打印机 try { socket = device(uuid); } catch (IOException e) { e.printStackTrace(); } new Thread() { //蓝牙连接必须新开一个线程 @Override public void run() { if (socket == null) return; try { if () == false(); String otxt = "OK\r\n打印汉字测试\r\nHeHe\r\n"; OutputStream outs = (); //InputStream ins=(); ou(0x1B); ou(0x40); ou(); //ESC初始化打印机 ou(0x1B); ou(0x61); ou(0x0); //0居左 1居中 2 居右 ou(); //ESC初始化打印机 ou("gb2312")); //这里必须与打印机内部编码一样,不然打印汉字是乱码 ou(); ("myapp","input:"+S()) ou(); Log.i("myapp", "print finish"); (); } catch (IOException e) { e.printStackTrace(); } } }.start(); } /** * 初始化蓝牙适配器 */ private void initBluetooth() { //Android从4.3开始增加支持BLE技术(即蓝牙4.0以上版本) if >= Build.VERSION_CODES.JELLY_BEAN_MR2) { //从系统服务中获取蓝牙管理器 BluetoothManager bm = (BluetoothManager) getSystemService); mBluetooth = bm.getAdapter(); Log.i("myapp", "从系统服务中获取蓝牙管理器"); } else { //获取系统默认的蓝牙适配器 mBluetooth = Blue(); Log.i("myapp", "获取系统默认的蓝牙适配器"); } if (mBluetooth == null) { ("本机没有蓝牙"); Log.i("findresult", "没有找到蓝牙"); } else { ("本机有蓝牙设备"); Log.i("findresult", "本机有蓝牙"); startBluetooth(); } } @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView); btn_bt_open = (Button) findViewById); btn_bt_start = (Button) findViewById); txtinfo = (TextView) findViewById); b(new View.OnClickListener() { @Override public void onClick(View view) { initBluetooth(); } }); b(new View.OnClickListener() { @Override public void onClick(View view) { printtxt(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { (requestCode, resultCode, data); if (requestCode == mOpenCode) {//来自允许蓝牙扫描的对话框 if (resultCode == 120) { ("允许本地蓝牙被附近的其他蓝牙设备发现"); //延迟50毫秒后启动蓝牙设备的刷新任务 new Handler().postDelayed(new Runnable() { @Override public void run() { Log.i("myApp", "开始查找蓝牙..."); beginDiscovery(); } }, 50); } else { ("不允许蓝牙被附近的其他蓝牙设备发现"); } } } }

ac

<?xml version="1.0" encoding="utf-8"?> <androidx.con xmlns:android="; xmlns:app="; xmlns:tools="; android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" tools:layout_editor_absoluteX="1dp" tools:layout_editor_absoluteY="1dp"> <Button android:id="@+id/btn_bt_open" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取蓝牙" /> <Button android:id="@+id/btn_bt_print" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="蓝牙打印" /> <TextView android:id="@+id/textinfo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> </androidx.con>

1.《关于淘宝热敏打印机怎么打印机,你需要知道这些蓝牙热敏打印机安卓源代码分析》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。

2.《关于淘宝热敏打印机怎么打印机,你需要知道这些蓝牙热敏打印机安卓源代码分析》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。

3.文章转载时请保留本站内容来源地址,https://www.lu-xu.com/why/3255988.html

上一篇

关于不插打印机怎么打印机,你需要知道这些一机多用,不用插电的打印机,爱立熊A4打印机到底有多香?

淘宝热敏打印机怎么打印机?终于找到答案了智能A4热敏打印机,0墨0碳粉学生家用汉印U100深度测评

  • 淘宝热敏打印机怎么打印机?终于找到答案了智能A4热敏打印机,0墨0碳粉学生家用汉印U100深度测评
  • 淘宝热敏打印机怎么打印机?终于找到答案了智能A4热敏打印机,0墨0碳粉学生家用汉印U100深度测评
  • 淘宝热敏打印机怎么打印机?终于找到答案了智能A4热敏打印机,0墨0碳粉学生家用汉印U100深度测评
淘宝热敏打印机怎么打印机看这里!有关热敏打印机无法打印的解决方法

淘宝热敏打印机怎么打印机看这里!有关热敏打印机无法打印的解决方法

淘宝热敏打印机怎么打印机相关介绍,在条形码生成软件中设计标签样板,以便连接打印机,以便在打印时不打印打印机。 通过检查,发现条码生成软件中的设置没有问题,我们可以检查下打印机,看下是不是因为打印机使用久了,出现了无法打印的情况。出现其...

淘宝热敏打印机怎么打印机?终于找到答案了有关热敏打印机打印不出字的原因及解决方法介绍

淘宝热敏打印机怎么打印机?终于找到答案了有关热敏打印机打印不出字的原因及解决方法介绍

淘宝热敏打印机怎么打印机相关介绍,标签打印机有热量、热量传输分数。 有的客户在条码生成软件中设计好标签预览正常,连接热敏打印机打印的时候打印不出来字,咨询这是怎么回事?今天针对这个问题,给大家分析下原因及介绍下解决方法: 热敏打印原理...

关于淘宝热敏打印机怎么打印机,你需要知道这些“咕咕机”到底是什么打印机?热敏打印机入门级常识要知道

  • 关于淘宝热敏打印机怎么打印机,你需要知道这些“咕咕机”到底是什么打印机?热敏打印机入门级常识要知道
  • 关于淘宝热敏打印机怎么打印机,你需要知道这些“咕咕机”到底是什么打印机?热敏打印机入门级常识要知道
  • 关于淘宝热敏打印机怎么打印机,你需要知道这些“咕咕机”到底是什么打印机?热敏打印机入门级常识要知道