我想通過藍牙在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();
}
你是這麼做的: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
好的,我現在刪除了該行,但仍然無法工作。我啓動了調試器模式,它顯示我的tmpOut(outputstream)爲空。 Soes這意味着我的藍牙套接字有問題嗎?此外,設備是否默認偵聽RFComm通道1,或者是否還需要在接收器設備中安裝接收器?我只想發送一個可以被另一部手機的默認藍牙服務接收的文件。 – exorcist 2012-07-16 11:10:31
所謂的藍牙配置文件(http://en.wikipedia.org/wiki/Bluetooth_profile)中指定了文件傳輸等常見應用程序,因此如果您想使用「默認藍牙服務」傳輸文件,則必須根據用於傳輸文件的OBEX配置文件(http://en.wikipedia.org/wiki/OBEX) – Weeman 2012-07-16 11:17:55