Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否可以自动接受蓝牙配对?_Android_Bluetooth_Pairing - Fatal编程技术网

Android 是否可以自动接受蓝牙配对?

Android 是否可以自动接受蓝牙配对?,android,bluetooth,pairing,Android,Bluetooth,Pairing,在Android 2.3.3 BluetoothChat示例中,使用CreateInsurerFComSocketToServiceRecord()API,用户仍然会被提示接受配对请求,即使没有显示PIN码 有没有一种方法可以在无需用户干预的情况下自动执行蓝牙配对请求?或者,出于安全考虑,这永远不可能实现?我已经在网上找了两天了,还没有找到什么,所以如果有人知道,请发帖子 谢谢 不适用于标准API,否:如果MAC地址不在配对数据库中,则始终会出现提示。有人告诉我,如果您的设备已植根,并且对blu

在Android 2.3.3 BluetoothChat示例中,使用CreateInsurerFComSocketToServiceRecord()API,用户仍然会被提示接受配对请求,即使没有显示PIN码

有没有一种方法可以在无需用户干预的情况下自动执行蓝牙配对请求?或者,出于安全考虑,这永远不可能实现?我已经在网上找了两天了,还没有找到什么,所以如果有人知道,请发帖子


谢谢

不适用于标准API,否:如果MAC地址不在配对数据库中,则始终会出现提示。有人告诉我,如果您的设备已植根,并且对bluetooth服务的DBus端点具有公共读/写访问权限,您可以解决此问题,但我从未见过实际实现此功能。

我遇到了相同的问题,我希望以下代码将有所帮助: 首先,我们需要:


其次,我们需要BluetoothDevice类,并且:

public class PairingRequest extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("ACTION_PAIRING_REQUEST")) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
        device.setPin(pinBytes);
    }
 }
}

所以,我有一个提示,如果有人需要在安卓4.4.2中工作的答案

 IntentFilter filter = new IntentFilter(
                "android.bluetooth.device.action.PAIRING_REQUEST");


        /*
         * Registering a new BTBroadcast receiver from the Main Activity context
         * with pairing request event
         */
        registerReceiver(
                new PairingRequest(), filter);
和接收器的代码

  public static class PairingRequest extends BroadcastReceiver {
        public PairingRequest() {
            super();
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
                try {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
                    //the pin in case you need to accept for an specific pin
                    Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));
                    //maybe you look for a name or address
                    Log.d("Bonded", device.getName());
                    byte[] pinBytes;
                    pinBytes = (""+pin).getBytes("UTF-8");
                    device.setPin(pinBytes);
                    //setPairing confirmation if neeeded
                    device.setPairingConfirmation(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
和在清单文件中

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

广播接收机呢

 <receiver android:name=".MainActivity$PairingRequest">
                <intent-filter>
                    <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
                    <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
                </intent-filter>
</receiver>


谢谢!这解释了很多。我甚至查看了BluetoothAdapter.java类的源代码,看看是否有任何非官方的API可以利用。那么,是否可以提前将MAC地址添加到配对数据库中,以便在设备处于范围内时自动进行配对?我不知道:这是一个BlueZ级别的问题,因此您可能会在android BlueZ git中找到更多运气。您使用的安卓版本是什么?尽管有grepcode,请检查此项,这两种方法在标准的Android发行版中都不存在,也不存在。虽然这当然很好,但它不存在。这段代码非常有用。只要使用一点反射,小心隐藏的字符串常量,你就会没事了。绝对有可能注入PIN并连接,但目标设备仍然未配对。好吧,我。。。我可以提供一个在我自己的项目中使用的完整源代码。当使用上述算法时,Android设备第一次连接到目标时,它确实连接了,但仍然显示PIN对话框。按下按钮将保持连接完整!目标永远不会配对。下次设备连接时,将不再显示该对话框。要么是种族问题,要么是安卓内部问题。通过2.3.5和4.1.2机器人验证。