[英]Trying to get BluetoothAdapter; Cannot resolve method 'GetSystemService(java.lang.String)'
I am following along with the BluetoothLE docs at Android Studio: 我正在关注Android Studio中的BluetoothLE文档:
https://developer.android.com/guide/topics/connectivity/bluetooth-le#setup https://developer.android.com/guide/topics/connectivity/bluetooth-le#setup
My goal is to set up Bluetooth to be able to work with my java app. 我的目标是设置蓝牙,使其能够与我的Java应用程序一起使用。
Why am I getting the 'Cannot resolve method' error, and how do I resolve this? 为什么会出现“无法解析方法”错误,该如何解决? Also, why is 'mBluetoothAdapter' an unknown class if I just declared it as the variable name?
另外,如果我只是将mBluetoothAdapter声明为变量名,为什么它是未知类?
package com.august.sensorclient;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import java.lang.String;
public class BLEScanner {
private BluetoothAdapter mBluetoothAdapter;
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
Solved: I BELIEVE THE REASON WHY MY CODE WAS NOT GETTING ERRORS WAS BECAUSE I WAS NOT ACTUALLY CALLING ANYTHING BECAUSE I WAS TRYING TO WRITE THIS CODE INTO THE CLASS AND NOT INTO AN ACTUAL FUNCTION. 解决:我相信我的代码之所以没有出错的原因,是因为我没有尝试实际调用任何代码,因为我正尝试将此代码写入该类而不是实际功能。
Edit your code with 使用编辑代码
BluetoothManager bluetoothAdapter = (BluetoothManager)
mContext.getSystemService(Context.BLUETOOTH_SERVICE);
to get the mContext you have to pass your activity context to BLEScanner class. 要获取mContext,您必须将活动上下文传递给BLEScanner类。
Or you can directly code like this 或者你可以像这样直接编码
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.