Android手机蓝牙连接热敏打印机 打印票据

news/2024/11/29 4:52:44/

手机蓝牙连接热敏打印机 打印票据


话不多说上代码:
项目地址:可直接作为项目依赖

  1. 引用
    allprojects {repositories {...maven { url 'https://jitpack.io' }}}dependencies {compile'com.github.guochenhome:BluetoothPrint:1.1.2'}
  1. 如果第一步报
Error:Execution failed for task ':app:processXXXDebugManifest'. >
Manifest merger failed with multiple errors, see logs

这个错的话
这说明在合并所有的Manfest文件时冲突了,几率最大的两个原因是

1.build.gradle中设置的compileSdkVersion buildToolsVersion minSdkVersion targetSdkVersion不统一,需要按宿主项目的配置进行统一。
2.几个项目的AndroidManifest文件中设置了多个android:allowBackup android:icon android:label android:theme 属性,这里需要在宿主项目的Manfest文件中添加两句话
manifest 节点下加入
xmlns:tools=”http://schemas.android.com/tools”
application节点下加入
tools:replace=”android:allowBackup,icon,theme,label”
不能写成tools:replace=”android:allowBackup,android:icon,android:theme” 虽然不报错,但是不起作用。

那就直接把项目下载直接导入你的项目 按照上述更改依赖的项目;


使用步骤


  • 第一步 初始化你的APPINFO
public class App extends Application {@Overridepublic void onCreate() {super.onCreate();AppInfo.init(getApplicationContext());}
}
  • 第二步
/**
1.实现PrinterInterface 接口
2.实例化    EventBus.getDefault().register(MainActivity.this);bluetoothController = new BluetoothController(this);bluetoothController.setPrinterInterface(this);
3. 初始化:bluetoothController.init();
4. 
*/
public class MainActivity extends BluetoothActivity implements View.OnClickListener, BluetoothController.PrinterInterface {TextView tv_bluename;TextView tv_blueadress;boolean mBtEnable = true;int PERMISSION_REQUEST_COARSE_LOCATION = 2;/*** bluetooth adapter*/BluetoothController bluetoothController;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_bluename = (TextView) findViewById(R.id.tv_bluename);tv_blueadress = (TextView) findViewById(R.id.tv_blueadress);findViewById(R.id.button4).setOnClickListener(this);findViewById(R.id.button5).setOnClickListener(this);findViewById(R.id.button6).setOnClickListener(this);findViewById(R.id.button).setOnClickListener(this);//6.0以上的手机要地理位置权限if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);}EventBus.getDefault().register(MainActivity.this);bluetoothController = new BluetoothController(this);bluetoothController.setPrinterInterface(this);}@Overrideprotected void onStart() {super.onStart();
//        BluetoothController.init(this);bluetoothController.init();}@Overridepublic void btStatusChanged(Intent intent) {super.btStatusChanged(intent);bluetoothController.init();}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.button4:startActivity(new Intent(MainActivity.this, SearchBluetoothActivity.class));break;case R.id.button5:if (TextUtils.isEmpty(AppInfo.btAddress)) {ToastUtil.showToast(MainActivity.this, "请连接蓝牙...");startActivity(new Intent(MainActivity.this, SearchBluetoothActivity.class));} else {if (bluetoothController.getmAdapter().getState() == BluetoothAdapter.STATE_OFF) {//蓝牙被关闭时强制打开bluetoothController.getmAdapter().enable();ToastUtil.showToast(MainActivity.this, "蓝牙被关闭请打开...");} else {ToastUtil.showToast(MainActivity.this, "打印测试...");Intent intent = new Intent(getApplicationContext(), BtService.class);intent.setAction(PrintUtil.ACTION_PRINT_TEST);startService(intent);}}break;case R.id.button6:if (TextUtils.isEmpty(AppInfo.btAddress)) {ToastUtil.showToast(MainActivity.this, "请连接蓝牙...");startActivity(new Intent(MainActivity.this, SearchBluetoothActivity.class));} else {ToastUtil.showToast(MainActivity.this, "打印测试...");Intent intent2 = new Intent(getApplicationContext(), BtService.class);intent2.setAction(PrintUtil.ACTION_PRINT_TEST_TWO);startService(intent2);}case R.id.button:if (TextUtils.isEmpty(AppInfo.btAddress)) {ToastUtil.showToast(MainActivity.this, "请连接蓝牙...");startActivity(new Intent(MainActivity.this, SearchBluetoothActivity.class));} else {ToastUtil.showToast(MainActivity.this, "打印图片...");Intent intent2 = new Intent(getApplicationContext(), BtService.class);intent2.setAction(PrintUtil.ACTION_PRINT_BITMAP);startService(intent2);}
//                startActivity(new Intent(MainActivity.this,TextActivity.class));break;}}/*** handle printer message** @param event print msg event*/public void onEventMainThread(PrintMsgEvent event) {if (event.type == PrinterMsgType.MESSAGE_TOAST) {ToastUtil.showToast(MainActivity.this, event.msg);}}@Overrideprotected void onDestroy() {super.onDestroy();EventBus.getDefault().register(MainActivity.this);}@Overridepublic void NoBT() {this.tv_bluename.setText("该设备没有蓝牙模块");this.mBtEnable = false;}@Overridepublic void BT_NoOpen() {this.tv_bluename.setText("蓝牙未打开");}@Overridepublic void BT_NoBind() {this.tv_bluename.setText("尚未绑定蓝牙设备");}@Overridepublic void BT_Bind(String name, String address) {this.tv_bluename.setText("已绑定蓝牙:" + name);this.tv_blueadress.setText(address);}
}
  • 第三步 下面是我搜索蓝牙的可视化页面
