如何通过蓝牙在Android中连接手机和打印机?

任何人都可以告诉我如何通过蓝牙连接手机和打印机在 Android中打印文本文件

也就是说,如果我点击Android应用程序的打印按钮,打印机必须打印相应的文件.根据我在Google上搜索的知识,但是我找不到任何好的样品.有任何人有至少一个样本android程序来做到这一点,最好是清除我的混乱.

请提出意见
感谢您宝贵的时间!

解决方法

蓝牙打印机Android示例
在您的编辑器中创建一个新的Android项目BluetoothPrinterapp.

步骤1:

创建主要活动,如下所示

com.example.BluetoothPrinterapp/BluetoothPrinterapp.java

package com.example.BluetoothPrinterapp;

import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import java.io.OutputStream;
import android.bluetooth.BluetoothSocket;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class BluetoothPrinterapp extends Activity
{
/** Called when the activity is first created. */
EditText message;
Button printbtn;

byte FONT_TYPE;
private static BluetoothSocket btsocket;
private static OutputStream btoutputstream;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
message = (EditText)findViewById(R.id.message);
printbtn = (Button)findViewById(R.id.printButton);

printbtn.setonClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
connect();
}
});
}

protected void connect() {
if(btsocket == null){
Intent BTIntent = new Intent(getApplicationContext(),BTDeviceList.class);
this.startActivityForResult(BTIntent,BTDeviceList.REQUEST_CONNECT_BT);
}
else{

OutputStream opstream = null;
try {
opstream = btsocket.getoutputStream();
} catch (IOException e) {
e.printstacktrace();
}
btoutputstream = opstream;
print_bt();

}

}
private void print_bt() {
try {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printstacktrace();
}

btoutputstream = btsocket.getoutputStream();

byte[] printformat = { 0x1B,0×21,FONT_TYPE };
btoutputstream.write(printformat);
String msg = message.getText().toString();
btoutputstream.write(msg.getBytes());
btoutputstream.write(0x0D);
btoutputstream.write(0x0D);
btoutputstream.write(0x0D);
btoutputstream.flush();
} catch (IOException e) {
e.printstacktrace();
}

}

@Override
protected void onDestroy() {
super.onDestroy();
try {
if(btsocket!= null){
btoutputstream.close();
btsocket.close();
btsocket = null;
}
} catch (IOException e) {
e.printstacktrace();
}
}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
try {
btsocket = BTDeviceList.getSocket();
if(btsocket != null){
print_bt();
}

} catch (Exception e) {
e.printstacktrace();
}
}
}

第2步:

com.example.BluetoothPrinterapp/BTDeviceList.java

package com.example.BluetoothPrinterapp;

import java.io.IOException;
import java.util.Set;
import java.util.UUID;

import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class BTDeviceList extends ListActivity {

static public final int REQUEST_CONNECT_BT = 0×2300;

static private final int REQUEST_ENABLE_BT = 0×1000;

static private BluetoothAdapter mBluetoothAdapter = null;

static private ArrayAdapter<String> mArrayAdapter = null;

static private ArrayAdapter<BluetoothDevice> btDevices = null;

private static final UUID SPP_UUID = UUID
.fromString(“8ce255c0-200a-11e0-ac64-0800200c9a66″);
// UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);

static private BluetoothSocket mbtSocket = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setTitle(“Bluetooth Devices”);

try {
if (initDevicesList() != 0) {
this.finish();
return;
}

} catch (Exception ex) {
this.finish();
return;
}

IntentFilter btIntentFilter = new IntentFilter(
BluetoothDevice.ACTION_FOUND);
registerReceiver(mBTReceiver,btIntentFilter);
}

public static BluetoothSocket getSocket() {
return mbtSocket;
}

private void flushData() {
try {
if (mbtSocket != null) {
mbtSocket.close();
mbtSocket = null;
}

if (mBluetoothAdapter != null) {
mBluetoothAdapter.canceldiscovery();
}

if (btDevices != null) {
btDevices.clear();
btDevices = null;
}

if (mArrayAdapter != null) {
mArrayAdapter.clear();
mArrayAdapter.notifyDataSetChanged();
mArrayAdapter.notifyDataSetInvalidated();
mArrayAdapter = null;
}

finalize();

} catch (Exception ex) {
} catch (Throwable e) {
}

}
private int initDevicesList() {

flushData();

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),“Bluetooth not supported!!”,Toast.LENGTH_LONG).show();
return -1;
}

if (mBluetoothAdapter.isdiscovering()) {
mBluetoothAdapter.canceldiscovery();
}

mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1);

setlistadapter(mArrayAdapter);

Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
try {
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
} catch (Exception ex) {
return -2;
}

Toast.makeText(getApplicationContext(),“Getting all available Bluetooth Devices”,Toast.LENGTH_SHORT)
.show();

return 0;

}

@Override
protected void onActivityResult(int reqCode,Intent intent) {
super.onActivityResult(reqCode,intent);

switch (reqCode) {
case REQUEST_ENABLE_BT:

if (resultCode == RESULT_OK) {
Set<BluetoothDevice> btDeviceList = mBluetoothAdapter
.getBondedDevices();
try {
if (btDeviceList.size() > 0) {

for (BluetoothDevice device : btDeviceList) {
if (btDeviceList.contains(device) == false) {

btDevices.add(device);

mArrayAdapter.add(device.getName() + “\n”
+ device.getAddress());
mArrayAdapter.notifyDataSetInvalidated();
}
}
}
} catch (Exception ex) {
}
}

break;
}

mBluetoothAdapter.startdiscovery();

}

