【问题标题】:Connect to specific Bluetooth device with a click单击即可连接到特定的蓝牙设备
【发布时间】:2015-08-10 13:52:27
【问题描述】:

我正在尝试使用我的 Android APP 连接到特定设备,直到现在我能够做的是让配对的项目这样做:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set < BluetoothDevice > pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
    for (BluetoothDevice device: pairedDevices) {
        mDeviceName.add(device.getName());
        mDeviceMAC.add(device.getAddress());

    }
}
bluetoothClass.setDeviceName(mDeviceName);
bluetoothClass.setDeviceMac(mDeviceMAC);

我在哪里可以得到我所有配对设备的MACDevice name。我想做的事情是当我选择一个连接到Bluetooth 设备时。

示例

Samsung S4 上,当我打开Bluetooth 时,它会弹出一个Dialog,其中包含我所有已配对的设备,当我点击它连接的任何人时(我已经能够...)所以基本上我想这样做,因为现在我一直在获得配对的设备(我不知道这是否是获得它的最佳方式,但确实如此),然后当用户点击任何它连接到设备的人时。

类似于question,但很遗憾没有得到答复。

【问题讨论】:

    标签: android android-bluetooth


    【解决方案1】:

    这种格式不可能给你一个例子,所以我给你提供了 有一个很好的示例和有用的链接来帮助您理解示例。

    我建议您按照我提供的步骤进行操作,然后,当您完成 具体问题,你可以把它带到这里,使用你拥有的代码 sn-p 有困难。

    我建议您使用下载此示例代码:
    http://developer.android.com/samples/BluetoothChat/index.html

    如果您还没有,最好学习一下:
    http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html

    这是一个很好的教程,他们有很多教程:
    http://www.tutorialspoint.com/android/android_bluetooth.htm

    您需要在清单中获得以下权限:

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    

    这是一种建议使用的意图,用于检查是否启用了 BT:

    if (!mBluetoothAdapter.isEnabled()) {
        android.content.Intent enableIntent = new android.content.Intent(
               android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    }
    

    并使您的设备可被其他设备发现:

    if (mBluetoothAdapter.getScanMode() !=
            android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        android.content.Intent discoverableIntent =
            new android.content.Intent(
                android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(
            android.bluetooth.BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
                    300); // You are able to set how long it is discoverable.
        startActivity(discoverableIntent);
    }
    

    正如我在my answer here 中提到的:

    您可以避免使用意图搜索配对设备。什么时候 连接到未配对的设备,会弹出通知 要求配对设备。配对后,此消息不应显示 再次对于这些设备,连接应该是自动的(根据 了解您编写程序的方式)。

    我使用 Intent 来启用蓝牙,并制作我的设备 可发现,然后我设置我的代码进行连接,然后按一个按钮 连接。在您的情况下,您需要确保您的配件是 也可发现。在我的情况下,我使用唯一的 UUID,并且两个设备 必须认识到这一点才能连接。仅当您是 对两种设备进行编程,无论两者都是安卓还是一个安卓,并且 另一种设备类型。

    您需要了解如何使用套接字,这就是设备通信的方式。
    我建议研究这两个链接:

    http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html
    http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html

    套接字用于建立设备之间的连接。将有一个服务器套接字和设备套接字(由许多因素决定,程序员,实际设备)。服务器套接字监听传入的连接,当一个连接被接受时,设备连接,每个设备都有一个简单的套接字。

    我不确定您对线程了解多少。 连接需要用线程来管理:

    http://developer.android.com/guide/components/processes-and-threads.html
    http://android-developers.blogspot.com.au/2009/05/painless-threading.html

    设备之间的连接由独立于用户的线程管理 接口线程。这是为了防止手机在运行时锁定 建立、寻找和建立 BT 连接。

    例如:

    AcceptThread - 侦听连接并接受连接(通过服务器套接字)的线程。该线程可以运行较长时间以等待设备连接。

    ConnectThread - 连接到服务器的设备用来连接到 serversocket 的线程。

    ConnectedThread - 这是管理两个套接字之间连接的线程。

    如果这对你有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 2016-06-08
      • 2012-03-31
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 2018-05-26
      相关资源
      最近更新 更多