蓝牙连接无需配对 [英] Bluetooth connect without pairing

查看:769
本文介绍了蓝牙连接无需配对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

We need to connect to a device in an abnormal way: By using the Bluetooth MAC address only. We do not want to be prompted for a PIN.

我们知道该设备支持此技术,但是我们找不到在Android上实现此功能的方法.

We know the device supports this technique, but we cannot find a way to do it on 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.

The problem is that upon socket.connect() we are prompted for a PIN. The PIN is not necessary for this device, so we do not want to be prompted for a PIN.

我们不需要PIN的原因:

The reason we know the PIN is not necessary:

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

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

How can we modify the code such that it will not prompt for a PIN?

我们像这样测试了方法createInsecureRfcommSocketToServiceRecord:

We tested method createInsecureRfcommSocketToServiceRecord like this:

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

然后我们会收到这样的错误:

Then we get error like this:

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

推荐答案

这绝对有可能.您应该在以下位置查看Android BluetoothChat演示应用程序:

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

请记住,您需要此蓝牙MAC地址. Google试图隐藏该地址并使其不可用(以防止跟踪).在获取自己的MAC地址时,您将遇到问题,并且远程设备的MAC地址会随机化并在一段时间后更改:

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

这篇关于蓝牙连接无需配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