2012-07-16 155 views
21

我想通過藍牙在Android設備上發送文件。我已經完成了發現,連接並製作了一個藍牙套接字。問題是當我在藍牙套接字的輸出流中寫入字節數組時,接收端雖然接受某些內容正在發送,卻沒有收到任何內容。Android藍牙文件發送

這裏的蔭做什麼(不好的是藍牙適配器)

請指教。

try 
    { 
     BluetoothDevice dev = bad.getRemoteDevice(a); 
     bad.cancelDiscovery(); 

     dev.createRfcommSocketToServiceRecord(new UUID(1111, 2222)); 
     Method m = dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
     bs = (BluetoothSocket) m.invoke(dev, Integer.valueOf(1)); 
     bs.connect(); 
     tmpOut = bs.getOutputStream(); 
    }catch(Exception e) 
    { 

    } 

    File f = new File(filename); 

    byte b[] = new byte[(int) f.length()]; 
    try 
    { 
     FileInputStream fileInputStream = new FileInputStream(f); 
     fileInputStream.read(b); 
    }catch(IOException e) 
    { 
     Log.d(TAG, "Error converting file"); 
     Log.d(TAG, e.getMessage()); 
    } 

    try { 
     tmpOut.write(b); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
+0

你是這麼做的:dev.createRfcommSocketToServiceRecord(new UUID(1111,2222));?您使用UUID創建BluetoothSocket而不使用它。方法m = dev.getClass()。getMethod(「createRfcommSocket」,new Class [] {int.class});bs =(BluetoothSocket)m.invoke(dev,Integer.valueOf(1));在RfComm Channel 1上打開BluetoothSocket。因此,只有當您試圖發送文件的設備正在此通道上偵聽時,您才能收到文件 – Weeman 2012-07-16 10:52:24

+0

好的,我現在刪除了該行,但仍然無法工作。我啓動了調試器模式,它顯示我的tmpOut(outputstream)爲空。 Soes這意味着我的藍牙套接字有問題嗎?此外,設備是否默認偵聽RFComm通道1,或者是否還需要在接收器設備中安裝接收器?我只想發送一個可以被另一部手機的默認藍牙服務接收的文件。 – exorcist 2012-07-16 11:10:31

+1

所謂的藍牙配置文件(http://en.wikipedia.org/wiki/Bluetooth_profile)中指定了文件傳輸等常見應用程序,因此如果您想使用「默認藍牙服務」傳輸文件,則必須根據用於傳輸文件的OBEX配置文件(http://en.wikipedia.org/wiki/OBEX) – Weeman 2012-07-16 11:17:55

回答

0

這可能是因爲開發和BS走出tmpout前範圍使用,因爲他們是你的try/catch塊內聲明。

3

我正在使用下面的代碼剪去連接到遠程藍牙設備中的串行服務,它對我來說工作正常。只要確保其他設備(可移動或PC)擁有藍牙串口通訊服務器套接字(請參閱下面的服務器端代碼)

客戶端:

UUID serialUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
BluetoothDevice btDevice = btAdapter.getRemoteDevice(BTAddress); // Get the BTAddress after scan 
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(SERIAL_UUID); 
btSocket.connect(); 
InputStream iStream = btSocket.getInputStream(); 
OutputStream oStream = btSocket.getOutputStream(); 

服務器端:

UUID serialUUID = new UUID("1101", true); 
String serviceURL = "btspp://localhost:" + serialUUID 
     + ";name=Android BT Server;authorize=false;authenticate=false"; 
StreamConnectionNotifier connectionNotifier = (StreamConnectionNotifier) Connector 
         .open(serviceURL); 
// Blocking method will wait for client to connect 
StreamConnection connection = connectionNotifier.acceptAndOpen(); 

RemoteDevice remoteDevice = RemoteDevice.getRemoteDevice(connection); 
InputStream btInput = connection.openInputStream(); 
OutputStream btOutput = connection.openOutputStream(); 
+0

如果我想發送數據到藍牙打印機適配器(例如HP bt500),我可以在哪裏寫服務器端代碼? – FARID 2017-02-16 11:26:36

1

爲什麼不使用標準的API調用,而不是通過反射調用,如:

BluetoothSocket socket = destination 
           .createRfcommSocketToServiceRecord(new UUID(...)) ; 

另外你的catch塊是空的。你確定插座連接沒有任何異常嗎?如果由於某種原因連接失敗,Connect將拋出IOException。看到這個link