gpt4 book ai didi

java - 通过蓝牙将数据从手机发送到笔记本电脑

转载 作者:行者123 更新时间:2023-11-30 10:44:12 24 4
gpt4 key购买 nike

我已经成功地将我的 Android (6.0.1) 手机 (Sony Xperia Z3) 与我的笔记本电脑(运行 Ubuntu 14.04)配对。我可以来回发送文件。蓝牙连接菜单显示,如果发送文件,连接开关切换到ON。

我使用以下方式建立了持久连接:

sudo rfcomm connect rfcomm0 [MAC ADDRESS] [CHANNEL]

我想通过蓝牙将数据从我的手机发送到我的笔记本电脑。如果我运行这个 code开关也会打开,但会立即关闭连接(开关会回到关闭状态)。

Logcat 在调用 init() 后显示以下警告:

W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback

在调用 write() 方法时出现此异常:

E/error: error init:java.io.IOException: read failed, socket might closed or timeout, read ret: -1

当使用 rfcomm 连接时,一些 channel 会失败并拒绝连接。我的猜测是我使用了错误的 channel 。

  • 调用 rfcomm 时如何知道使用哪个 channel ?
  • 如何在我的 Android 应用中指定此 channel ?
  • 我怎么知道要使用哪个 UUID?
  • 在示例代码中使用了第一个 UUID:为什么?

最佳答案

不知何故,我无法使用 createRfcommSocketToServiceRecord 方法让它工作。

然后我所做的是删除:

ParcelUuid[] uuids = device.getUuids();
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());

并将这些行替换为:

int channel = 1; // substitute with channel you are listening on
Method m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
BluetoothSocket socket = (BluetoothSocket) m.invoke(device, channel);

然后我发出 sudo rfcomm listen rfcomm0,然后显示它正在 Linux 终端上监听的 channel ,我终于可以连接了!

回答我自己的问题:

  • 调用 rfcomm 时如何知道使用哪个 channel ?
    Linux 终端在发出 sudo rfcomm listen rfcomm0

  • 时显示 channel
  • 如何在我的 Android 应用中指定此 channel ?
    我使用反射访问的方法现在有这个参数 (createRfcommSocket)

  • 我怎么知道要使用哪个 UUID?
    在这个解决方案中没有。

  • 示例代码中使用了第一个 UUID:为什么?
    对于找到的解决方案无关紧要。

关于java - 通过蓝牙将数据从手机发送到笔记本电脑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37489483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com