0

再次需要你的帮助:

我想建立与 obd2-bluetooth-adapter 的连接。出于这个原因,我查看了 AndroidSDK 中的 BluetoothChat-Example。我能够与我的计算机建立连接,但我无法将我的 android 平板电脑与我的 odb2-bluetooth-adapter (elm327) 配对。发现了一些提示,例如:

    myRemoteBluetoothDevice.setPassKey(....); 

首先,我无法在“myRemoteBluetoothDevice”上使用该功能 - 然后我不知道在哪里使用该功能。在连接线程内?

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

我认为一个可能的解决方案是实现一个事件监听器或类似的东西,当远程设备需要密码时调用它?但是我必须在哪里实施它?我必须在哪里使用它?有人可以帮我吗?

提前致谢 !!!

PS:我的应用程序基于以下示例: https ://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7b​​dc0dfa1a79eaf009ad178fe/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java

问候。

编辑 :

试图实现第一个答案:

我的广播接收器:

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

还有我的连接方法,我在其中注册了 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);
    }
}

非常感谢您的帮助!提前致谢。

没有人有提示吗??

4

1 回答 1

1

注册一个广播监听器:android.bluetooth.device.action.PAIRING_REQUEST。

在收到:

    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);
于 2013-11-04T20:20:30.273 回答