Я использую BluetoothAdapter.getScanMode()
, чтобы проверить, установлено ли Bluetooth Bluetooth для обнаружения. Однако, если bluetooth выключен, тогда getScanMode()
возвращает SCAN_MODE_NONE
, который не говорит мне, является ли это доступным или нет. Как проверить, доступна ли Bluetooth, даже когда она выключена?Проверьте, доступна ли Bluetooth Bluetooth при отключении Bluetooth?
1
A
ответ
0
Чтобы определить, является ли Bluetooth обнаруженным, я бы проверил текущее состояние getState() на адаптере.
ГлавнаяActivity getState();
public class MainActivity extends AppCompatActivity {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!BluetoothConnection.canDeviceConnectViaBluetooth(bluetoothAdapter, this)) {
Toast.makeText(this, "Not Compatible", Toast.LENGTH_SHORT).show();
} else {
if (BluetoothConnection.isBluetoothAdapterEnabled(bluetoothAdapter, this)) {
int isBlueToothDiscoverable = bluetoothAdapter.getState();
switch (isBlueToothDiscoverable) {
case BluetoothAdapter.STATE_OFF:
Toast.makeText(this, "STATE_OFF", Toast.LENGTH_SHORT).show(); // Not discoverable
break;
case BluetoothAdapter.STATE_TURNING_ON:
Toast.makeText(this, "STATE_TURNING_ON", Toast.LENGTH_SHORT).show(); // Maybe discoverable
break;
case BluetoothAdapter.STATE_ON:
Toast.makeText(this, "STATE_ON", Toast.LENGTH_SHORT).show(); // Discoverable
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Toast.makeText(this, "STATE_TURNING_OFF", Toast.LENGTH_SHORT).show(); // Not discoverable
break;
default:
break;
}
}
}
}
}
Проверьте, если устройство Bluetooth Совместимость
public class BluetoothConnection {
public final static int REQUEST_ENABLE_BT = 1;
public static boolean canDeviceConnectViaBluetooth(BluetoothAdapter bluetoothAdapter, Activity activity) {
if (bluetoothAdapter == null) {
Log.e("DEVICE_COMPATIBLE", "FALSE");
return false;
}
return true;
}
public static boolean isBluetoothAdapterEnabled(BluetoothAdapter bluetoothAdapter, Activity activity) {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Log.e("IS_BLUETOOTH_ON", "FALSE");
return false;
} else {
return true;
}
}
}
Смежные вопросы
- 1. Ошибка приложения при отключении Bluetooth
- 2. Получить сигнал Bluetooth при отключении Bluetooth в устройстве
- 3. Сбой при подключении Bluetooth Bluetooth
- 4. Мониторинг зоны маяка iOS при отключении Bluetooth
- 5. Является ли Bluetooth периферийным IP-телефоном Bluetooth Bluetooth?
- 6. Ошибка Bluetooth Bluetooth
- 7. Ошибка Bluetooth Bluetooth для Bluetooth потеряна
- 8. Кнопка сканирования Bluetooth Bluetooth
- 9. Соединение Bluetooth Bluetooth потеряно
- 10. Bluetooth IOException: Bluetooth отключен
- 11. Android Bluetooth: RFCOMM + bluetooth headset
- 12. Подключиться к разъему Bluetooth Bluetooth
- 13. Поиск профилей Bluetooth Bluetooth GATT
- 14. Расширение возможностей обнаружения Bluetooth Bluetooth
- 15. Возможности Bluetooth для Android Bluetooth
- 16. Auto Accept Bluetooth Bluetooth Pairing
- 17. Bluetooth Bluetooth Low Energy Pairing
- 18. Визуализируйте силу сигнала Bluetooth Bluetooth
- 19. Ошибка соединения Bluetooth с Bluetooth
- 20. Ошибка подключения Bluetooth к Bluetooth
- 21. Проверьте состояние подключенных устройств Bluetooth
- 22. Подключение Bluetooth-ключа Bluetooth с BLE (Bluetooth Low Energy)
- 23. Handle Android Уведомление о отключении Bluetooth LE
- 24. Выход Bluetooth для выхода Bluetooth Bluetooth слишком медленный.
- 25. Уровни безопасности Bluetooth низкой энергии Bluetooth
- 26. Различия между Bluetooth и Bluetooth LE?
- 27. Сканирование для Bluetooth и Bluetooth LE
- 28. Время ожидания Bluetooth Bluetooth для BluetoothSocket.connect();
- 29. Ошибка записи Bluetooth Bluetooth на 4.2.2
- 30. Как переименовать Android-устройство Bluetooth Bluetooth?