




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 第1节 压力概述教学设计-2025-2026学年中职心理健康全一册高教版
- 第三节 其他生物的呼吸说课稿-2025-2026学年初中生物学北京版七年级下册-北京版
- §3 柱坐标系和球坐标系说课稿-2025-2026学年高中数学北师大版2011选修4-4坐标系与参数方程-北师大版2006
- 第1课 生活在信息时代-信息说课稿-2025-2026学年初中信息技术辽师大版2015七年级上册-辽师大版2015
- 第14课 当代中国的外交教学设计-2025-2026学年高中历史选择性必修1 国家制度与社会治理统编版(部编版)
- 活动二 书包物品理整齐说课稿-2023-2024学年小学劳动二年级上册东北师大版《学生劳动实践与评价指导手册》
- 湘教版世界地理七年级上册第一章第1课我们身边的地理说课稿
- 2025工业仓库租赁合同模板
- 2025年低空经济「城市监控」空中平台解决方案报告
- 2025年低空经济视角下无人机遥感技术在生态环境监测中的可持续发展报告
- 腾讯云大数据云平台TBDS 产品白皮书
- 一道美丽的风景作文500字
- 个人简历模板表格式
- 现网终端问题分析报告
- 第十五章巷道与井筒施工测量
- GB/T 1864-2012颜料和体质颜料通用试验方法颜料颜色的比较
- GB/T 13384-2008机电产品包装通用技术条件
- FZ/T 07019-2021针织印染面料单位产品能源消耗限额
- 《计算机辅助翻译》课程教学大纲
- 电厂化学运行规程
- 新版香港朗文1A-6B全部单词汇总
评论
0/150
提交评论