Android - ClassCastException - BluetoothManager无法转换为android.bluetooth.BluetoothAdapter错误

时间:2015-04-27 14:08:30

标签: android android-activity android-bluetooth

我现在遇到一个问题,我写了一些蓝牙适配器代码。我正在试图确定究竟是什么导致了这个问题:

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);

可能导致此问题的原因是什么?任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

例外情况说Context.getSystemService(Context.BLUETOOTH_SERVICE)返回BluetoothManager而不是BluetoothAdapter

要使适配器使用BluetoothManager.getAdapter()

答案 1 :(得分:0)

我会在这里引用official documentation

  

要获得代表本地蓝牙适配器的BluetoothAdapter,在JELLY_BEAN_MR1及以下版本上运行时,请调用静态getDefaultAdapter()方法;在JELLY_BEAN_MR2及更高版本上运行时,使用BLUETOOTH_SERVICE通过getSystemService(String)检索它。

答案 2 :(得分:0)

appcontext.getSystemService(Context.BLUETOOTH_SERVICE)返回BluetoothManager类型的对象。您试图将此对象声明为cast,而不是BluetoothAdapter,这样(在这种情况下)是不可能的,因此错误。幸运的是,你可以从经理那里得到适配器。

要修复,只需更改此内容:

blutoothAdapter = (BluetoothAdapter) appcontext.getSystemService(Context.BLUETOOTH_SERVICE);

到此:

blutoothAdapter = ((BluetoothManager) appcontext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();

答案 3 :(得分:0)

最终的BluetoothManager bluetoothManager =(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);

mbluetoothAdpt = bluetoothManager.getAdapter();