安卓蓝牙文件发送

21

我正在尝试在安卓设备上通过蓝牙发送文件。我已经进行了发现、连接并建立了蓝牙 socket。问题是,当我将字节数组写入蓝牙 socket 的输出流时,接收端没有接收到任何内容,尽管它承认有东西正在被发送。

这是我的代码(bad 是蓝牙适配器)

请给予建议。

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上打开BluetoothSocket。因此,只有当你尝试发送文件的设备正在侦听此通道时,你才能接收文件。 - jobnz
好的,我现在已经删除了那一行,但仍然无法工作。我启动了调试模式,它显示我的tmpOut(输出流)为空。这是否意味着我的蓝牙套接字存在问题?另外,设备默认会在RFComm通道1上侦听吗?还是我需要在接收设备中也有一个接收器?我只想发送一个文件,可以由另一部手机的默认蓝牙服务接收。 - exorcist
1
常见的应用程序,如文件传输,是在所谓的蓝牙配置文件中指定的(http://en.wikipedia.org/wiki/Bluetooth_profile),因此,如果您想使用“默认蓝牙服务”传输文件,则必须根据用于传输文件的OBEX配置文件进行操作(http://en.wikipedia.org/wiki/OBEX)。 - jobnz
http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices - 这应该能回答你所有的问题 - 编辑:还有这个:http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection - jobnz
你应该尝试使用tmpOut.flush()。我发现有时候它直到被刷新出去才会发送。 - FabianCook
显示剩余3条评论
3个回答

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();

如果我想将数据发送到蓝牙打印机适配器(例如 HP bt500),我应该在哪里编写服务器端代码? - Farid

1
为什么不使用标准的API调用,而要通过反射来调用呢?例如:
BluetoothSocket socket =  destination
                              .createRfcommSocketToServiceRecord(new UUID(...)) ;

另外,你的catch块是空的。你确定套接字连接没有任何异常吗?如果连接失败,Connect将抛出IOException异常。请参阅link


0

这可能是因为 dev 和 bs 在你的 try/catch 块内声明,而在使用 tmpout 之前就超出了作用域。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接