I am developing an application where I want to connect a Bluetooth device main issue is I don't want user to enter required pin instead application should do that by himself...I don't have any connection related issue...Only want to insert and complete pin authentication process by application itself.
I found following code I am sure it is working but not sure on how to add pin in this code??
private void pairDevice(BluetoothDevice device) { try { Log.d("pairDevice()", "Start Pairing..."); Method m = device.getClass().getMethod("createBond", (Class[]) null); m.invoke(device, (Object[]) null); Log.d("pairDevice()", "Pairing finished."); } catch (Exception e) { Log.e("pairDevice()", e.getMessage()); } }
Does anyone know how to enter pin in above code or any similar code to solve problem.. Thank You
Using the BluetoothSocket , the general procedure to transfer data is as follows: Get the InputStream and OutputStream that handle transmissions through the socket using getInputStream() and getOutputStream() , respectively. Read and write data to the streams using read(byte[]) and write(byte[]) .
Using Android Bluetooth API, we can use createBond method to pair with a device or removeBond to unpair. This is an asynchronous call so that it will return immediately. To catch the pairing process, we have to register a BroadcastReceiver with ACTION_BOND_STATE_CHANGED intent to catch the process.
How can I avoid or dismiss Android's Bluetooth pairing notification when I am doing programmatic pairing?
This seems to give you the answer, with the pin entering and all. It involves sending .setPin() whenever you get the message.
So, I had this question, if someone needs the answer to this working in android 4.4.2.
IntentFilter filter = new IntentFilter( "android.bluetooth.device.action.PAIRING_REQUEST"); /* * Registering a new BTBroadcast receiver from the Main Activity context * with pairing request event */ registerReceiver( new PairingRequest(), filter);
And the code for the Receiver.
public static class PairingRequest extends BroadcastReceiver { public PairingRequest() { super(); } @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) { try { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0); //the pin in case you need to accept for an specific pin Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0)); //maybe you look for a name or address Log.d("Bonded", device.getName()); byte[] pinBytes; pinBytes = (""+pin).getBytes("UTF-8"); device.setPin(pinBytes); //setPairing confirmation if neeeded device.setPairingConfirmation(true); } catch (Exception e) { e.printStackTrace(); } } } }
And in the manifest file.
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
And the broadcastReceiver.
<receiver android:name=".MainActivity$PairingRequest"> <intent-filter> <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" /> </intent-filter> </receiver>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With