Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pair Bluetooth device programmatically Android

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

like image 256
Sandip Jadhav Avatar asked Jun 18 '13 11:06

Sandip Jadhav


People also ask

How get data from Bluetooth in Android programmatically?

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[]) .

How do you Unpair Bluetooth device programmatically in Android?

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.


2 Answers

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.

like image 115
Mats Willemsen Avatar answered Sep 20 '22 06:09

Mats Willemsen


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> 
like image 32
Rodolfo Abarca Avatar answered Sep 20 '22 06:09

Rodolfo Abarca