android手机通过蓝牙和蓝牙打印机建立连接.doc_第1页
android手机通过蓝牙和蓝牙打印机建立连接.doc_第2页
android手机通过蓝牙和蓝牙打印机建立连接.doc_第3页
android手机通过蓝牙和蓝牙打印机建立连接.doc_第4页
android手机通过蓝牙和蓝牙打印机建立连接.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

android手机通过蓝牙和蓝牙打印机建立连接android手机 通过蓝牙怎么和蓝牙打印机建立连接啊 !package com.ea.test;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.UUID;import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.bluetooth.BluetoothServerSocket;import android.bluetooth.BluetoothSocket;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Bluetooth_one extends Activity / Debugging private static final String TAG = BluetoothChatService; private static final boolean D = true;protected static final int REQUEST_ENABLE_BT = 0;private Button open = null;private Button find = null;private Button conn = null;private Button trans = null;private BluetoothAdapter mBluetoothAdapter=null;private int mState;/ Constants that indicate the current connection statepublic static final int STATE_NONE = 0; / were doing nothingpublic static final int STATE_LISTEN = 1; / now listening for incoming connectionspublic static final int STATE_CONNECTING = 2; / now initiating an outgoing connectionpublic static final int STATE_CONNECTED = 3; / now connected to a remote device/ Unique UUID for this application private static final UUID MY_UUID = UUID.fromString(fa87c0d0-afac-11de-8a39-0800200c9a66);/* Called when the activity is first created. */Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);open = (Button) findViewById(R.id.open);find = (Button) findViewById(R.id.find);conn = (Button) findViewById(R.id.conn);trans = (Button) findViewById(R.id.trans);addLisenner();public void addLisenner() open.setOnClickListener(new OnClickListener() Overridepublic void onClick(View v) / TODO Auto-generated method stubmBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (mBluetoothAdapter = null) / Device does not support BluetoothshowDialog(R.string.no_support); else if (!mBluetoothAdapter.isEnabled() Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);else);find.setOnClickListener(new OnClickListener() Overridepublic void onClick(View arg0) / TODO Auto-generated method stubnew AcceptThread().start(););conn.setOnClickListener(new OnClickListener() Overridepublic void onClick(View v) / TODO Auto-generated method stubBluetoothDevice device=mBluetoothAdapter.getRemoteDevice(00:80:37:21:18:81);new ConnectThread(device).start(););/* * This thread runs while listening for incoming connections. It behaves * like a server-side client. It runs until a connection is accepted * (or until cancelled). */ private class AcceptThread extends Thread / The local server socket private final BluetoothServerSocket mmServerSocket; public AcceptThread() BluetoothServerSocket tmp = null; / Create a new listening server socket try tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(ddd, MY_UUID); catch (IOException e) Log.e(TAG, listen() failed, e); mmServerSocket = tmp; public void run() if (D) Log.d(TAG, BEGIN mAcceptThread + this); setName(AcceptThread); BluetoothSocket socket = null; / Listen to the server socket if were not connected while (mState != STATE_CONNECTED) try / This is a blocking call and will only return on a / successful connection or an exception socket = mmServerSocket.accept(); catch (IOException e) Log.e(TAG, accept() failed, e); break; / If a connection was accepted if (socket != null) synchronized (Bluetooth_one.this) switch (mState) case STATE_LISTEN: case STATE_CONNECTING: / Situation normal. Start the connected thread. / connected(socket, socket.getRemoteDevice(); break; case STATE_NONE: case STATE_CONNECTED: / Either not ready or already connected. Terminate new socket. try socket.close(); catch (IOException e) Log.e(TAG, Could not close unwanted socket, e); break; if (D) Log.i(TAG, END mAcceptThread); public void cancel() if (D) Log.d(TAG, cancel + this); try mmServerSocket.close(); catch (IOException e) Log.e(TAG, close() of server failed, e); Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) / TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (REQUEST_ENABLE_BT = requestCode) if (resultCode = RESULT_OK) / user open bluetooth/ mBluetoothAdapter.startDiscovery(); else / user not open bluetoothprivate class ConnectedThread extends Thread private final BluetoothSocket mmSocket;private final InputStream mmInStream;private final OutputStream mmOutStream;public ConnectedThread(BluetoothSocket socket) mmSocket = socket;InputStream tmpIn = null;OutputStream tmpOut = null;/ Get the input and output streams, using temp objects because/ member streams are finaltry tmpIn = socket.getInputStream();tmpOut = socket.getOutputStream(); catch (IOException e) mmInStream = tmpIn;mmOutStream = tmpOut;public void run() byte buffer = new byte1024; / buffer store for the streamint bytes; / bytes returned from read()/ Keep listening to the InputStream until an exception occurswhile (true) try / Read from the InputStreambytes = mmInStream.read(buffer);/ Send the obtained bytes to the UI Activity/ mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); catch (IOException e) break;/* Call this from the main Activity to send data to the remote device */public void write(byte bytes) try mmOutStream.write(bytes); catch (IOException e) /* Call this from the main Activity to shutdown the connection */public void cancel() try mmSocket.close(); catch (IOException e) private class ConnectThread extends Thread private final BluetoothSocket mmSocket;private final BluetoothDevice mmDevice;public ConnectThread(BluetoothDevice device) / Use a temporary object that is later assigned to mmSocket,/ because mmSocket is finalBluetoothSocket tmp = null;mmDevice = device;/ Get a BluetoothSocket to connect with the given BluetoothDevicetry / MY_UUID is the apps UUID string, also used by the server codetmp = device.createRfcommSocketToServiceRecord(MY_UUID); catch (

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论