簡體   English   中英

無法使用 Android BT API 方法

[英]Can't use Android BT API methods

我正在開發一個需要 BT 連接的 android 應用程序。 閱讀 Android 開發人員頁面后,我們提供的isEnabled()方法不起作用。 錯誤是cannot resolve symbol isEnabled 導入的庫是android.bluetooth.BluetoothAdapter 在manifest文件中,按照Android頁面的說明,我還插入了藍牙、BT admin和fine location的權限。 編碼:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    Log.i("Fallo","Dispositivo sin bluetooth");
}

if (!bluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new     Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

}

您正在 onCreate 方法之外創建您的 BluetoothAdapter object。 將所有內容放在 onCreate 大括號內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM