【问题标题】:Send data from phone to laptop via Bluetooth通过蓝牙将数据从手机发送到笔记本电脑
【发布时间】:2016-09-26 03:32:51
【问题描述】:

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

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

sudo rfcomm connect rfcomm0 [MAC ADDRESS] [CHANNEL]

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

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

W/BluetoothAdapter:没有调用 getBluetoothService() 蓝牙管理器回调

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

E/error: error init:java.io.IOException: read failed, socket may 关闭或超时,读取 ret: -1

使用rfcomm 连接时,某些通道失败并拒绝连接。我的猜测是我使用了错误的频道。

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

【问题讨论】:

  • 嘿...一直以来,我都只是通过 USB 将手机插入。 //完全没有帮助...

标签: java android bluetooth rfcomm


【解决方案1】:

不知何故,我无法使用 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终端上监听的频道,我终于可以连接了!

回答我自己的问题:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-21
    • 2016-02-29
    • 2013-01-28
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多