【发布时间】:2012-07-15 04:39:03
【问题描述】:
我正在尝试在 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 通道 1 上打开蓝牙套接字。因此,只有当您尝试发送文件的设备正在侦听此通道时,您才能接收到文件
-
好的,我现在删除了该行,但仍然无法正常工作。我启动了调试器模式,它显示我的 tmpOut(输出流)为空。这是否意味着我的蓝牙插座有问题?设备是否默认在 RFComm 通道 1 上侦听,或者我是否需要在接收器设备中也有接收器?我只想发送一个可以被另一部手机的默认蓝牙服务接收的文件。
-
文件传输等常见应用程序在所谓的蓝牙配置文件 (en.wikipedia.org/wiki/Bluetooth_profile) 中指定,因此如果您想使用“默认蓝牙服务”传输文件,您必须根据 OBEX 配置文件进行用于传输文件(en.wikipedia.org/wiki/OBEX)
-
我认为我发布问题的方式可能有点糟糕。我基本上想通过蓝牙发送文件而不使用内置的 Android Intent。我怎样才能做到这一点?
标签: android file bluetooth transfer