2013-03-08 80 views
1

我需要發送一個字符串到藍牙設備。但在發送時,我在java.io.OutputStream.write(byte[])方法上得到了一個Exeption java.io.IOException: Transport endpoint is not connectedandroid java.io.IOException:傳輸端點未連接

代碼如下所示。該代碼僅從配對設備列表中搜索特定設備併發送字符串。

public class MainActivity extends Activity { 

    TextView out; 
    private static final int REQUEST_ENABLE_BT = 1; 
    private BluetoothAdapter btAdapter; 
    private ArrayList<BluetoothDevice> btDeviceList = new ArrayList<BluetoothDevice>(); 
    private ArrayAdapter<String> mPairedDevicesArrayAdapter; 
    private ArrayAdapter<String> mNewDevicesArrayAdapter; 
    BluetoothDevice device1; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     out = (TextView) findViewById(R.id.out); 
     btAdapter = BluetoothAdapter.getDefaultAdapter(); 
     ListpairedDevices(); 
    } 

    /* This routine is called when an activity completes. */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == REQUEST_ENABLE_BT) { 
      CheckBTState(); 
     } 
    } 

    public void ListpairedDevices() { 
     Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices(); 

     // If there are paired devices, add each one to the ArrayAdapter 
     if (pairedDevices.size() > 0) { 
      out.append("\nPaired Devices \n"); 
      for (BluetoothDevice device : pairedDevices) { 
       out.append("\n Device123: " + device.getName() + "," + device); 
       //mPairedDevicesArrayAdapter.add("\n Device123: " + device.getName() + "," + device); 
       String dv=device.toString(); 
       if(dv.contains("00:1B:EE:82:31:1E")) 
       { 
       device1=device; 
       } 
      } 
     } else { 
      out.append("\nNo Pared Device"); 
     } 
     out.append("\nDiscovered Devices"); 
    } 

    public void sendtext(View v) {//button click 

    Set<BluetoothDevice> bd=btAdapter.getBondedDevices(); 
    sendDataToPairedDevice("message1"); 
    } 
    private void sendDataToPairedDevice(String message){  
     byte[] toSend = message.getBytes(); 
     try { 
     UUID applicationUUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66"); 
     BluetoothSocket socket = device1.createInsecureRfcommSocketToServiceRecord(applicationUUID); 
     OutputStream mmOutStream = socket.getOutputStream(); 
     mmOutStream.write(toSend); 
    } catch (IOException e) { 
     Log.e("Exception during write", e.toString()); 
    } 
} 
} 

回答

2

編輯:

您需要連接到插座,在這之前,你需要取消發現

btAdapter.cancelDiscovery(); 
socket.connect(); 

UUID工作了基蘭KJ是

00001105-0000- 1000-8000-00805F9B34FB


你還沒有拿到藍牙適配器,你剛剛宣佈它。

獲取藍牙適配器這樣使用靜態工廠方法

中的onCreate加入這個

在listpaireddevice已經宣佈
btAdapter=BluetoothAdapter.getDefaultAdapter(); 
+0

()函數 – 2013-03-08 08:47:09

+0

這是集 pairedDevices = btAdapter.getBondedDevices() ;你的listpaireddevice()方法中的第一行 – Pragnani 2013-03-08 08:47:56

+0

你已經使用過adatper,但是當時你還沒有拿到適配器 – Pragnani 2013-03-08 08:48:22

相關問題