package com.xmwdkk.boothprint;import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;import com.ysh.rn.printet.bt.BluetoothActivity;
import com.ysh.rn.printet.bt.BtUtil;
import com.ysh.rn.printet.print.PrintQueue;
import com.ysh.rn.printet.print.PrintUtil;
import com.ysh.rn.printet.util.ToastUtil;import java.lang.reflect.Method;/*** 蓝牙搜索界面* Created by liuguirong on 2017/8/3.*/public class SearchBluetoothActivity extends BluetoothActivity implements AdapterView.OnItemClickListener, View.OnClickListener {private BluetoothAdapter bluetoothAdapter;private ListView lv_searchblt;private TextView tv_title;private TextView tv_summary;private SearchBleAdapter searchBleAdapter;@Overridepublic void onCreate(@Nullable Bundle savedInstanceState ) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_searchbooth);lv_searchblt = (ListView) findViewById(R.id.lv_searchblt);tv_title = (TextView) findViewById(R.id.tv_title);tv_summary = (TextView) findViewById(R.id.tv_summary);//初始化蓝牙适配器bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();searchBleAdapter = new SearchBleAdapter(SearchBluetoothActivity.this, null);lv_searchblt.setAdapter(searchBleAdapter);init();searchDeviceOrOpenBluetooth();lv_searchblt.setOnItemClickListener(this);tv_title.setOnClickListener(this);tv_summary.setOnClickListener(this);}private void init() {if (!BtUtil.isOpen(bluetoothAdapter)) {tv_title.setText("未连接蓝牙打印机");tv_summary.setText("系统蓝牙已关闭,点击开启");} else {if (!PrintUtil.isBondPrinter(this, bluetoothAdapter)) {//未绑定蓝牙打印机器tv_title.setText("未连接蓝牙打印机");tv_summary.setText("点击后搜索蓝牙打印机");} else {//已绑定蓝牙设备tv_title.setText(getPrinterName() + "已连接");String blueAddress = PrintUtil.getDefaultBluethoothDeviceAddress(this);if (TextUtils.isEmpty(blueAddress)) {blueAddress = "点击后搜索蓝牙打印机";}tv_summary.setText(blueAddress);}}
}@Overridepublic void btStatusChanged(Intent intent) {if ( bluetoothAdapter.getState()==BluetoothAdapter.STATE_OFF ){//蓝牙被关闭时强制打开bluetoothAdapter.enable();}if ( bluetoothAdapter.getState()==BluetoothAdapter.STATE_ON ){//蓝牙打开时搜索蓝牙searchDeviceOrOpenBluetooth();}}private String getPrinterName(){String dName = PrintUtil.getDefaultBluetoothDeviceName(this);if (TextUtils.isEmpty(dName)) {dName = "未知设备";}return dName;}private String getPrinterName(String dName) {if (TextUtils.isEmpty(dName)) {dName = "未知设备";}return dName;}/*** 开始搜索* search device*/private void searchDeviceOrOpenBluetooth() {if (BtUtil.isOpen(bluetoothAdapter)) {BtUtil.searchDevices(bluetoothAdapter);}}/*** 关闭搜索* cancel search*/@Overrideprotected void onStop() {super.onStop();BtUtil.cancelDiscovery(bluetoothAdapter);}@Overridepublic void btStartDiscovery(Intent intent) {tv_title.setText("正在搜索蓝牙设备…");tv_summary.setText("");}@Overridepublic void btFinishDiscovery(Intent intent) {tv_title.setText("搜索完成");tv_summary.setText("点击重新搜索");}@Overridepublic void btFoundDevice(Intent intent) {BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);Log.d("1","!");if (null != bluetoothAdapter && device != null) {searchBleAdapter.addDevices(device);String dName = device.getName() == null ? "未知设备" : device.getName();Log.d("未知设备",dName);Log.d("1","!");}}@Overridepublic void btBondStatusChange(Intent intent) {super.btBondStatusChange(intent);BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);switch (device.getBondState()) {case BluetoothDevice.BOND_BONDING://正在配对Log.d("BlueToothTestActivity", "正在配对......");break;case BluetoothDevice.BOND_BONDED://配对结束Log.d("BlueToothTestActivity", "完成配对");connectBlt(device);break;case BluetoothDevice.BOND_NONE://取消配对/未配对Log.d("BlueToothTestActivity", "取消配对");default:break;}}@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {if (null == searchBleAdapter) {return;}final BluetoothDevice bluetoothDevice = searchBleAdapter.getItem(position);if (null == bluetoothDevice) {return;}new AlertDialog.Builder(this).setTitle("绑定" + getPrinterName(bluetoothDevice.getName()) + "?").setMessage("点击确认绑定蓝牙设备").setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {try {BtUtil.cancelDiscovery(bluetoothAdapter);if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {connectBlt(bluetoothDevice);} else {Method createBondMethod = BluetoothDevice.class.getMethod("createBond");createBondMethod.invoke(bluetoothDevice);}PrintQueue.getQueue(getApplicationContext()).disconnect();String name = bluetoothDevice.getName();} catch (Exception e) {e.printStackTrace();PrintUtil.setDefaultBluetoothDeviceAddress(getApplicationContext(), "");PrintUtil.setDefaultBluetoothDeviceName(getApplicationContext(), "");ToastUtil.showToast(SearchBluetoothActivity.this,"蓝牙绑定失败,请重试");}}}).create().show();}/**** 配对成功连接蓝牙* @param bluetoothDevice*/private void connectBlt(BluetoothDevice bluetoothDevice) {if (null != searchBleAdapter) {searchBleAdapter.setConnectedDeviceAddress(bluetoothDevice.getAddress());}init();searchBleAdapter.notifyDataSetChanged();PrintUtil.setDefaultBluetoothDeviceAddress(getApplicationContext(), bluetoothDevice.getAddress());PrintUtil.setDefaultBluetoothDeviceName(getApplicationContext(), bluetoothDevice.getName());}@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.tv_title:break;case R.id.tv_summary:searchDeviceOrOpenBluetooth();break;}}
}

Adapter

package com.xmwdkk.boothprint;import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;import com.ysh.rn.printet.base.AppInfo;
import com.ysh.rn.printet.print.PrintUtil;import java.util.ArrayList;/*** Created by yefeng on 6/2/15.* github:yefengfreedom*/
public class SearchBleAdapter extends BaseAdapter {private ArrayList<BluetoothDevice> mDevices;private LayoutInflater mInflater;private String mConnectedDeviceAddress;public SearchBleAdapter(Context mContext, ArrayList<BluetoothDevice> mDevices) {this.mInflater = LayoutInflater.from(mContext);this.mDevices = null == mDevices ? new ArrayList<BluetoothDevice>() : mDevices;mConnectedDeviceAddress = PrintUtil.getDefaultBluethoothDeviceAddress(mContext);}public ArrayList<BluetoothDevice> getDevices() {return mDevices;}public void setDevices(ArrayList<BluetoothDevice> mDevices) {if (null == mDevices) {mDevices = new ArrayList<BluetoothDevice>();}this.mDevices = mDevices;this.notifyDataSetChanged();}@Overridepublic void notifyDataSetChanged() {if (null != this.mDevices) {this.mDevices = sortByBond(this.mDevices);}super.notifyDataSetChanged();}private ArrayList<BluetoothDevice> sortByBond(ArrayList<BluetoothDevice> mDevices) {if (null == mDevices) {return null;}if (mDevices.size() < 2) {return mDevices;}ArrayList<BluetoothDevice> bondDevices = new ArrayList<BluetoothDevice>();ArrayList<BluetoothDevice> unBondDevices = new ArrayList<BluetoothDevice>();int size = mDevices.size();for (int i = 0; i < size; i++) {BluetoothDevice bluetoothDevice = mDevices.get(i);if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {bondDevices.add(bluetoothDevice);} else {unBondDevices.add(bluetoothDevice);}}mDevices.clear();mDevices.addAll(bondDevices);mDevices.addAll(unBondDevices);bondDevices.clear();bondDevices = null;unBondDevices.clear();unBondDevices = null;return mDevices;}public void setConnectedDeviceAddress(String macAddress) {this.mConnectedDeviceAddress = macAddress;}public void addDevices(ArrayList<BluetoothDevice> mDevices) {if (null == mDevices) {return;}for (BluetoothDevice bluetoothDevice : mDevices) {addDevices(bluetoothDevice);}}public void addDevices(BluetoothDevice mDevice) {if (null == mDevice) {return;}if (!this.mDevices.contains(mDevice)) {this.mDevices.add(mDevice);this.notifyDataSetChanged();}}@Overridepublic int getCount() {return mDevices.size();}@Overridepublic BluetoothDevice getItem(int position) {return mDevices.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if (convertView != null) {holder = (ViewHolder) convertView.getTag();} else {convertView = mInflater.inflate(R.layout.adapter_bt_device, parent, false);holder = new ViewHolder();if (null != convertView) {convertView.setTag(holder);}}holder.name = (TextView) convertView.findViewById(R.id.txt_adapter_bt_name);holder.address = (TextView) convertView.findViewById(R.id.txt_adapter_bt_address);holder.bond = (TextView) convertView.findViewById(R.id.btn_adapter_bt_has_bond);BluetoothDevice bluetoothDevice = mDevices.get(position);String dName = bluetoothDevice.getName() == null ? "未知设备" : bluetoothDevice.getName();if (TextUtils.isEmpty(dName)) {dName = "未知设备";}holder.name.setText(dName);String dAddress = bluetoothDevice.getAddress() == null ? "未知地址" : bluetoothDevice.getAddress();if (TextUtils.isEmpty(dAddress)) {dAddress = "未知地址";}holder.address.setText(dAddress);int paddingVertical = 8;int paddingHorizontal = 16;if (AppInfo.density != 0) {paddingVertical = (int) (paddingVertical * AppInfo.density);paddingHorizontal = (int) (paddingHorizontal * AppInfo.density);}if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {if (dAddress.equals(mConnectedDeviceAddress)) {holder.bond.setText("已连接");holder.bond.setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical);} else {holder.bond.setText("已配对");holder.bond.setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical);}} else {holder.bond.setText("未配对");holder.bond.setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical);}return convertView;}static class ViewHolder {TextView name;TextView address;TextView bond;}
}

