2012-07-11 80 views
8

嗨朋友, 我試圖通過使用第三方API稱爲bluecove與obex從Android設備的文件發送到特定的藍牙設備。我可以連接設備,當連接頭設置我面臨一個問題,對等連接重置。藍牙對等連接重置

這裏我的下面的代碼片段。任何對此的幫助表示讚賞。

在我的活動我使用下面的代碼來處理文件發送

BlueCoveImpl.setConfigObject(
       BlueCoveConfigProperties.PROPERTY_ANDROID_CONTEXT, 
       this); 
String deviceAddress = "0007ABB6D96E"; 
      int channel = 9; 
      String obexURL = "btgoep://" + deviceAddress + ":" 
        + "0000110600001000800000805f9b34fb" + ";android=true"; 

      // String obexURL = "btgoep://" + deviceAddress + ":" + channel 
      // + ";android=true"; 
      String fileToSend = "sdcard/DSCN9379.jpg"; 
      System.out.println("Connecting to " + obexURL); 
      FileInputStream stream = new FileInputStream(fileToSend); 
      File f = new File(fileToSend); 
      int size = (int) f.length(); 
      byte file[] = new byte[size]; 
      stream.read(file); 
      String filename = f.getName(); 
      System.out 
        .println("***************Now sending file to device*****************"); 
      SendFileTask task = new SendFileTask(MainActivity.this, obexURL, 
        file, filename); 
      Thread thread = new Thread(task); 
      thread.start(); 

================下面,我試圖連接bluecove連接包含標題的類對象如下所示。

package com.example.bluecovesample; 

import java.io.OutputStream; 

import javax.microedition.io.Connection; 
import javax.microedition.io.Connector; 
import javax.obex.ClientSession; 
import javax.obex.HeaderSet; 
import javax.obex.Operation; 
import javax.obex.ResponseCodes; 

//import static write ; 
import android.content.Context; 
import android.widget.Toast; 

public class SendFileTask implements Runnable { 

    private byte[] file; 
    private String filename; 
    public static final int WRITE = 2; 

    String logString; 

    int responseCode; 
    Context context; 
    String obexURL; 

    public SendFileTask(Context ctx, String obexUrl, byte[] file, 
      String filename) { 
     this.context = ctx; 
     this.obexURL = obexUrl; 
     this.file = file; 
     this.filename = filename; 

    } 

    public void run() { 
     Connection connection = null; 
     try { 
      System.out.println(obexURL); 
      // for (int i = 0; i 3; i++) 
      { 
       // connection = Connector.open("btgoep://"+btConnectionURL+ 
       // ":6"); 
       connection = Connector.open(obexURL); 
      } 
      // connection obtained 

      // now, let's create a session and a headerset objects 
      ClientSession cs = (ClientSession) connection; 

      HeaderSet hs = cs.createHeaderSet(); 
      // toastMsg(hs.toString()); 
      // now let's send the connect header 
      // cs.notifyAll(); 

      **cs.connect(hs);** 

      hs.setHeader(HeaderSet.NAME, filename); 
      // System.out.println("sfname:"+filename); 
      int dotIndex = filename.lastIndexOf("."); 
      // System.out.println("doti:"+dotIndex); 
      String extension = filename.substring(dotIndex).toLowerCase(); 
      // System.out.println("sfname:"+extension); 

      if (extension.equals(".txt")) { 
       hs.setHeader(HeaderSet.TYPE, "text/plain"); 
      } else if (extension.equals(".jpg") || extension.equals(".jpeg")) { 
       hs.setHeader(HeaderSet.TYPE, "image/jpeg"); 
      } else if (extension.equals(".mpeg") || extension.equals(".mpg") 
        || extension.equals(".mp3")) { 
       hs.setHeader(HeaderSet.TYPE, "video/mpeg"); 
      } else if (extension.equals(".wav")) { 
       hs.setHeader(HeaderSet.TYPE, "audio/x-wav"); 
      } else if (extension.equals(".3gp")) { 
       hs.setHeader(HeaderSet.TYPE, "image/jpeg"); 
      } else if (extension.equals("mid") || extension.equals("rmi")) { 
       hs.setHeader(HeaderSet.TYPE, "audio/mid"); 
      } 

      hs.setHeader(HeaderSet.LENGTH, new Long(file.length)); 

      Operation putOperation = cs.put(hs); 

      // System.out.println("response code:"+putOperation.getResponseCode()); 

      // putOperation.getResponseCode(); 
      // this.responseCode = putOperation.getResponseCode(); 

      OutputStream outputStream = putOperation.openOutputStream(); 
      outputStream.write(file); 
      // file push complete 

      outputStream.close(); 
      responseCode = putOperation.getResponseCode(); 

      putOperation.close(); 

      cs.disconnect(null); 

      connection.close(); 

      // file successfully sent 

      System.out.println("RESPONSE CODE " + responseCode); 
      if (responseCode == ResponseCodes.OBEX_HTTP_OK) { 
       System.out.println("FILE SUCCESSFULLY SENT " + filename); 
      } 

      else { 
       System.out.println("FILE SUCCESSFULLY NOT SENT" + filename 
         + " not in exception"); 

      } 

     } catch (Exception e) { 

      System.out.println("FILE SUCCESSFULLY NOT SENT" + filename 
        + " in exception"); 
      // System.out.println("In exception"); 
      e.printStackTrace(); 

      try { 
       connection.close(); 
      } catch (Exception ex) { 
       System.out.println("error closing connection" + ex.toString()); 
      } 
     } 
    } 

