最新SPP蓝牙总结Word下载.docx
- 文档编号:18138390
- 上传时间:2022-12-13
- 格式:DOCX
- 页数:11
- 大小:44.72KB
最新SPP蓝牙总结Word下载.docx
《最新SPP蓝牙总结Word下载.docx》由会员分享,可在线阅读,更多相关《最新SPP蓝牙总结Word下载.docx(11页珍藏版)》请在冰豆网上搜索。
开始扫描外围设备
c)退出,停止搜索,注销播送
mBtAdapter.cancelDiscovery();
privatevoidunregisterReceiver(){
if(mReceiverRegisted&
&
mContext!
=null){
mContext.unregisterReceiver(mReceiver);
mReceiverRegisted=false;
}
3、蓝牙连接
mConnectThread=newConnectThread(device,secure);
mConnectThread.start();
新建连接线程并启动
privateclassConnectThreadextendsThread{
privatefinalBluetoothSocketmmSocket;
privatefinalBluetoothDevicemmDevice;
privateStringmSocketType;
publicConnectThread(BluetoothDevicedevice,booleansecure){
mmDevice=device;
BluetoothSockettmp=null;
mSocketType=secure?
"
Secure"
:
Insecure"
;
try{
if(secure){
tmp=device.createRfcommSocketToServiceRecord(SPP_UUID);
}else{
tmp=device
.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
}catch(IOExceptione){
mmSocket=tmp;
publicvoidrun(){
setName("
ConnectThread"
+mSocketType);
if(mmSocket!
mmSocket.connect();
mmSocket.close();
}catch(IOExceptione2){}
connectionFailed(mmDevice);
return;
synchronized(BluetoothConnectClient.this){
mConnectThread=null;
mCount=0;
connected(mmSocket,mmDevice,mSocketType);
}
连接
publicvoidcancel(){
if(null!
=mmSocket){
Logger.e(TAG+"
connectcancle:
"
e);
publicsynchronizedvoidconnected(BluetoothSocketsocket,
BluetoothDevicedevice,finalStringsocketType){
ReadWriterBufferbuffer=newReadWriterBuffer();
mWriteThread=newWriteThread(device,socket,socketType,buffer);
mWriteThread.start();
OutputStreamtmpOut=null;
tmpOut=socket.getOutputStream();
connect"
mRunnable=newThread(newWriteRunnable(device,buffer,tmpOut));
mRunnable.start();
mReadThread=newReadThread(device,socket,socketType);
mReadThread.start();
c)创立读线程,创立写线程
d)写线程WriteRunnable,写入本地缓冲区域ReadWriterBuffer;
写WriteThread,写入输出流。
classWriteRunnableimplementsRunnable{
privateReadWriterBuffermBuffer;
privateOutputStreammOut;
privateWriteRunnable(BluetoothDevicedevice,ReadWriterBufferbuffer,
OutputStreamout){
this.mmDevice=device;
this.mBuffer=buffer;
this.mOut=out;
@Override
while(mFlag){
mBuffer.write(mOut);
Thread.sleep(Constants.INTERVEL_TIME);
}catch(InterruptedExceptione){
mFlag:
+mFlag,e);
privateclassWriteThreadextendsHandlerThread{
privateHandlermmWriteHandler;
publicWriteThread(BluetoothDevicedevice,BluetoothSocketsocket,
StringsocketType,ReadWriterBufferbuffer){
super("
WriteThread"
);
publicvoidstart(){
super.start();
mmWriteHandler=newHandler(this.getLooper()){
publicvoidhandleMessage(Messagemsg){
if(msg.what==MESSAGE_DATA_WRITHE){
byte[]data=(byte[])msg.obj;
intlen=data.length;
mBuffer.read(data,len);
};
publicvoidwrite(byte[]data){
if(mmWriteHandler!
Message.obtain(mmWriteHandler,MESSAGE_DATA_WRITHE,data)
.sendToTarget();
this.quit();
e)
publicclassReadWriterBuffer{
privateStringTAG="
ReadWriterBuffer"
privateintmPos;
privatebyte[]mBuffer=newbyte[1024];
privateReadWriteLocklock;
publicReadWriterBuffer(){
lock=newReentrantReadWriteLock();
publicvoidread(byte[]res,intlen){
lock.readLock().lock();
System.arraycopy(res,0,mBuffer,mPos,len);
mPos=mPos+len;
lock.readLock().unlock();
publicvoidwrite(OutputStreamoutStream){
lock.writeLock().lock();
if(mPos!
=0){
outStream.write(mBuffer,0,mPos);
outStream.flush();
Logger.d(TAG+"
write"
mPos=0;
lock.writeLock().unlock();
}
自定义缓冲Buffer
f)创立读线程
privateclassReadThreadextendsThread{
privatefinalInputStreammmInStream;
publicReadThread(BluetoothDevicedevice,BluetoothSocketsocket,
StringsocketType){
mmSocket=socket;
InputStreamtmpIn=null;
tmpIn=socket.getInputStream();
mmInStream=tmpIn;
}
byte[]buffer=newbyte[1024];
intbytes;
while(true){
bytes=mmInStream.read(buffer);
dataReceived(buffer,bytes);
connectionLost(mmDevice);
break;
g)使用Handler把数据抛给主线程
privatevoidsendDataReceivedMessage(byte[]data){
if(mMainHandler!
Messagemsg=mMainHandler.obtainMessage(MESSAGE_DATA_RECEIVED);
msg.obj=data;
mMainHandler.sendMessage(msg);
privateHandlermMainHandler=newHandler(Looper.getMainLooper()){
if(mConnectClientListener==null){
switch(msg.what){
caseMESSAGE_DATA_RECEIVED:
{
mDataDecoder.parseKeyEvent(data,data.length);
default:
h)解析数据
voidparseKeyEvent(byte[]data,intlen){
StringBuffersb=newStringBuffer();
for(inti=0;
i<
len;
i++){
sb.append(data[i]+"
|"
if((len<
3)||(mDataParsedListener==null)){
if(len==3){
parseData(data,len);
intl=len%3;
System.arraycopy(data,0,buffer,pos,len-l);
intsize=len-l+pos;
intoffset=0;
if(size>
=1024){
size"
+size);
while(offset<
size){
byte[]tmp=newbyte[3];
intlength=tmp.length;
if(offset+length>
pos=0;
System.arraycopy(buffer,offset,tmp,0,tmp.length);
parseData(tmp,length);
offset=offset+length;
pos=size-offset;
补充知识点:
蓝牙配对,连接蓝牙标准profile,蓝牙效劳〔音响,耳机,文件传输〕,BLE蓝牙,蓝牙硬件支持〔Android设备,iOS设备〕,蓝牙独享,无符号十六进制转换
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 最新 SPP 蓝牙 总结
![提示](https://static.bdocx.com/images/bang_tan.gif)