http://www.ppmy.cn/news/487118.html

相关文章

android phone电脑驱动下载,全机型Android Phone驱动及安装教程(XP,Vista,Win7).pdf

全机型Android Phone驱动及安装教程(XP,Vista,Win7) HTC Android( ) Android Phone 安卓 全机型 驱动及安 装教程(XP,Vista,Win7 ) 此驱动&#xff0c;添加集合了 HTC 安卓所有机型的驱动&#xff0c;支持在 AMD64,XP32 XP64 ,Vista32 Vista64,Win7 32 Win7 64 下 安装 辛苦制作…

夏普复印机最最最详细的驱动安装教程

虽然本教程是以夏普举例&#xff0c;但是所有打印机驱动安装方法都大同小异&#xff0c;网络打印机通过IP地址传输数据&#xff0c;USB打印机通过USB连接线传输数据&#xff0c;无非就是端口设置上和驱动文件上有区别&#xff0c;其它型号安装时也可以参考一下本教程安装的步骤…

【ZenUML】时序图之ZenUML详解

时序图 序列图是一种交互图&#xff0c;显示进程如何彼此操作以及顺序。 Mermaid可以使用ZenUML渲染序列图。请注意&#xff0c;ZenUML使用的语法与mermaid中的原始序列图不同。 目前&#xff0c;最新版本mermaid v10.2.3 暂时不单独支持zenuml语法,需要配合mermaid-zenuml一…

