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

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

概述任何人都可以告诉我如何通过蓝牙连接手机打印机在 Android中打印文本文件? 也就是说,如果我点击Android应用程序的打印按钮,打印机必须打印相应的文件.根据我在Google上搜索的知识,但是我找不到任何好的样品.有任何人有至少一个样本android程序来做到这一点,最好是清除我的混乱. 请提出意见 感谢您宝贵的时间! 蓝牙打印机Android示例 在您的编辑器中创建一个新的Android 任何人都可以告诉我如何通过蓝牙连接手机和打印机在 Android中打印文本文件?

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

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

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

步骤1:

创建主要活动,如下所示

com.example.BluetoothPrinterapp/BluetoothPrinterapp.javapackage 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;@OverrIDepublic 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() {@OverrIDepublic 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();}}@OverrIDeprotected voID onDestroy() {super.onDestroy();try {if(btsocket!= null){btoutputstream.close();btsocket.close();btsocket = null;}} catch (IOException e) {e.printstacktrace();}}@OverrIDeprotected 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.javapackage 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;@OverrIDepublic 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;}@OverrIDeprotected 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() {@OverrIDepublic 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();}}}};@OverrIDeprotected 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() {@OverrIDepublic 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() {@OverrIDepublic voID run() {finish();}});}}});connectThread.start();}private Runnable socketErrorRunnable = new Runnable() {@OverrIDepublic voID run() {Toast.makeText(getApplicationContext(),“Cannot establish connection”,Toast.LENGTH_SHORT).show();mBluetoothAdapter.startdiscovery();}};@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {super.onCreateOptionsMenu(menu);menu.add(0,Menu.FirsT,Menu.NONE,“Refresh Scanning”);return true;}@OverrIDepublic 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” ><TextVIEwandroID:ID=”@+ID/msgtextlbl”androID:layout_wIDth=”match_parent”androID:layout_height=”wrap_content”androID:text=”Enter Your Message : ” ></TextVIEw><EditTextandroID:ID=”@+ID/message”androID:layout_wIDth=”fill_parent”androID:layout_height=”100dp”androID:layout_below=”@+ID/msgtextlbl”androID:text=”” ></EditText><buttonandroID:ID=”@+ID/printbutton”androID:layout_wIDth=”fill_parent”androID:layout_height=”wrap_content”androID:layout_below=”@+ID/message”androID:layout_@R_301_5553@top=”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…

总结

以上是内存溢出为你收集整理的如何通过蓝牙在Android中连接手机和打印机?全部内容,希望文章能够帮你解决如何通过蓝牙在Android中连接手机和打印机?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1131498.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存