Reputation: 1927
I'm having an issue right now with some BluetoothAdapter code I wrote. 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:
blutoothAdapter = (BluetoothAdapter) appcontext.getSystemService(Context.BLUETOOTH_SERVICE);
What could be causing this issue? Any help would be greatly appreciated.
Upvotes: 0
Views: 1732
Reputation: 72
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mbluetoothAdpt = bluetoothManager.getAdapter();
Upvotes: 0
Reputation: 712
appcontext.getSystemService(Context.BLUETOOTH_SERVICE)
returns an object of type 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. 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();
Upvotes: 0
Reputation: 29434
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; when running on JELLY_BEAN_MR2 and higher, retrieve it through getSystemService(String) with BLUETOOTH_SERVICE.
Upvotes: 0
Reputation: 8734
As the exception says Context.getSystemService(Context.BLUETOOTH_SERVICE)
returns BluetoothManager
not BluetoothAdapter
.
To get the adapter use the BluetoothManager.getAdapter()
Upvotes: 1