首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为Android蓝牙连接定义Passkey

为Android蓝牙连接定义Passkey
EN

Stack Overflow用户
提问于 2013-11-05 02:33:11
回答 1查看 5K关注 0票数 0

再次需要您的帮助:

我想建立到OBD2蓝牙适配器的连接。出于这个原因,我看了一下AndroidSDK中的BluetoothChat示例。我可以与我的电脑建立连接,但我无法将我的android平板电脑与我的odb2蓝牙适配器(elm327)配对。找到一些提示,例如:

代码语言:javascript
复制
    myRemoteBluetoothDevice.setPassKey(....); 

首先,我不能在'myRemoteBluetoothDevice‘上使用这个函数--然后我不知道在哪里使用这个函数。在connect-Thread中?

代码语言:javascript
复制
    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/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java

欢迎光临。

编辑:

尝试实现第一个答案:

我的BroadcastReceiver:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
    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);
    }
}

会非常感谢你的帮助!提前谢谢。

没有人有提示??

EN

回答 1

Stack Overflow用户

发布于 2013-11-05 04:20:30

为: android.bluetooth.device.action.PAIRING_REQUEST.注册广播监听程序

在接收到的前缀中:

代码语言:javascript
复制
    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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19774615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档