如何在android中与蓝牙设备配对?

如何在android中与蓝牙设备配对?,android,android-bluetooth,Android,Android Bluetooth,大家好,我正在使用蓝牙,我想将我的设备和Find Bluetooth配对,并使用配对的Bluetooth连接 我想知道怎么做。我还读过关于客户机-服务器方法的书,其中我们使用bluetooth服务器套接字和bluetooth套接字,并使用RfcommWithServiceRecord和createRfcommSocketToServiceRecord方法来传递mac和uuid 我想知道我们在哪里使用这种方法,以及如何找到远程设备UUId。提前感谢。您不需要使用上述方法,至少对于配对设备而言 尝试

大家好,我正在使用蓝牙,我想将我的设备和Find Bluetooth配对,并使用配对的Bluetooth连接

我想知道怎么做。我还读过关于客户机-服务器方法的书,其中我们使用bluetooth服务器套接字和bluetooth套接字,并使用RfcommWithServiceRecord和createRfcommSocketToServiceRecord方法来传递mac和uuid


我想知道我们在哪里使用这种方法,以及如何找到远程设备UUId。提前感谢。

您不需要使用上述方法,至少对于配对设备而言

尝试使用意图配对。此代码可能会让您更熟悉蓝牙

    public void pairDevice(BluetoothDevice device)
{
    String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
    Intent intent = new Intent(ACTION_PAIRING_REQUEST);
    String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
    intent.putExtra(EXTRA_DEVICE, device);
    String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
    int PAIRING_VARIANT_PIN = 0;
    intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
intent.putExtra(EXTRA_DEVICE, device);
int PAIRING_VARIANT_PIN = 272;
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
sendBroadcast(intent);

Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivityForResult(intent, REQUEST_PAIR_DEVICE);
如果您想要更好地连接这些配对设备,请查看AndroidDevelopers上的蓝牙:

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) { }
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it will slow down the connection
        mBluetoothAdapter.cancelDiscovery();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }

        // Do work to manage the connection (in a separate thread)
        manageConnectedSocket(mmSocket);
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}
链接资源:


感谢Machado,但在配对了如何连接该设备以及我们在哪里使用这两种方法(即客户机-服务器方法)之后,Google开发者网页上有一些有用的文档和源代码示例。请检查编辑后的答案。我已经读出了它们,但无法找到使用它们的原因,请告诉我。RFCOMM BluetoothSocket是一种使用UUID的SDP查找启动到远程设备的安全传出连接的方法。它设计用于对等蓝牙应用程序的listenUsingRfcommWithServiceRecord(字符串,UUID)。远程设备将进行身份验证,此套接字上的通信将被加密。仅当可以使用经过身份验证的套接字链接时,才应使用此套接字。身份验证是指对链接密钥进行身份验证,以防止中间人类型的攻击。