gpt4 book ai didi

java - 无法使用Android BT API方法

转载 作者:行者123 更新时间:2023-12-02 09:24:27 31 4
gpt4 key购买 nike

我正在开发一个需要 BT 连接的 Android 应用程序。阅读 Android 开发者页面后,给定的方法(例如 isEnabled())不起作用。错误是无法解析符号isEnabled。导入的库是android.bluetooth.BluetoothAdapter。在 list 文件中,按照Android页面的说明,我还插入了蓝牙、BT管理和精细位置的权限。代码:

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 对象。将所有内容放在 onCreate 大括号内。

关于java - 无法使用Android BT API方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58438134/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com