Bluetooth connect without pairing

后端 未结 1 765
我寻月下人不归
我寻月下人不归 2021-02-06 16:08

The normal way to connect to a bluetooth device is by pairing.

We need to connect to a device in an abnormal way: By using the Bluetooth MAC address only. We do not wa

1条回答
  •  有刺的猬
    2021-02-06 16:22

    It's definitely possible. You should have a look at the Android BluetoothChat demo app at: http://developer.android.com/samples/BluetoothChat/src/com.example.android.bluetoothchat/BluetoothChatService.html they use the insecure channel:

    public ConnectThread(BluetoothDevice device, boolean secure) {
        mmDevice = device;
        BluetoothSocket tmp = null;
        mSocketType = secure ? "Secure" : "Insecure";
    
        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            if (secure) {
                tmp = device.createRfcommSocketToServiceRecord(
                        MY_UUID_SECURE);
            } else {
                tmp = device.createInsecureRfcommSocketToServiceRecord(
                        MY_UUID_INSECURE);
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        }
        mmSocket = tmp;
    }
    

    on the client side. And on the server side:

      public AcceptThread(boolean secure) {
                BluetoothServerSocket tmp = null;
                mSocketType = secure ? "Secure" : "Insecure";
    
                // Create a new listening server socket
                try {
                    if (secure) {
                        tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
                                MY_UUID_SECURE);
                    } else {
                        tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
                                NAME_INSECURE, MY_UUID_INSECURE);
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
                }
                mmServerSocket = tmp;
            }
    

    Keep in mind that you need the Bluetooth MAC address for this. Google is trying to hide the address and make it unusable (to prevent tracking). You'll have problems getting your own MAC address and the MAC addresses of remote devices are randomised and change after some time: http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id

    0 讨论(0)
提交回复
热议问题