Android蓝牙串口通信模板及小trick


Android蓝牙操作:与蓝牙串口模块通信,或其他蓝牙设备通信。

初涉android的蓝牙操作,按照固定MAC地址连接获取Device时,程序始终是异常终止,查了好多天代码都没查出原因。今天改了一下API版本,突然就成功连接了。总结之后发现果然是个坑爹之极的错误。

为了这种错误拼命查原因浪费大把时间是非常不值得的,但是问题不解决更是揪心。可惜我百度了那么多,都没有给出确切原因。今天特此mark,希望后来者遇到这个问题的时候能轻松解决。

下面是我的连接过程,中间崩溃原因及解决办法。

1:用AT指令获得蓝牙串口的MAC地址,地址是简写的,按照常理猜测可得标准格式。

2:开一个String adress= "************" //MAC地址, String MY_UUID= "************"//UUID根据通信而定,网上都有。

3:取得本地Adapter用getDefaultAdapter(); 远程的则用getRemoteDevice(adress); 之后便可用UUID开socket进行通信。

如果中途各种在getRemoteDevice处崩溃,大家可以查看一下当前的API版本,如果是2.1或以下版本的话,便能确定是API版本问题,只要换成2.2或者以上就都可以正常运行了~   这么坑爹的错误的确很为难初学者。  唉··········  为这种小trick浪费很多时间真是难过。

下面附上Android蓝牙操作中用固定MAC地址传输信息的模板,通用搜索模式日后再补删模板:

  1. private BluetoothAdapter mBluetoothAdapter = null;      
  2.       
  3. private BluetoothSocket btSocket = null;      
  4.       
  5. private OutputStream outStream = null;      
  6.       
  7. private InputStream inStream = null;      
  8.       
  9. private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");  //这条是蓝牙串口通用的UUID,不要更改       
  10.       
  11. private static String address = "00:12:02:22:06:61"// <==要连接的蓝牙设备MAC地址       
  12.       
  13.       
  14. /*获得通信线路过程*/      
  15.       
  16.       
  17. /*1:获取本地BlueToothAdapter*/      
  18. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();      
  19. if(mBluetoothAdapter == null)       
  20. {      
  21.     Toast.makeText(this"Bluetooth is not available.", Toast.LENGTH_LONG).show();      
  22.     finish();      
  23.     return;      
  24. }      
  25. if(!mBluetoothAdapter.isEnabled())       
  26. {      
  27.     Toast.makeText(this"Please enable your Bluetooth and re-run this program.", Toast.LENGTH_LONG).show();      
  28.     finish();      
  29.     return;      
  30. }       
  31.        
  32. /*2:获取远程BlueToothDevice*/       
  33.     BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);      
  34. if(mBluetoothAdapter == null)       
  35. {      
  36.     Toast.makeText(this"Can't get remote device.", Toast.LENGTH_LONG).show();      
  37.     finish();      
  38.     return;      
  39. }      
  40.        
  41. /*3:获得Socket*/            
  42.     try {      
  43.     btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);      
  44. catch (IOException e) {      
  45.       
  46.     Log.e(TAG, "ON RESUME: Socket creation failed.", e);      
  47.       
  48. }      
  49.       
  50. /*4:取消discovered节省资源*/      
  51. mBluetoothAdapter.cancelDiscovery();              
  52.       
  53.       
  54. /*5:连接*/      
  55.       
  56. try {      
  57.       
  58.     btSocket.connect();      
  59.       
  60.     Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");      
  61.       
  62. catch (IOException e) {      
  63.       
  64.     try {      
  65.         btSocket.close();      
  66.       
  67.     } catch (IOException e2) {      
  68.       
  69.         Log .e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);      
  70.     }      
  71. }       
  72.           
  73. /*此时可以通信了,放在任意函数中*/      
  74. /*  try {   
  75. outStream = btSocket.getOutputStream();   
  76.   
  77. inStream = btSocket.getInputStream(); //可在TextView里显示   
  78.   
  79. } catch (IOException e) {   
  80.     Log.e(TAG, "ON RESUME: Output stream creation failed.", e);   
  81. }   
  82.    
  83.    
  84. String message = "1";   
  85.    
  86. byte[] msgBuffer = message.getBytes();   
  87.    
  88. try {   
  89.     outStream.write(msgBuffer);   
  90.    
  91. } catch (IOException e) {   
  92.     Log.e(TAG, "ON RESUME: Exception during write.", e);   
  93. }   
  94. */   

相关内容