【问题标题】:Android Bluetooth file sending安卓蓝牙文件发送
【发布时间】: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


【解决方案1】:

为什么不使用标准的api调用而不是通过反射调用,例如:

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

你的 catch 块也是空的。你确定套接字没有任何异常连接吗?如果由于某种原因连接失败,Connect 将抛出 IOException。看到这个link

【讨论】:

    【解决方案2】:

    我正在使用以下代码连接到远程蓝牙设备中的串行服务,它对我来说工作正常。只需确保其他设备(可以是移动设备或 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),我在哪里可以编写服务器端代码?
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-18
      • 2011-08-13
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多