繁体   English   中英

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

[英]Send data from phone to laptop via Bluetooth

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

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

sudo rfcomm connect rfcomm0 [MAC ADDRESS] [CHANNEL]

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

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

W/BluetoothAdapter: getBluetoothService() 在没有 BluetoothManagerCallback 的情况下被调用

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

E/error: error init:java.io.IOException: 读取失败,套接字可能关闭或超时,读取 ret: -1

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

  • 调用rfcomm时如何知道要使用哪个频道?
  • 如何在我的 Android 应用程序中指定此频道?
  • 我怎么知道要使用哪个 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 终端上侦听的频道,我终于可以连接了!

回答我自己的问题:

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM