Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.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 从适配器侦听时设置蓝牙身份验证PIN_Android_Android Bluetooth - Fatal编程技术网

Android 从适配器侦听时设置蓝牙身份验证PIN

Android 从适配器侦听时设置蓝牙身份验证PIN,android,android-bluetooth,Android,Android Bluetooth,我正在制作一个需要连接蓝牙设备并从中获取数据的应用程序。。。该设备被设置为主设备,因此我需要实现一个线程,在该线程中,我使用RFCOMMWITHSERVICERECORD监听并等待来自它的连接: public AcceptThread(Context context, String serverName, UUID myUUID) { this.context = context; BluetoothServerSocket tmp = null; Blueto

我正在制作一个需要连接蓝牙设备并从中获取数据的应用程序。。。该设备被设置为主设备,因此我需要实现一个
线程
,在该线程中,我
使用RFCOMMWITHSERVICERECORD
监听并等待来自它的连接:

    public AcceptThread(Context context, String serverName, UUID myUUID) {
    this.context = context;
    BluetoothServerSocket tmp = null;

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    try {

        // MY_UUID is the app's UUID string, also used by the client code
        tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(serverName, myUUID);
    } catch (IOException e) { }
    mmServerSocket = tmp;
}
然后在
run
上运行代码
socket=mmServerSocket.accept(5000)
等待它开始与设备配对:

    public void run() {
    BluetoothSocket socket = null;
    while (true) {
        try {
            socket = mmServerSocket.accept(5000);
        } catch (IOException e) {
            Log.e(TAG,"IOException: " + e);
        }

        // If a connection was accepted
        if (socket != null) {

            // Manage the connection
            ManageConnectedSocket manageConnectedSocket = new ManageConnectedSocket(socket);
            manageConnectedSocket.run();

            try {
                mmServerSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException: " + e);
            }
            break;
        }
    }
}
设备要求提供身份验证PIN,我需要一个自动程序。。。因为我想实现一个广播接收器来知道设备何时被要求与另一个设备保持一致:

IntentFilter filter = new IntentFilter(ACTION_PAIRING_REQUEST);
context.registerReceiver(mPairReceiver, filter);
并收到:

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_PAIRING_REQUEST.equals(action)) {
            Log.e(TAG,"ACTION_PAIRING_REQUEST");
            setBluetoothPairingPin(device);
        }
    }
};
在我的
setBluetoothPairingPin
方法中,我收到一个
BluetoothDevice
对象:

public void setBluetoothPairingPin(BluetoothDevice device) {
    byte[] pinBytes = convertPinToBytes("0000");
    try {
        Log.e(TAG, "Try to set the PIN");
        Method m = device.getClass().getMethod("setPin", byte[].class);
        m.invoke(device, pinBytes);
        Log.e(TAG, "Success to add the PIN.");
        try {
            device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, false);
            Log.e(TAG, "Success to setPairingConfirmation.");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    }

}
问题是,我不知道我的套接字何时接收到信息,也不知道什么是我的
蓝牙设备
,无法在连接之前设置配对Pin。。。 有人能帮我超越这个吗?或者,当我从
BluetoothServerSocket
进行监听时,是否有其他方法放置pin认证

如果我解释不正确,请让我知道

提前感谢

在和的帮助下,我能够为自己工作

我对
setBluetoothPairingPin
方法感到困惑,我不明白
操作配对请求实际上是在设备开始配对时调用的,也就是在用户请求PIN时调用的。。。因此调用
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备),并改变了一点设置配对方法,我设法使它工作

这是我的最终代码:

public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
    if (ACTION_PAIRING_REQUEST.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                String PIN = "0000";

                byte[] pin = new byte[0];
                try {
                    pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, PIN);
                    BluetoothDevice.class.getMethod("setPin", byte[].class).invoke(device, pin);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
    }
}

为什么不使用按钮配置而不是输入pin。您只需单击Pair按钮,而无需输入pin。这有用吗?嗨,谢谢你的回答。不是真的。。。我不控制其他设备的要求,它要求一个pin确认,它的设置有主。所以为了让我连接,我需要听设备,但我还需要在连接时插入一个pin码。。。有什么建议吗?