Note that there are some explanatory texts on larger screens.

plurals
  1. PODefining Passkey for Android Bluetooth Connection
    text
    copied!<p>need your help again : </p> <p>I want to establish a connection to a obd2-bluetooth-adapter. For that reason i had a look at the BluetoothChat-Example from the AndroidSDK. 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). Found some hints, for instance : </p> <pre><code> myRemoteBluetoothDevice.setPassKey(....); </code></pre> <p>First, i can not use the function on 'myRemoteBluetoothDevice' - and then i don't know where to use this function. Within the connect-Thread ? </p> <pre><code> 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); } </code></pre> <p>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 ? </p> <p>Thanks in advance !!! </p> <p>PS : My App is based on the following example : <a href="https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java" rel="nofollow">https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java</a></p> <h2>Greetings.</h2> <h2>EDIT :</h2> <p>Tried to implement the first answer : </p> <p>My BroadcastReceiver : </p> <pre><code> private final BroadcastReceiver mReceiverRequiresPin = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent){ try { BluetoothDevice newDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Class&lt;?&gt; 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(); } } }; </code></pre> <p>And my connect-method, where i register the broadcastReceiver : </p> <pre><code> 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); } } </code></pre> <p>Would really appreciate your help ! Thanks in advance. </p> <p>No one has a hint ??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload