我想檢查在使用Android應用程序的設備中是否啓用了藍牙。 我使用了.isEnabled方法。但是有一個錯誤。我發現(通過註釋行)錯誤在.isEnabled方法中。你能幫我解決這個問題嗎?檢查是否使用Android應用程序啓用藍牙
final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String status = "Bluetooth";
if(bluetooth != null) {
if (bluetooth.isEnabled()) {
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
status = ("Address "+ mydeviceaddress + " Name" + mydevicename);
Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
} else {
status = ("Bluetooth not enabled");
Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
}
}
}
我剛試過。但是這也不起作用:( – Saku
@Saku有什麼錯誤?更新你的文章,並把錯誤的詳細信息 – Bishan
'強制關閉'出現由於模擬器不支持藍牙我試着在一個實際的選項卡。 t給出明確的例外或錯誤信息 – Saku