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


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

  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;}
}

Android手机蓝牙连接热敏打印机 打印票据相关推荐

  1. Android手机蓝牙连接笔记本电脑蓝牙

    1. 实验设备 1.1 笔记电脑 品牌:DELL 蓝牙模块:BCM43142(内置型带蓝牙的无线网卡) 蓝牙驱动:bluetooth_broadcom_6.5.1.6400_w7(通过360驱动大师下 ...

  2. Android手机无法连接HC-05蓝牙模块

    目录 前言 问题描述 尝试方法 问题原因 解决方法 总结 前言 这学期选的毕业设计中需要用到蓝牙模块与Android手机通信,于是我就在淘宝购买了一款HC-05的蓝牙模块,到货之后首先使用蓝牙模块连接 ...

  3. Android 蓝牙连接打印机打印网络图片

    实现蓝牙连接打印机打印网络图片 经过自己一下午加一个小时的时间整理出来,希望能帮助到各位码兄弟! 主要分为以下几步: 将网络图片URL转为bitmap :其中需要进行网络请求,不可在主线程中进行,需另 ...

  4. uniapp蓝牙连接热敏打印机

    uniapp蓝牙连接热敏打印机 需求:通过小程序连接蓝牙,打印指定内容 前提:根据打印机的品牌型号不同,所遵循的打印规范不同(具体询问卖家) 我使用的是佳博的tspl规范 准备:需要几个外部js(具体 ...

  5. Android 设备蓝牙连接扫描枪获取扫描内容

    Android 设备蓝牙连接扫描枪获取扫描内容 标签(空格分隔): 未分类 条形扫描枪主要可以扫描条形码和二维码等,扫描速度比手机扫描设备快得多,本文简单介绍android 通过蓝牙监听蓝牙连接,当扫 ...

  6. Android 手机蓝牙 (普通蓝牙篇)

    Android 手机蓝牙一 (普通蓝牙) hello,大家好有很久没有更新博客了.想写的东西很多,提起笔又觉得无从下笔.可能这就是菜鸟吧!好了废话不多说,进入正题. 本篇讲的是Android手机蓝牙分 ...

  7. Android手机一直连接USB进行自动化,一直充电,可能导致电池鼓包,如何定时禁止充电和开启充电?

    为了避免 Android 手机在连接 USB 进行自动化测试时充电过度导致电池鼓包的问题,可以通过以下步骤实现禁止充电若干小时后自动充电的功能. 步骤: 连接 Android 手机到电脑的 USB 端 ...

  8. win7蓝牙怎么连接_蓝牙车载MP3播放器和手机蓝牙连接以后怎么不响呢?

    车载MP3播放器和手机蓝牙连接以后怎么不响呢? 车载MP3播放器和手机蓝牙连接以后怎么不响呢? 如果是安卓手机可能是手机的媒体音量没有打开.在播放音乐的时候按一下手机的音量键,调整手机的音量,将媒体音 ...

  9. 用代码实现断开Android手机USB连接【转】

    本文转载自:https://blog.csdn.net/phoebe_2012/article/details/47025309 用代码实现断开Android手机USB连接 用代码 实现了一个小功能: ...

最新文章

  1. Sql Server 日志清理 (数据库压缩方法)
  2. mysql 拼音查询_mysql实现用拼音搜索中文的数据库实现
  3. python nltk lemmatizer_Python聊天机器人–使用NLTK和Keras构建第一个聊天机器人
  4. UnixLinux技术文章目录(2015-12-22更新)
  5. 大一计算机专业学生如何在寒假充电?
  6. IaaS、PaaS、SaaS、BaaS、FaaS、APaaS、IPaaS、IDaaS、DaaS
  7. mysql中同一天入职怎么表示_ORACLE入职考试题及答案
  8. 智能语音识别究竟是如何实现的?
  9. python 直播源_直播源获取软件下载|直播源获取工具(斗鱼B站西瓜)下载-蛙扑下载站...
  10. win7 虚拟机安装
  11. 三阶交调截取点的测量
  12. 通过cRIO 9047 USB端口自定义开发周立功CAN盒
  13. 微信小程序学习笔记——环境准备 【注册账号】【获取APPID】
  14. 诺顿杀毒软件设置日常设置及防火墙配置(图文)
  15. win10怎么设置护眼背景
  16. TML5期末大作业:动漫网站设计——神偷奶爸(10页) HT简单个人网页设计作业 静态动漫主题网页作业 DW个人网站模板下载 大学生简单个人网页作品代码
  17. Python-Regression
  18. Matlab中动画绘制中hold on的小问题
  19. 微信自动回复小程序(有手就行)
  20. Error launching datagrip的if you already have a 64-bit JDK installed,define a JAVA_HOME variable in..

热门文章

  1. vector赋值操作
  2. C语言(最孤单的数) 质数的判断以及输出0~100内的质数
  3. sumo构建栅格城市并仿真
  4. 【项目】bxg基于SaaS的餐掌柜项目实战(2023)
  5. Silverlight中生动的火柴人动画
  6. php yii composer,使用Composer安装Yii框架的方法
  7. 尽在双11 阿里巴巴技术演进与超越
  8. 推荐一款免费的Markdown编辑器,GitHub斩获22.8k Star
  9. unity 开发射击打靶vr_暑假VR集训+unity自制射击游戏+李明
  10. python课程设计题目-python课设题目