    private void toastMsg(String msg) { 
     Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); 
    } 

} 

在執行上面的代碼我得到連接接受目的地設備上通知對話框,然後通知在在執行線* cs.connect(HS)等連接休息;在我的logcat。 logcat的堆棧跟蹤:

07-11 14:48:07.044:W/System.err的(4527):產生java.io.IOException:由對等07-11 連接復位14:48:07.052:W /System.err(4527): at android.bluetooth.BluetoothSocket.readNative(Native Method)07-11 14:48:07.059:W/System.err(4527):at android.bluetooth.BluetoothSocket.read( BluetoothSocket.java:336)07-11 14:48:07.059:W/System.err(4527):at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) 07-11 14:48:07.059 :W/System.err(4527):在 com.intel.bluetooth.BluetoothStackAndroid.connectionR fRead(BluetoothStackAndroid.java:437) 07-11 14:48:07.059:W/System.err(4527):at com.intel.bluetooth.BluetoothRFCommInputStream.read(BluetoothRFCommInputStream.java:139) 07-11 14 :48:07.059:W/System.err(4527):at com.intel.bluetooth.obex.OBEXUtils.readFully(OBEXUtils.java:71)07-11 14:48:07.059:W/System.err( 4527):at com.intel.bluetooth.obex.OBEXSessionBase.readPacket(OBEXSessionBase.java:217) 07-11 14:48:07.059:W/System.err(4527):at com.intel.bluetooth。 OBEXClientSessionImpl.connectImpl(OBEXClientSessionImpl.java:100) 07-11 14: ) 07-11 14:48:07.059:W/System.err(4527):at com.example.bluecovesample.SendFileTask.run(SendFileTask.java:78) 07-11 14:48:07.059:W/System .err(4527):at java.lang.Thread.run(Thread.java:1019)

回答

3

由於某種原因,客戶端正在切斷連接。這可能是由於幾個原因,例如發送錯誤的數據或錯誤的配對。你確定你正確配對設備嗎?

沒有輸入配對碼可能是一個問題。這使配對不當。

修復方法是進入設置 - >無線&網絡 - >藍牙設置,長按「配對」設備並選擇「取消配對」,然後單擊產生「藍牙配對請求」窗口的設備文本框,我輸入PIN碼(配對碼)。完成後,配對將會成功。

您還可以使用一個名爲Wireshark的程序來分析通過協議發送的消息。

+0

...我可以連接,但當我達到cs.connect(hs);試圖設置連接設備的headeret發送文件,它會斷開連接。所有其他解決方案。 – 2012-07-14 21:41:25

+0

您的清單中是否具有READ_EXTERNAL_STORAGE和BLUETOOTH權限? – Erol 2012-07-14 23:43:26

+1

如果是的話,看看這裏的源代碼:http://gitorious.org/android-obex並比較你的代碼。 – Erol 2012-07-14 23:51:01

0
import javax.obex.ClientSession; 

您使用此類「ClientSession」。

據我所知,createHeaderSet是不是一個ClientSession

的方法,我不知道你是如何成功編譯您的應用程序。