onActivityResult() 启用蓝牙和发现后的错误结果代码

Wrong result code after onActivityResult() to enable bluetooth and discovery

我有一个 activity 要求启用蓝牙和发现模式。 请求被正确执行并被 onactivityresult() 接受。 问题出在发现请求中。

如果我点击拒绝然后 RESULT_CANCELED 被正确接受,而如果我点击允许,结果代码是 120 因此它不是 RESULT_OK 我无法启动 activity,正常是120不是RESULT_OK?

我的活动

public class TransitionActivity extends AppCompatActivity {
    private static final int ENABLE_REQUEST = 0;
    private static final int DISCOVER_REQUEST = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transition);

        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(bluetoothAdapter == null){ //bluetooth not supported
            Toast.makeText(this, "Bluetooth not supported.", Toast.LENGTH_SHORT).show();
            finish();
        }

        if(!bluetoothAdapter.isEnabled()){
            Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(i,ENABLE_REQUEST);

        }
        if(!bluetoothAdapter.isDiscovering()){
            Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            startActivityForResult(i,DISCOVER_REQUEST);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == ENABLE_REQUEST){
            if(resultCode == RESULT_OK){
            }
            if(resultCode == RESULT_CANCELED){
                Toast.makeText(this, "You need to enable the bluetooth.", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
        if(requestCode == DISCOVER_REQUEST){
            System.out.println("RESULT CODE" + resultCode); //it is 120
            if(resultCode ==  RESULT_OK){ //skipped
                Intent secondActivity = new Intent(this, com.project.secondActivity.class);
                this.startActivity(secondActivity);
            }
            if(resultCode == RESULT_CANCELED){
                finish();
            }
        }
    }
}

resultCode 的值与 Intent 中传递的可发现持续时间 EXTRA_DISCOVERABLE_DURATION 相同。 并且默认时长是120。所以没问题。

如果你会这样开始activity

Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 240);
startActivityForResult(i,DISCOVER_REQUEST);

它将return 240 作为结果代码。