android – 蓝牙连接没有配对

连接蓝牙设备的常用方法是配对.

我们需要以异常方式连接到设备:仅使用蓝牙MAC地址.我们不希望系统提示您输入PIN码.

我们知道该设备支持这种技术,但我们无法在Android上找到这种方法.

缩写代码如下所示:

  String mac_address = “00:11:22:33:44:55”
  BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

  BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(mac_address);

  Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", 
           new Class[] {int.class});
  BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);

  socket.connect();

问题是在socket.connect()上我们被提示输入PIN.此设备不需要PIN,因此我们不希望系统提示输入PIN.

我们知道PIN的原因不是必要的:

>我们制造此设备并编写固件.
>我们可以使用C#编写的Windows程序连接到它.

我们如何修改代码,使其不会提示输入PIN码?

[编辑以测试评论中的建议]

我们测试了方法createInsecureRfcommSocketToServiceRecord,如下所示:

ParcelUuid list[] = device.getUuids();
BluetoothSocket  socket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(list[0].getUuid());

然后我们得到这样的错误:

I/InputDispatcher(  382): Delivering touch to current input target: action: 0x1
I/InputDispatcher(  382): Delivering touch to current input target: action: 0x1
V/BluetoothSocket.cpp(16956): initSocketNative
V/BluetoothSocket.cpp(16956): ...fd 66 created (RFCOMM, lm = 0)
V/BluetoothSocket.cpp(16956): initSocketFromFdNative
I/BluetoothPolicyService(  382): getBluetoothDataTransferAllowed
D/BluetoothUtils(16956): isSocketAllowedBySecurityPolicy start : device null
V/BluetoothService.cpp(  382): createDeviceNative
V/BluetoothService.cpp(  382): ... address =
V/BluetoothEventLoop.cpp(  382): onCreateDeviceResult
V/BluetoothEventLoop.cpp(  382): onCreateDeviceResult
E/BluetoothEventLoop.cpp(  382): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)
D/BluetoothEventLoop(  382): Result of onCreateDeviceResult:1
V/BluetoothService.cpp(  382): discoverServicesNative
V/BluetoothService.cpp(  382): ... Object Path = /org/bluez/1100/hci0/dev_00_11_22_33_44_55
V/BluetoothService.cpp(  382): ... Pattern = , strlen = 0
D/FStest  (  633): check default
D/FStest  (  633): check default

最佳答案 这绝对是可能的.您应该在
http://developer.android.com/samples/BluetoothChat/src/com.example.android.bluetoothchat/BluetoothChatService.html查看Android BluetoothChat演示应用程序

他们使用不安全的渠道:

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

在客户端.在服务器端:

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

请记住,您需要蓝牙MAC地址.谷歌试图隐藏地址并使其无法使用(以防止跟踪).您在获取自己的MAC地址时遇到问题,远程设备的MAC地址会随机化,并在一段时间后发生变化:http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id

点赞