`
897371388
  • 浏览: 526870 次
文章分类
社区版块
存档分类
最新评论

android bluetooth开发基础-9管理连接 android蓝牙开发——管理连接

 
阅读更多

android蓝牙开发——管理连接

分类:google android应用开发1486人阅读评论(2)收藏举报

目录(?)[+]

当你成功地连接了两台(或多台)设备时,每个设备都有一个已连接的BluetoothSocket。这时你可以在设备之间共享数据,乐趣才刚开始。 使用BluetoothSocket,传输二进制数据的过程是简单的:

  1. 分别通过getInputStream()和getOutputStream()获得管理数据传输的InputStream和OutputStream。
  2. 通过read(byte[])和write(byte[])从流中读取或写入数据。

首先,你必须使用一个线程专门用于数据的读或写。这是非常重要的,因为read(byte[])和write(byte[])方法都是阻塞调用。read(byte[])将会阻塞到流中有数据可读。write(byte[])一般不会阻塞,但当远程设备的中间缓冲区已满而对方没有及时地调用read(byte[])时将会一直阻塞。所以,你的线程中的主循环将一直用于从InputStream中读取数据。 A separate public method in the thread can be used to initiate writes to theOutputStream.

Example

  1. privateclassConnectedThreadextendsThread{
  2. privatefinalBluetoothSocketmmSocket;
  3. privatefinalInputStreammmInStream;
  4. privatefinalOutputStreammmOutStream;
  5. publicConnectedThread(BluetoothSocketsocket){
  6. mmSocket=socket;
  7. InputStreamtmpIn=null;
  8. OutputStreamtmpOut=null;
  9. //Gettheinputandoutputstreams,usingtempobjectsbecause
  10. //memberstreamsarefinal
  11. try{
  12. tmpIn=socket.getInputStream();
  13. tmpOut=socket.getOutputStream();
  14. }catch(IOExceptione){}
  15. mmInStream=tmpIn;
  16. mmOutStream=tmpOut;
  17. }
  18. publicvoidrun(){
  19. byte[]buffer=newbyte[1024];//bufferstoreforthestream
  20. intbytes;//bytesreturnedfromread()
  21. //KeeplisteningtotheInputStreamuntilanexceptionoccurs
  22. while(true){
  23. try{
  24. //ReadfromtheInputStream
  25. bytes=mmInStream.read(buffer);
  26. //SendtheobtainedbytestotheUIActivity
  27. mHandler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
  28. }catch(IOExceptione){
  29. break;
  30. }
  31. }
  32. }
  33. /*CallthisfromthemainActivitytosenddatatotheremotedevice*/
  34. publicvoidwrite(byte[]bytes){
  35. try{
  36. mmOutStream.write(bytes);
  37. }catch(IOExceptione){}
  38. }
  39. /*CallthisfromthemainActivitytoshutdowntheconnection*/
  40. publicvoidcancel(){
  41. try{
  42. mmSocket.close();
  43. }catch(IOExceptione){}
  44. }}

当你成功地连接了两台(或多台)设备时,每个设备都有一个已连接的BluetoothSocket。这时你可以在设备之间共享数据,乐趣才刚开始。 使用BluetoothSocket,传输二进制数据的过程是简单的:

  1. 分别通过getInputStream()和getOutputStream()获得管理数据传输的InputStream和OutputStream。
  2. 通过read(byte[])和write(byte[])从流中读取或写入数据。

首先,你必须使用一个线程专门用于数据的读或写。这是非常重要的,因为read(byte[])和write(byte[])方法都是阻塞调用。read(byte[])将会阻塞到流中有数据可读。write(byte[])一般不会阻塞,但当远程设备的中间缓冲区已满而对方没有及时地调用read(byte[])时将会一直阻塞。所以,你的线程中的主循环将一直用于从InputStream中读取数据。 A separate public method in the thread can be used to initiate writes to theOutputStream.

Example

  1. privateclassConnectedThreadextendsThread{
  2. privatefinalBluetoothSocketmmSocket;
  3. privatefinalInputStreammmInStream;
  4. privatefinalOutputStreammmOutStream;
  5. publicConnectedThread(BluetoothSocketsocket){
  6. mmSocket=socket;
  7. InputStreamtmpIn=null;
  8. OutputStreamtmpOut=null;
  9. //Gettheinputandoutputstreams,usingtempobjectsbecause
  10. //memberstreamsarefinal
  11. try{
  12. tmpIn=socket.getInputStream();
  13. tmpOut=socket.getOutputStream();
  14. }catch(IOExceptione){}
  15. mmInStream=tmpIn;
  16. mmOutStream=tmpOut;
  17. }
  18. publicvoidrun(){
  19. byte[]buffer=newbyte[1024];//bufferstoreforthestream
  20. intbytes;//bytesreturnedfromread()
  21. //KeeplisteningtotheInputStreamuntilanexceptionoccurs
  22. while(true){
  23. try{
  24. //ReadfromtheInputStream
  25. bytes=mmInStream.read(buffer);
  26. //SendtheobtainedbytestotheUIActivity
  27. mHandler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
  28. }catch(IOExceptione){
  29. break;
  30. }
  31. }
  32. }
  33. /*CallthisfromthemainActivitytosenddatatotheremotedevice*/
  34. publicvoidwrite(byte[]bytes){
  35. try{
  36. mmOutStream.write(bytes);
  37. }catch(IOExceptione){}
  38. }
  39. /*CallthisfromthemainActivitytoshutdowntheconnection*/
  40. publicvoidcancel(){
  41. try{
  42. mmSocket.close();
  43. }catch(IOExceptione){}
  44. }}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics