【问题标题】:How to pair and connect a device programmatically using bluetooth in Android?如何在 Android 中使用蓝牙以编程方式配对和连接设备?
【发布时间】:2012-04-15 12:54:11
【问题描述】:

在 StackOverflow 上已经有很多关于如何将远程蓝牙设备与 android 配对和连接的问题。我已经尝试了所有这些,没有找到任何有关与远程设备配对的正确链接或文档。

另外关于我尝试以编程方式将我的笔记本电脑与 android 连接,但我收到以下错误:

java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:395)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:209)

连接安卓设备有很多参考,我关注的一个是this。但在我的情况下它不起作用。我正在尝试连接我的 vaio 笔记本电脑,并且我有三星 Galaxy S 安卓设备。

如果有人知道如何以编程方式配对和连接设备,请告诉我解决方案。

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    如果服务器实际上不可发现,您的客户端将永远不会发现服务器。您的服务器代码的注释说“确保它可发现的设备”,但在套接字上侦听并不意味着该设备是可发现的。您可以通过调用使服务器可发现:

    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
    startActivity(discoverableIntent);
    

    Android 开发指南中详细介绍了这一切:http://developer.android.com/guide/topics/wireless/bluetooth.html

    您的应用程序必须在清单文件中具有以下蓝牙权限:

    android.permission.BLUETOOTH_ADMIN
    android.permission.BLUETOOTH
    

    【讨论】:

    • 我不是在制作 Android 聊天客户端应用程序,其中两个对等方都是 Android。在这里,我尝试连接笔记本电脑和汽车的蓝牙,我认为使用 A2dp,我必须连接这些设备。如果有人有经验,请分享..
    猜你喜欢
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2011-07-07
    • 1970-01-01
    • 2017-07-30
    • 2012-12-23
    • 1970-01-01
    相关资源
    最近更新 更多