ImageVerifierCode 换一换
格式:DOCX , 页数:10 ,大小:17.41KB ,
资源ID:4516044      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4516044.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(SPP蓝牙总结.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

SPP蓝牙总结.docx

1、SPP蓝牙总结SPP蓝牙总结SPP蓝牙总结一、 SPP蓝牙使用1、 添加权限2、 蓝牙搜索a) IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);mContext.registerReceiver(mReceiver, filter);注册广播b) mBtAdapter.star

2、tDiscovery();开始扫描外围设备c) 退出,停止搜索,注销广播 mBtAdapter.cancelDiscovery(); private void unregisterReceiver() if (mReceiverRegisted & mContext != null) mContext.unregisterReceiver(mReceiver); mReceiverRegisted = false; 3、 蓝牙连接a) mConnectThread = new ConnectThread(device, secure);mConnectThread.start();新建连接线程

3、并启动 private class ConnectThread extends Thread private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; private String mSocketType; public ConnectThread(BluetoothDevice device, boolean secure) mmDevice = device; BluetoothSocket tmp = null; mSocketType = secure ? Secure : Insec

4、ure; try if (secure) tmp = device.createRfcommSocketToServiceRecord(SPP_UUID); else tmp = device .createInsecureRfcommSocketToServiceRecord(SPP_UUID); catch (IOException e) mmSocket = tmp; public void run() setName(ConnectThread + mSocketType); if (mmSocket != null) try mmSocket.connect(); catch (IO

5、Exception e) try mmSocket.close(); catch (IOException e2) connectionFailed(mmDevice); return; synchronized (BluetoothConnectClient.this) mConnectThread = null; mCount = 0; connected(mmSocket, mmDevice, mSocketType); b)创建BluetoothSocket, 并建立Socket连接 public void cancel() try if (null != mmSocket) mmSo

6、cket.close(); catch (IOException e) Logger.e(TAG + connect cancle:, e); public synchronized void connected(BluetoothSocket socket, BluetoothDevice device, final String socketType) ReadWriterBuffer buffer = new ReadWriterBuffer(); mWriteThread = new WriteThread(device, socket, socketType, buffer); mW

7、riteThread.start(); OutputStream tmpOut = null; try tmpOut = socket.getOutputStream(); catch (IOException e) Logger.e(TAG + connect, e); mRunnable = new Thread(new WriteRunnable(device, buffer, tmpOut); mRunnable.start(); mReadThread = new ReadThread(device, socket, socketType); mReadThread.start();

8、 c)创建读线程,创建写线程d) 写线程WriteRunnable,写入本地缓冲区域ReadWriterBuffer;写WriteThread,写入输出流。class WriteRunnable implements Runnable private final BluetoothDevice mmDevice; private ReadWriterBuffer mBuffer; private OutputStream mOut; private WriteRunnable(BluetoothDevice device, ReadWriterBuffer buffer, OutputStre

9、am out) this.mmDevice = device; this.mBuffer = buffer; this.mOut = out; Override public void run() while (mFlag) mBuffer.write(mOut); try Thread.sleep(Constants.INTERVEL_TIME); catch (InterruptedException e) Logger.e(TAG + mFlag: + mFlag, e); private class WriteThread extends HandlerThread private H

10、andler mmWriteHandler; private ReadWriterBuffer mBuffer; public WriteThread(BluetoothDevice device, BluetoothSocket socket, String socketType, ReadWriterBuffer buffer) super(WriteThread); this.mBuffer = buffer; public void start() super.start(); mmWriteHandler = new Handler(this.getLooper() Override

11、 public void handleMessage(Message msg) if (msg.what = MESSAGE_DATA_WRITHE) byte data = (byte) msg.obj; int len = data.length; mBuffer.read(data, len); ; public void write(byte data) if (mmWriteHandler != null) Message.obtain(mmWriteHandler, MESSAGE_DATA_WRITHE, data) .sendToTarget(); public void ca

12、ncel() this.quit(); e) public class ReadWriterBuffer private String TAG = ReadWriterBuffer; private int mPos; private byte mBuffer = new byte1024; private ReadWriteLock lock; public ReadWriterBuffer() lock = new ReentrantReadWriteLock(); public void read(byte res, int len) lock.readLock().lock(); Sy

13、stem.arraycopy(res, 0, mBuffer, mPos, len); mPos = mPos + len; lock.readLock().unlock(); public void write(OutputStream outStream) lock.writeLock().lock(); if (mPos != 0) try outStream.write(mBuffer, 0, mPos); outStream.flush(); catch (IOException e) Logger.d(TAG + write); mPos = 0; lock.writeLock()

14、.unlock(); 自定义缓冲Bufferf) 创建读线程 private class ReadThread extends Thread private final BluetoothDevice mmDevice; private final BluetoothSocket mmSocket; private final InputStream mmInStream; public ReadThread(BluetoothDevice device, BluetoothSocket socket, String socketType) mmDevice = device; mmSocke

15、t = socket; InputStream tmpIn = null; try tmpIn = socket.getInputStream(); catch (IOException e) mmInStream = tmpIn; public void run() byte buffer = new byte1024; int bytes; while (true) try bytes = mmInStream.read(buffer); dataReceived(buffer, bytes); catch (IOException e) connectionLost(mmDevice);

16、 break; public void cancel() try if (null != mmSocket) mmSocket.close(); catch (IOException e) g) 使用Handler把数据抛给主线程 private void sendDataReceivedMessage(byte data) if (mMainHandler != null) Message msg = mMainHandler.obtainMessage(MESSAGE_DATA_RECEIVED); msg.obj = data; mMainHandler.sendMessage(msg)

17、; private Handler mMainHandler = new Handler(Looper.getMainLooper() Override public void handleMessage(Message msg) if (mConnectClientListener = null) return; switch (msg.what) case MESSAGE_DATA_RECEIVED: byte data = (byte) msg.obj; mDataDecoder.parseKeyEvent(data, data.length); break; default: brea

18、k; ;h) 解析数据 void parseKeyEvent(byte data, int len) StringBuffer sb = new StringBuffer(); for (int i = 0; i len; i+) sb.append(datai + |); if (len = 1024) Logger.d(TAG + size + size); while (offset size) pos = 0; return; System.arraycopy(buffer, offset, tmp, 0, tmp.length); parseData(tmp, length); offset = offset + length; pos = size - offset; 补充知识点:蓝牙配对,连接蓝牙规范profile,蓝牙服务(音响,耳机,文件传输),BLE蓝牙,蓝牙硬件支持(Android设备,iOS设备),蓝牙独享,无符号十六进制转换

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1