与BLE的蓝牙配对在Android中不起作用

时间:2019-06-12 05:35:10

标签: android bluetooth-lowenergy

我正在尝试将我的Android设备与BLE设备配对。问题是当我要求配对请求活动时,出现对​​话框。但是,当我输入密码时,它没有配对或未onActivityResult被调用。那么如何成功配对?

   private void startBluetoothPairing(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);
    ((Activity) appContext).startActivityForResult(intent,1);
   }

OnActivityResult没有被调用。

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.v("TAG","Bluetooth Device!!");
    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            BluetoothDevice bluetoothDevice = data.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Parcel parcel = Parcel.obtain();
            data.getParcelableExtra(BluetoothDevice.EXTRA_PAIRING_KEY).writeToParcel(parcel, 0);
            byte[] bytes = parcel.marshall();
            parcel.recycle();
            bluetoothDevice.setPin(bytes);
            bluetoothDevice.createBond();
        }
    }
}

问题已解决:  更新后的代码:

在应用程序启动期间注册了broadCasterReciever

    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
    appContext.getApplicationContext().registerReceiver(broadCastReceiver,intentFilter);

广播接收器的实现。

    private  String BLE_PIN= "000012";
    private BroadcastReceiver broadCastReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action))
                {
                    BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    bluetoothDevice.setPin(BLE_PIN.getBytes());
                    Log.e("TAG","Auto-entering pin: " + BLE_PIN);

                }
           }
      };

发现设备后,我打电话给 device.createBond()

1 个答案:

答案 0 :(得分:1)

这是我使用的,对我来说效果很好。

与设备配对

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
                    BluetoothActivity.this.registerReceiver(mReceiver, filter);
                    device.createBond();

BroadcastReceiver检查设备是否配对

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                String address = device.getAddress();
                if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                    //Device Paired
                }
            }
        }
    };

在onDestroy中取消注册接收器

BluetoothActivity.this.unregisterReceiver(mReceiver);