简体   繁体   English

无法使用 Android BT API 方法

[英]Can't use Android BT API methods

I'm developing an android app that needs a BT connection.我正在开发一个需要 BT 连接的 android 应用程序。 After reading the Android Developers page, the given method such us isEnabled() doesn't work.阅读 Android 开发人员页面后,我们提供的isEnabled()方法不起作用。 The error is cannot resolve symbol isEnabled .错误是cannot resolve symbol isEnabled The imported library is android.bluetooth.BluetoothAdapter .导入的库是android.bluetooth.BluetoothAdapter In the manifest file, following the instructions of the Android page, I also have inserted the permission to Bluetooth, BT admin and fine location.在manifest文件中,按照Android页面的说明,我还插入了蓝牙、BT admin和fine location的权限。 The code:编码:

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);
    }

}

You are creating your BluetoothAdapter object outside the onCreate method.您正在 onCreate 方法之外创建您的 BluetoothAdapter object。 Put everything inside the onCreate curly brackets.将所有内容放在 onCreate 大括号内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM