我正在尝试通过复选框找到一种简单的方法来在Android上切换设备蓝牙可发现性。打开它很简单,但如果再次关闭,我没有任何运气。我正在使用一种解决方法,将设备设置为可发现一秒钟(我在stackoverflow上找到的方法),但我需要找到一种正确的方法。
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
if (mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),
"No Bluetooth Support", Toast.LENGTH_SHORT).show();
} else {
if (mBluetoothAdapter.isEnabled()) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivity(discoverableIntent);
Toast.makeText(getApplicationContext(), "Device Discoverable", Toast.LENGTH_SHORT).show();
}
}
} else {
if (mBluetoothAdapter.isEnabled()) {
Intent disablediscoverableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
disablediscoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 1);
startActivity(disablediscoverableIntent);
Toast.makeText(getApplicationContext(),
"Discoverable Disabled", Toast.LENGTH_SHORT).show();
}
}