没有用户的Android蓝牙配对使用Android API输入Pin和Confirmation

时间:2016-02-20 05:00:11

标签: android bluetooth android-bluetooth pairing

我是Android编程的初学者,因为我3个月前刚刚开始。我正在做一个使用蓝牙将Android应用程序连接到arduino的项目。我已经有了Android应用程序的代码(bluetooth.adapter,socket,.etc。)。连接代码已经正常工作。其中一个目标是Android应用程序在与蓝牙设备配对时自动输入密码,而无需用户输入PIN。

此论坛上的旧帖子没有多大帮助。 (许多建议使用不安全模式,但我确实需要安全模式,在我的情况下,arduino是服务器,而手机应用程序是客户端,因此createInsecureRfcommSocketToServiceRecord()服务器方法对我不起作用)

我在android开发者网站上搜索并发现了这个关于bluetoothdevice类:

setPairingConfirmation(布尔确认) 确认PAIRING_VARIANT_PASSKEY_CONFIRMATION配对的密码。

PAIRING_VARIANT_PIN ="将提示用户输入图钉或应用程序将为用户输入图钉"。

PAIRING_VARIANT_PASSKEY_CONFIRMATION ="系统将提示用户确认屏幕上显示的密钥,或者应用程序将确认用户的密码"

似乎使用代码,应用程序将是输入密码和确认的人 密码使其成为"自动连接"功能,但Android网站没有给出如何使用它的示例代码。你们有没有使用这个或相关过程的示例代码?感谢您的帮助!

3 个答案:

答案 0 :(得分:12)

首先要澄清的是,此解决方案是针对较新版本的API(15或更高版本?)

而设计的

我在另一篇文章中找到了答案(见Here中的Roldofo答案)。这是我重新组织的答案和详细的代码。

简而言之,您需要设置一个广播接收器来捕获ACTION_PAIRING_REQUEST,然后以编程方式传递PIN并确认。

注册广播接收器:

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    getActivity().registerReceiver(mPairingRequestReceiver, filter);

接收者的定义:

private final BroadcastReceiver mPairingRequestReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
            try {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 1234);
                    //the pin in case you need to accept for an specific pin
                    Log.d(TAG, "Start Auto Pairing. PIN = " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",1234));
                    byte[] pinBytes;
                    pinBytes = (""+pin).getBytes("UTF-8");
                    device.setPin(pinBytes);
                    //setPairing confirmation if neeeded
                    device.setPairingConfirmation(true);
            } catch (Exception e) {
                Log.e(TAG, "Error occurs when trying to auto pair");
                e.printStackTrace();
            }
        }
    }
};

然后在您的活动或片段(您想要启动配对的任何地方),您可以调用以下定义的pairDevice()方法来调用配对尝试(将生成ACTION_PAIRING_REQUEST)

private void pairDevice(BluetoothDevice device) {
    try {
        Log.d(TAG, "Start Pairing... with: " + device.getName());
        device.createBond();
        Log.d(TAG, "Pairing finished.");
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

答案 1 :(得分:1)

我也面临同样的问题,经过全部研究,我找到了以下解决方案。

(经过测试和工作!!!)

我基本上是在寻找一个特定的蓝牙设备(我知道MAC地址),并在找到后与它配对。首先要做的是使用boradcast接收器创建配对请求,并按如下方式处理请求。

IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
                intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
                registerReceiver(broadCastReceiver,intentFilter);

您需要编写broadcastReceiver并按以下方式处理它。

String BLE_PIN = "1234"
private BroadcastReceiver broadCastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action))
        {
            BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            bluetoothDevice.setPin(BLE_PIN.getBytes());
            Log.e(TAG,"Auto-entering pin: " + BLE_PIN);
            bluetoothDevice.createBond();
            Log.e(TAG,"pin entered and request sent...");
        }
    }
};

瞧!您应该能够在没有任何手动干预的情况下配对蓝牙设备。

希望这会有所帮助:-)如果适合您,请正确回答。

答案 2 :(得分:1)

可以通过代码做到这一点

在您的主要活动中,添加以下代码

BluetoothReceiver myreceiver = new BluetoothReceiver();
var intentfilterparingrequest = new IntentFilter(BluetoothDevice.ActionPairingRequest);
RegisterReceiver(myreceiver, intentfilterparingrequest);

在您的广播接收器中,编写以下代码(如果未创建新的广播接收器)

public class BluetoothReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        string BLE_PIN = "0000";
        var action = intent.Action;
        switch (action)
        {
            case BluetoothDevice.ActionPairingRequest:
                BluetoothDevice bluetoothDevice = 
              (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
                bluetoothDevice.SetPin(Encoding.ASCII.GetBytes(BLE_PIN));
                bluetoothDevice.CreateBond();
                break;
        }
    } 
}