如何以编程方式将Android设备与蓝牙设备配对

我正在开发一个应用程序,我想连接一个蓝牙设备的主要问题是我不希望用户input所需的引脚而不是应用程序应该自己做…我没有任何连接相关的问题…只想插入并完成由应用程序本身的引脚authentication过程。

我发现下面的代码,我相信它是工作,但不知道如何添加在此代码中的PIN?

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

有谁知道如何inputpin在上面的代码或任何类似的代码来解决问题..谢谢你

Solutions Collecting From Web of "如何以编程方式将Android设备与蓝牙设备配对"

当我正在进行程序配对时,如何避免或解除Android的蓝牙配对通知?

这似乎给你的答案,随着引脚进入和所有。 它涉及每当你得到消息时发送.setPin()。

所以,我有这个问题,如果有人需要在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); 

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

并在清单文件中。

 <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

和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> 

试试这个代码:

 public void pairDevice(BluetoothDevice device) { String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST"; Intent intent = new Intent(ACTION_PAIRING_REQUEST); String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE"; intent.putExtra(EXTRA_DEVICE, device); String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT"; int PAIRING_VARIANT_PIN = 0; intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST); intent.putExtra(EXTRA_DEVICE, device); int PAIRING_VARIANT_PIN = 272; intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN); sendBroadcast(intent); Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); startActivityForResult(intent, REQUEST_PAIR_DEVICE); 

我希望这有帮助

参考: http : //pastebin.com/N8dR4Aa1

尝试这个,

 BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE"); device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device); 
  BluetoothSocket bluetoothSocket = null; try { bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(UUID_DIVING)); } catch (IOException e) { Log.i("Bluetooth", "IOException = " + e.getMessage()); e.printStackTrace(); } try { byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "0000"); Method m = device.getClass().getMethod("setPin", byte[].class); m.invoke(device, (Object) pin); device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Log.i("Bluetooth", "IOException = " + e.getMessage()); e.printStackTrace(); } try { if (bluetoothSocket != null) { bluetoothSocket.connect(); Log.i("Bluetooth", "bluetoothSocket.connect() "); InputStream inputStream = bluetoothSocket.getInputStream(); OutputStream outputStream = bluetoothSocket.getOutputStream(); } } catch (IOException e) { e.printStackTrace(); }