如何通过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, int resultCode, Intent intent) {
super.onActivityResult(reqCode, resultCode, 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;
}
}

第三步:

编辑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 : "/>

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

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

第四步:

现在编辑您的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>

编译并运行该应用程序。输入信息,然后按打印按钮。

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

检查您的蓝牙打印机上的打印。

这是代码参考...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)

来自分类Dev

如何将自定义 ZPL 标签从移动设备打印到连接到设备集线器的打印机?

来自分类Dev

如何将android设备中的PDF,图像和HTML文档打印到wifi上的打印机上?

来自分类Dev

如何在Windows 8中通过CLI或AHK打开“设备和打印机”控制面板?

来自分类Dev

如何通过CLI或AHK在Windows 8中打开“设备和打印机”控制面板?

来自分类Dev

在“设备和打印机”窗口中获取蓝牙设备的名称

来自分类Dev

设置/设备/打印机中的打印机加倍

来自分类Dev

通过Android中的Bluetooth打印机多次打印

来自分类Dev

通过Android中的Bluetooth打印机多次打印

来自分类Dev

通过移动设备在4英寸打印机上打印网页

来自分类Dev

如何通过单击Android中的listview项连接蓝牙设备?

来自分类Dev

安卓蓝牙热敏打印机连接问题

来自分类Dev

Android App从蓝牙打印机进行打印

来自分类Dev

如何打印到热敏蓝牙打印机?

来自分类Dev

如何连接到WLAN打印机?

来自分类Dev

如何连接到WLAN打印机?

来自分类Dev

如何通过终端安装打印机?

来自分类Dev

如何通过终端安装打印机?

来自分类Dev

通过USB连接打印打印机的IP地址

来自分类Dev

如何在Android中连接蓝牙设备

来自分类Dev

如何在Android中连接蓝牙设备

来自分类Dev

Microsoft Word 显示 2 台打印机,但“设备和打印机”仅显示 1 台打印机,我想清除隐藏的打印机

来自分类Dev

如何通过 VPN 连接两个网络并通过网络访问网络打印机?

来自分类Dev

打印机的设备URI

来自分类Dev

如何通过 iOS SDK 通过蓝牙将字体发送到 Zebra 打印机 (Zebra imz320)

来自分类Dev

HP 打印机出现在“其他设备”而不是“打印机”中

来自分类Dev

如何配置网络以使计算机可以访问本地无线打印机和设备

来自分类Dev

批量打开“设备和打印机”窗口?

来自分类Dev

批量打开“设备和打印机”窗口?

Related 相关文章

  1. 1

    如何检查配对的蓝牙设备是打印机还是扫描仪(Android)

  2. 2

    如何将自定义 ZPL 标签从移动设备打印到连接到设备集线器的打印机?

  3. 3

    如何将android设备中的PDF,图像和HTML文档打印到wifi上的打印机上?

  4. 4

    如何在Windows 8中通过CLI或AHK打开“设备和打印机”控制面板?

  5. 5

    如何通过CLI或AHK在Windows 8中打开“设备和打印机”控制面板?

  6. 6

    在“设备和打印机”窗口中获取蓝牙设备的名称

  7. 7

    设置/设备/打印机中的打印机加倍

  8. 8

    通过Android中的Bluetooth打印机多次打印

  9. 9

    通过Android中的Bluetooth打印机多次打印

  10. 10

    通过移动设备在4英寸打印机上打印网页

  11. 11

    如何通过单击Android中的listview项连接蓝牙设备?

  12. 12

    安卓蓝牙热敏打印机连接问题

  13. 13

    Android App从蓝牙打印机进行打印

  14. 14

    如何打印到热敏蓝牙打印机?

  15. 15

    如何连接到WLAN打印机?

  16. 16

    如何连接到WLAN打印机?

  17. 17

    如何通过终端安装打印机?

  18. 18

    如何通过终端安装打印机?

  19. 19

    通过USB连接打印打印机的IP地址

  20. 20

    如何在Android中连接蓝牙设备

  21. 21

    如何在Android中连接蓝牙设备

  22. 22

    Microsoft Word 显示 2 台打印机,但“设备和打印机”仅显示 1 台打印机,我想清除隐藏的打印机

  23. 23

    如何通过 VPN 连接两个网络并通过网络访问网络打印机?

  24. 24

    打印机的设备URI

  25. 25

    如何通过 iOS SDK 通过蓝牙将字体发送到 Zebra 打印机 (Zebra imz320)

  26. 26

    HP 打印机出现在“其他设备”而不是“打印机”中

  27. 27

    如何配置网络以使计算机可以访问本地无线打印机和设备

  28. 28

    批量打开“设备和打印机”窗口?

  29. 29

    批量打开“设备和打印机”窗口?

热门标签

归档