Flask 全局异常处理

文章目录 一、异常处理流程1.1 异常注册1.1.1 装饰器模式1.1.2 工厂模式 1.2 异常触发1.2.1 assert触发异常1.2.2 raise触发异常1.2.3 abort触发异常 1.3 异常处理1.3.1 正常请求1.3.2 异常请求 二、自定义异常&#xff08;客户端异常&#xff09;三、服务端异常参考资料 异常分…

scanpy单细胞分析流程

梳理一下scanpy单细胞分析流程&#xff08;处理的是scRNA-seq&#xff09;。 先上一张流程图&#xff1a; scanpy单细胞分析流程 import scanpy as scRead data 常用的文件格式有两种&#xff0c;分别是h5ad和10X mtx # read h5ad adata sc.read()# read 10X mtx adata …

python:使用Scikit-image库的slic函数分割遥感图像

作者:CSDN @ _养乐多_ 本文记录了使用Scikit-image库的skimage.segmentation模块中的slic函数,进行超像素分割的代码。 文章目录 一、slic函数详解二、代码一、slic函数详解 在Scikit-image库的skimage.segmentation模块中,slic函数用于进行超像素分割。该函数的参数含义如…

元宇宙虚拟人解决方案:创新变革营销模式,用科技助力营销

近两年&#xff0c;元宇宙虚拟人大火&#xff0c;虚拟人与品牌联合已经成为了一种新的营销趋势&#xff0c;如阿喜与兰蔻、柳夜熙与娇韵诗、A-Soul与Keep……这些品牌商业合作实现了虚拟人商业的变现。虚拟人可以与各虚实场景交互&#xff0c;拉近与用户的距离&#xff0c;增强…

HAYDON黑洞全球高端美妆专卖店设计分享!

作为国内首个提出不可定义的黑洞之旅概念的高端美妆体验店&#xff0c;通过邀请顶尖的专卖店设计公司&#xff0c;HAYDON黑洞把首家线下体验店落户在了武汉楚河汉街。 随着首店开启&#xff0c;众美妆、时尚、生活方式领域的达人纷纷到场打卡&#xff0c;这个沉浸式的黑洞之旅体…