gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:48:52 29 4
gpt4 key购买 nike

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

如果我点击拒绝,那么 RESULT_CANCELED正确获取,而如果我单击允许,结果代码为 120,因此它不是 RESULT_OK我无法启动 Activity ,它是120而不是RESULT_OK是正常的?

我的 Activity

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 的值与可发现持续时间相同 EXTRA_DISCOVERABLE_DURATION通过 Intent。
默认持续时间为 120。所以没关系。

如果你要开始这样的 Activity

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

它将返回 240 作为结果代码。

关于android - onActivityResult() 启用蓝牙和发现后的错误结果代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56701666/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com