Android蓝牙文件发送

exo*_*ist 21 android bluetooth file transfer

我想在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();
    }
Run Code Online (Sandbox Code Playgroud)

bil*_*vis 0

这可能是因为 dev 和 bs 在使用 tmpout 之前超出了范围,因为它们是在 try/catch 块中声明的。