简体   繁体   English

为Android蓝牙连接定义密码

[英]Defining Passkey for Android Bluetooth Connection

need your help again : 再次需要您的帮助:

I want to establish a connection to a obd2-bluetooth-adapter. 我想与obd2-bluetooth-adapter建立连接。 For that reason i had a look at the BluetoothChat-Example from the AndroidSDK. 因此,我看了AndroidSDK中的BluetoothChat-Example。 I am able to establish a connection to my computer, but i am not able to pair my android tablet with my odb2-bluetooth-adapter (elm327). 我能够与计算机建立连接,但无法将android平板电脑与odb2-bluetooth-adapter(elm327)配对。 Found some hints, for instance : 找到了一些提示,例如:

    myRemoteBluetoothDevice.setPassKey(....); 

First, i can not use the function on 'myRemoteBluetoothDevice' - and then i don't know where to use this function. 首先,我无法在“ myRemoteBluetoothDevice”上使用该功能-然后我不知道在哪里使用此功能。 Within the connect-Thread ? 在连接线程内?

    public synchronized void connect(BluetoothDevice device, boolean secure) {
      if (D) Log.d(TAG, "connect to: " + device);

      // Cancel any thread attempting to make a connection
      if (mState == STATE_CONNECTING) {
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
      }

      // Cancel any thread currently running a connection
      if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

      // Start the thread to connect with the given device
      mConnectThread = new ConnectThread(device, secure);
      mConnectThread.start();
      setState(STATE_CONNECTING);
}

I think a possible solution would be to implement a event-listener or something like this, which is called when the remote device needs a passcode ? 我认为可能的解决方案是实现事件侦听器或类似的方法,当远程设备需要密码时调用该方法? But where i have to implement it ? 但是我必须在哪里实施呢? And where i have to use it ? 我必须在哪里使用它? Can somebody help me out there ? 有人可以帮我吗?

Thanks in advance !!! 提前致谢 !!!

PS : My App is based on the following example : https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java PS:“我的应用”基于以下示例: https : //android.googlesource.com/platform/development/+/25b6aed7b2e01ce7b​​dc0dfa1a79eaf009ad178fe/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java

Greetings. 问候。

EDIT : 编辑:

Tried to implement the first answer : 试图实施第一个答案:

My BroadcastReceiver : 我的BroadcastReceiver:

    private final BroadcastReceiver mReceiverRequiresPin = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent){

        try {
            BluetoothDevice newDevice =    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Class<?> btDeviceInstance =  Class.forName(BluetoothDevice.class.getCanonicalName());

            Method convert = btDeviceInstance.getMethod("convertPinToBytes", String.class);

            byte[] pin = (byte[]) convert.invoke(newDevice, "1234");

            Method setPin = btDeviceInstance.getMethod("setPin", byte[].class);
            boolean success = (Boolean) setPin.invoke(newDevice, pin);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
};

And my connect-method, where i register the broadcastReceiver : 还有我的连接方法,我在其中注册broadcastReceiver:

    private void connect(CordovaArgs args, boolean secure,
    CallbackContext callbackContext) throws JSONException {

      final String actionPinRequested = "android.bluetooth.device.action.PAIRING_REQUEST";
    IntentFilter intentFilterPinRequested = new IntentFilter(actionPinRequested);

      cordova.getActivity().registerReceiver(mReceiverRequiresPin, intentFilterPinRequested);
    String macAddress = args.getString(0);
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress);

    if (device != null) {
        connectCallback = callbackContext;
        bluetoothSerialService.connect(device, secure);

        PluginResult result = new PluginResult(
                PluginResult.Status.NO_RESULT);
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);

    } else {
        callbackContext.error("Could not connect to " + macAddress);
    }
}

Would really appreciate your help ! 非常感谢您的帮助! Thanks in advance. 提前致谢。

No one has a hint ?? 没有人有暗示吗?

Register a broadcast listener for: android.bluetooth.device.action.PAIRING_REQUEST. 注册以下广播侦听器:android.bluetooth.device.action.PAIRING_REQUEST。

In the onrecieve: 在一次:

    BluetoothDevice newDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Class<?> btDeviceInstance =  Class.forName(BluetoothDevice.class.getCanonicalName());

    Method convert = btDeviceInstance.getMethod("convertPinToBytes", String.class);

    byte[] pin = (byte[]) convert.invoke(newDevice, "1234");

    Method setPin = btDeviceInstance.getMethod("setPin", byte[].class);
    success = (Boolean) setPin.invoke(newDevice, pin);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM