欢迎来到天天文库
浏览记录
ID:38983447
大小:70.92 KB
页数:10页
时间:2019-06-23
《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
此文档下载收益归作者所有