简体   繁体   English

如何与Android中的蓝牙设备配对?

[英]How to pair with bluetooth device in android?

Hello Everyone I am working on Bluetooth and I want to pair my device with the finded bluetooth and connect with the paired bluetooth. 大家好,我正在蓝牙上工作,我想将设备与找到的蓝牙配对并与配对的蓝牙连接。

I want to know how to do this. 我想知道怎么做。 And I have also read about client server approach in which we use bluetoothserver socket and bluetooth socket and listenUsingRfcommWithServiceRecord and createRfcommSocketToServiceRecord methods in which we pass mac and uuid. 我还阅读了有关客户端服务器方法的信息,其中我们使用了蓝牙服务器套接字和蓝牙套接字,以及使用了mac和uuid的listenUsingRfcommWithServiceRecord和listenRscomingRfcommSocketToServiceRecord方法。

I want to know where we use this approach and how to find the remote device UUId. 我想知道我们在哪里使用这种方法以及如何找到远程设备UUId。 Thanks in advance. 提前致谢。

You don't need to use the mentioned methods, at least for pairing the devices. 您不需要使用上述方法,至少不需要配对设备。

Try using Intents to pair. 尝试使用Intent进行配对。 This code might let you get more familiar with Bluetooth. 此代码可以使您更加熟悉蓝牙。

    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);

If you want a better approach on how to connect these paired devices, take a look at Bluetooth on AndroidDevelopers: 如果您想要更好的方法来连接这些配对的设备,请查看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) { }
    }
}

Link resources: 链接资源:

http://pastebin.com/N8dR4Aa1 http://pastebin.com/N8dR4Aa1

http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient

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

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