从pairDevice()获取响应

时间:2019-02-13 13:40:39

标签: java android

我配对了设备,并且希望在活动中得到此功能的响应

 private void pairDevice(BluetoothDevice device) {
        try {
            Method method = device.getClass().getMethod("createBond", (Class[]) null);
            method.invoke(device, (Object[]) null);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(DeviceListActivity.this, "Nie udalo się sparować urządzenia", Toast.LENGTH_LONG).show();
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要注册BroadcastReceiver。

BroadcastReceiver mBondStateBroadcastReceiver = new BroadcastReceiver() {
  public void onReceive(Context context, Intent intent) {
         if(intent.getAction() == BluetoothDevice.ACTION_BOND_STATE_CHANGED) {
              // do your stuff
         }
      }
    };

final IntentFilter bondFilter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mBondStateBroadcastReceiver, bondFilter);