У меня возникла проблема прямо сейчас с некоторым кодом BluetoothAdapter, который я написал. Я пытаюсь определить, что именно вызывает этот вопрос:Android - ClassCastException - BluetoothManager не может быть передан в android.bluetooth.BluetoothAdapter Error
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)
Вот код, который я сейчас использую:
@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();
}
}
Это строка кода, которая является причиной этого ClassCastException:
blutoothAdapter = (BluetoothAdapter) appcontext.getSystemService(Context.BLUETOOTH_SERVICE);
Что может быть причиной этой проблемы? Любая помощь будет принята с благодарностью.