SPP蓝牙总结

SPP蓝牙总结

ID:38983447

大小:70.92 KB

页数:10页

时间:2019-06-23

SPP蓝牙总结_第1页
SPP蓝牙总结_第2页
SPP蓝牙总结_第3页
SPP蓝牙总结_第4页
SPP蓝牙总结_第5页
资源描述:

《SPP蓝牙总结》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、SPP蓝牙总结一、SPP蓝牙使用1、添加权限2、蓝牙搜索IntentFilterfilter=newIntentFilter(BluetoothDevice.ACTION_FOUND);filter.addAction(BluetoothAdapter.ACTION_DISCOVER

2、Y_FINISHED);filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);mContext.registerReceiver(mReceiver,filter);a)注册广播mBtAdapter.startDiscovery();b)开始扫描外围设备c)退出,停止搜索,注销广播mBtAdapter.cancelDiscovery();privatevoidunregisterReceiver(){if(mReceiverRegisted&&mContext!=null){mCo

3、ntext.unregisterReceiver(mReceiver);mReceiverRegisted=false;}}3、蓝牙连接mConnectThread=newConnectThread(device,secure);mConnectThread.start();a)新建连接线程并启动privateclassConnectThreadextendsThread{privatefinalBluetoothSocketmmSocket;privatefinalBluetoothDevicemmDevice;privateStr

4、ingmSocketType;publicConnectThread(BluetoothDevicedevice,booleansecure){mmDevice=device;BluetoothSockettmp=null;mSocketType=secure?"Secure":"Insecure";try{if(secure){tmp=device.createRfcommSocketToServiceRecord(SPP_UUID);}else{tmp=device.createInsecureRfcommSocketToServ

5、iceRecord(SPP_UUID);}}catch(IOExceptione){}mmSocket=tmp;}publicvoidrun(){setName("ConnectThread"+mSocketType);if(mmSocket!=null){try{mmSocket.connect();}catch(IOExceptione){try{mmSocket.close();}catch(IOExceptione2){}connectionFailed(mmDevice);return;}synchronized(Bluet

6、oothConnectClient.this){mConnectThread=null;}mCount=0;connected(mmSocket,mmDevice,mSocketType);}}b)创建BluetoothSocket,并建立Socket连接publicvoidcancel(){try{if(null!=mmSocket){mmSocket.close();}}catch(IOExceptione){Logger.e(TAG+"connectcancle:",e);}}}publicsynchronizedvoidcon

7、nected(BluetoothSocketsocket,BluetoothDevicedevice,finalStringsocketType){ReadWriterBufferbuffer=newReadWriterBuffer();mWriteThread=newWriteThread(device,socket,socketType,buffer);mWriteThread.start();OutputStreamtmpOut=null;try{tmpOut=socket.getOutputStream();}catch(IO

8、Exceptione){Logger.e(TAG+"connect",e);}mRunnable=newThread(newWriteRunnable(device,buffer,tmpOut));mRunnable.s

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。