2014-01-31 31 views
2

我在Android中遇到蓝牙串行连接问题。有两个场景:Android蓝牙:java.io.IOException:传输端点未连接

  1. 一个Activity(带片段),一个EditTex和一个Button(在片段中)。该按钮建立与串行设备的连接并从EditText发送数据。这是工作,但是......我不想每次都建立连接。

  2. 一个活动(带片段),一个EditTex和两个Button(在片段中)。一个键建立连接,而另一个发送数据,好吧,当我按下连接按钮,它似乎做工精细(无例外),但是当我按下发送数据按钮logcat的告诉我:

java.io.IOException:传输端点没有连接“

当然,数据没有到达接收器。唯一的例外是投

out.write(txtData.getText().toString().getBytes()); 

我的代码(即第二写景我想):

public class MainFragment extends Fragment implements View.OnClickListener { 
    private EditText txtData; 
    private Button btnSend; 
    private Button btnConnect; 
    private InputStream in = null; 
    private byte[] buffer = null; 
    BluetoothSocket socket = null; 
    OutputStream out = null; 

    public MainFragment(){} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     txtData = (EditText) rootView.findViewById(R.id.txtData); 
     btnSend = (Button) rootView.findViewById(R.id.btnSend); 
     btnConnect = (Button) rootView.findViewById(R.id.btnConnect); 
     btnConnect.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       connectBluetooth();  
      } 
     }); 
     btnSend.setOnClickListener(this); 
     return rootView; 
    } 

    public void connectBluetooth(){ 
     BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 
     if (adapter == null) { 
      // Device does not support Bluetooth 
      getActivity().finish(); //exit 
     } 

     if (!adapter.isEnabled()) { 
//make sure the device's bluetooth is enabled 
      Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBluetooth, 0); 
     } 

     final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection 
     String mac = "00:1B:35:0B:75:BE"; //my device's mac adress 
     BluetoothDevice device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired 


     // Get a BluetoothSocket to connect with the given BluetoothDevice 

     try { 
      socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID); 
     } catch (IOException e) { 
      Log.d("socket","Error al crear el socket"); 
     } 

     try { 

      socket.connect();  
      out = socket.getOutputStream(); 
      in = socket.getInputStream(); 
      out.write(txtData.getText().toString().getBytes()); 
     } catch (IOException e) { 
     } 
    } 

    @Override 
    public void onClick(View view) { 
     try {  
      out.write(txtData.getText().toString().getBytes()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

编辑:这是不是其他蓝牙设备的问题,因为我有一个应用程序测试它在谷歌播放,它的作品。

回答

1

最后我用createInsecureRfcommSocketToServiceRecord(SERIAL_UUID)代替createRfcommSocketToServiceRecord(SERIAL_UUID)

1

在发送数据之前确定两个设备已连接吗?

此外,你应该做一个单独的线程,而不是在你的主线程的所有蓝牙连接。

+0

两台设备conected解决我的问题。但所有代码都在主线程(ui线程)中,因为我只想连接并发送一次数据,我不想监听数据。 – RdlP

+0

连接另一端的设备是什么? 连接工作后写入权限还是两者都给你错误? – Braunster

+0

一个设备是我的android(2.3),另一个设备是串行设备通过串行-USB转换器连接到PC。在第二个场景中(一个按钮建立连接,另一个按钮发送数据),当我尝试发送数据(它引发了一个异常)时,我只会遇到错误,当我建立连接时它不会抛出错误。 – RdlP