private final broadcastReceiver mBTReceiver = new broadcastReceiver() {

@Override
public void onReceive(Context context,Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

try {
if (btDevices == null) {
btDevices = new ArrayAdapter<BluetoothDevice>(
getApplicationContext(),android.R.id.text1);
}

if (btDevices.getPosition(device) < 0) {
btDevices.add(device);
mArrayAdapter.add(device.getName() + “\n”
+ device.getAddress() + “\n” );
mArrayAdapter.notifyDataSetInvalidated();
}
} catch (Exception ex) {
// ex.fillInStackTrace();
}
}
}
};

@Override
protected void onListItemClick(ListView l,View v,final int position,long id) {
super.onListItemClick(l,v,position,id);

if (mBluetoothAdapter == null) {
return;
}

if (mBluetoothAdapter.isdiscovering()) {
mBluetoothAdapter.canceldiscovery();
}

Toast.makeText(
getApplicationContext(),“Connecting to ” + btDevices.getItem(position).getName() + “,”
+ btDevices.getItem(position).getAddress(),Toast.LENGTH_SHORT).show();

Thread connectThread = new Thread(new Runnable() {

@Override
public void run() {
try {
boolean gotuuid = btDevices.getItem(position)
.fetchUuidsWithSdp();
UUID uuid = btDevices.getItem(position).getUuids()[0]
.getUuid();
mbtSocket = btDevices.getItem(position)
.createRfcommSocketToServiceRecord(uuid);

mbtSocket.connect();
} catch (IOException ex) {
runOnUiThread(socketErrorRunnable);
try {
mbtSocket.close();
} catch (IOException e) {
// e.printstacktrace();
}
mbtSocket = null;
return;
} finally {
runOnUiThread(new Runnable() {

@Override
public void run() {
finish();

}
});
}
}
});

connectThread.start();
}

private Runnable socketErrorRunnable = new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(),“Cannot establish connection”,Toast.LENGTH_SHORT).show();
mBluetoothAdapter.startdiscovery();

}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0,Menu.FirsT,Menu.NONE,“Refresh Scanning”);

return true;
}

@Override
public boolean onoptionsItemSelected(MenuItem item) {
super.onoptionsItemSelected(item);

switch (item.getItemId()) {
case Menu.FirsT:
initDevicesList();
break;
}

return true;
}
}

步骤3:

编辑您的main.xml文件并粘贴以下代码.

RES /布局/ main.xml中

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:paddingLeft=”16dp”
android:paddingRight=”16dp” >

<TextView
android:id=”@+id/msgtextlbl”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Enter Your Message : ” >
</TextView>

<EditText
android:id=”@+id/message”
android:layout_width=”fill_parent”
android:layout_height=”100dp”
android:layout_below=”@+id/msgtextlbl”
android:text=”” >
</EditText>

<Button
android:id=”@+id/printButton”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/message”
android:layout_marginTop=”5dip”
android:layout_centerHorizontal=”true”
android:text=”Print” >
</Button>
</RelativeLayout>

步骤4:

现在编辑您的AndroidManifest.xml

添加蓝牙权限和管理员权限.

AndroidManifest.xml中

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.BluetoothPrinterapp”
android:versionCode=”1″
android:versionName=”1.0″>
<application android:label=”@string/app_name” android:icon=”@drawable/ic_launcher”>
<activity android:name=”BluetoothPrinterapp”
android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”BTDeviceList”></activity>
</application>
<uses-sdk android:minSdkVersion=”14″ />
<uses-permission android:name=”android.permission.BLUetoOTH”></uses-permission>
<uses-permission android:name=”android.permission.BLUetoOTH_ADMIN”></uses-permission>
</manifest>

编译并运行此应用程序.输入消息并按打印按钮.

您将看到蓝牙设备列表.选择蓝牙打印机.

检查蓝牙打印机上的打印.

here is the CODE Reference…

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android岛屿数量算...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Andro...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android数据压缩的方法是什么”文章能帮助大家解决疑惑...
这篇“Android怎么使用Intent传大数据”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅...
本文小编为大家详细介绍“Android事件冲突怎么解决悬浮窗拖拽问题”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android事件冲突怎么解决悬浮窗拖拽问题”文...
这篇文章主要介绍了Android拼接如何实现动态对象的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android拼接如何实现动态对象文...
今天小编给大家分享一下Android全面屏适配怎么实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下...
本篇内容介绍了“Android怎么开发Input系统触摸事件分发”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何...
今天小编给大家分享一下AndroidRoom怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下...
本文小编为大家详细介绍“AndroidRoom使用方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“AndroidRoom使用方法有哪些”文章能帮助大家...
这篇文章主要介绍“Android中的OpenGL怎么配置使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android中的Open...
这篇文章主要介绍了Android如何自定义自动识别涂鸦工具类的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android如何自定义自动...
今天小编给大家分享一下Android如何自定义有限制区域的图例角度自识别涂鸦工具类的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以...
今天小编给大家分享一下ReactNative错误采集原理在Android中如何实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章...
这篇文章主要讲解了“Android崩溃日志收集和保存代码分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“A...
这篇“Android面向单Activity开发实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大...
本篇内容介绍了“Android应用启动白屏处理的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何...