[英]Android - ClassCastException - BluetoothManager cannot be cast to android.bluetooth.BluetoothAdapter Error
I'm having an issue right now with some BluetoothAdapter code I wrote. 我编写的某些BluetoothAdapter代码现在出现问题。 I'm trying to determine what exactly is causing this issue:
我正在尝试确定到底是什么导致了此问题:
04-27 08:27:35.749 7802-7802/com.engineering.yellow E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.engineering.yellow, PID: 7802
java.lang.ClassCastException: android.bluetooth.BluetoothManager cannot be cast to android.bluetooth.BluetoothAdapter
at com.engineering.yellow.fragment.SetDevicesFragment.isNotConnected(SetDevicesFragment.java:190)
at com.engineering.yellow.bluetooth.BluetoothConnectionTask.onPostExecute(BluetoothConnectionTask.java:117)
at com.engineering.yellow.bluetooth.BluetoothConnectionTask.onPostExecute(BluetoothConnectionTask.java:22)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Here is the code I'm currently using: 这是我当前正在使用的代码:
@Override
public void isConnected() {
Log.d(Constants.TAG, "Connected device");
mNumDevicesConnected++;
if (mNumDevicesConnected == mNumDevicesSelected) {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
mBluetoothManager.startReading(getActivity());
getActivity().setResult(Activity.RESULT_OK);
getActivity().finish();
}
}
@Override
public void isNotConnected() {
BluetoothAdapter blutoothAdapter;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
appcontext = getActivity().getApplicationContext();
Log.i("SetDevicesFragment", "Context Val:" + appcontext);
appcontext = getActivity().getApplicationContext();
blutoothAdapter = (BluetoothAdapter) appcontext.getSystemService(Context.BLUETOOTH_SERVICE);
} else {
blutoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
Log.i("SetDevicesFragment", "SetDevicesFragment-BluetoothConnection-MACAddress: " + blutoothAdapter.getAddress());
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
//We check to make sure we still have activity access, in case the user attempted to go back
//before we show the dialog.
if (getActivity() != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog));
builder.setTitle(R.string.device_connection_error_title).setMessage(R.string.device_connection_error_message)
.setPositiveButton(R.string.auto_shutdown_dialog_session_ended_positive_button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
breakExistingConnections();
alertDialog.show();
}
}
This is the line of code that is causing this ClassCastException: 这是导致此ClassCastException的代码行:
blutoothAdapter = (BluetoothAdapter) appcontext.getSystemService(Context.BLUETOOTH_SERVICE);
What could be causing this issue? 是什么导致此问题? Any help would be greatly appreciated.
任何帮助将不胜感激。
As the exception says Context.getSystemService(Context.BLUETOOTH_SERVICE)
returns BluetoothManager
not BluetoothAdapter
. 如异常所示,
Context.getSystemService(Context.BLUETOOTH_SERVICE)
返回BluetoothManager
而不是BluetoothAdapter
。
To get the adapter use the BluetoothManager.getAdapter()
要获取适配器,请使用
BluetoothManager.getAdapter()
I'll cite the official documentation here: 我在这里引用官方文档 :
To get a BluetoothAdapter representing the local Bluetooth adapter, when running on JELLY_BEAN_MR1 and below, call the static getDefaultAdapter() method;
要在本地JELLY_BEAN_MR1及更低版本上运行时获取代表本地蓝牙适配器的BluetoothAdapter,请调用静态的getDefaultAdapter()方法; when running on JELLY_BEAN_MR2 and higher, retrieve it through getSystemService(String) with BLUETOOTH_SERVICE.
在JELLY_BEAN_MR2及更高版本上运行时,请通过带有BLUETOOTH_SERVICE的getSystemService(String)对其进行检索。
appcontext.getSystemService(Context.BLUETOOTH_SERVICE)
returns an object of type BluetoothManager
. appcontext.getSystemService(Context.BLUETOOTH_SERVICE)
返回BluetoothManager
类型的对象。 You're trying to declare, or cast , this object as a BluetoothAdapter
instead, which (in this case) isn't possible and hence the error. 您正在尝试将该对象声明或强制转换为
BluetoothAdapter
,这是不可能的(在这种情况下),因此是错误。 Luckily though, you can get the the adapter from the manager. 幸运的是,您可以从管理器中获取适配器。
To fix, simply change this: 要解决此问题,只需更改以下内容:
blutoothAdapter = (BluetoothAdapter) appcontext.getSystemService(Context.BLUETOOTH_SERVICE);
to this: 对此:
blutoothAdapter = ((BluetoothManager) appcontext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 最终的BluetoothManager bluetoothManager =(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mbluetoothAdpt = bluetoothManager.getAdapter(); mbluetoothAdpt = bluetoothManager.getAdapter();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.