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