0

我编写的用于在启用蓝牙时禁用蓝牙的服务崩溃。

///////////////////////////////// 服务 //////////////// ///////////////

 public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            Intent startServiceIntent = new Intent(context, Bluetoothservice.class);
            context.startService(startServiceIntent);

            if(intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
                BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
                if (bluetooth.getState() == BluetoothAdapter.STATE_ON
                        || bluetooth.getState() == BluetoothAdapter.STATE_TURNING_ON) {
                bluetooth.disable();
                }
                return;
            }

///////////////////////////////// 接收者 //////////////// ////////////////

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        IntentFilter filterb = new IntentFilter(
                BluetoothAdapter.ACTION_STATE_CHANGED);
        BroadcastReceiver mreceiverb = new Broadcastreceiver();
        registerReceiver(mreceiverb, filterb);
        return super.onStartCommand(intent, flags, startId);

    }

//////////////////////////// Logcat /////////////////////////////////////////////////////////////////////// ////////////////

  E/AndroidRuntime( 8759): FATAL EXCEPTION: main
    E/AndroidRuntime( 8759): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.adapter.action.STATE_CHANGED flg=0x10000010 (has extras) } in com.teststopper.StatusReceiver@407290f8
    E/AndroidRuntime( 8759):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:734)
    E/AndroidRuntime( 8759):    at android.os.Handler.handleCallback(Handler.java:587)
    E/AndroidRuntime( 8759):    at android.os.Handler.dispatchMessage(Handler.java:92)
    E/AndroidRuntime( 8759):    at android.os.Looper.loop(Looper.java:132)
    E/AndroidRuntime( 8759):    at android.app.ActivityThread.main(ActivityThread.java:4126)
    E/AndroidRuntime( 8759):    at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime( 8759):    at java.lang.reflect.Method.invoke(Method.java:491)
    E/AndroidRuntime( 8759):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
    E/AndroidRuntime( 8759):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    E/AndroidRuntime( 8759):    at dalvik.system.NativeStart.main(Native Method)
    E/AndroidRuntime( 8759): Caused by: java.lang.SecurityException: Calling uid 10099 gave packageandroid which is owned by uid 1000
    E/AndroidRuntime( 8759):    at android.os.Parcel.readException(Parcel.java:1321)
    E/AndroidRuntime( 8759):    at android.os.Parcel.readException(Parcel.java:1275)
    E/AndroidRuntime( 8759):    at android.bluetooth.IBluetooth$Stub$Proxy.disable(IBluetooth.java:806)
    E/AndroidRuntime( 8759):    at android.bluetooth.BluetoothAdapter.disable(BluetoothAdapter.java:496)
    E/AndroidRuntime( 8759):    at com.teststopper.StatusReceiver.onReceive(StatusReceiver.java:17)
    E/AndroidRuntime( 8759):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:725)
    E/AndroidRuntime( 8759):    ... 9 more

任何人都可以帮忙吗?

4

2 回答 2

0

您必须在单独的<uses-feature>元素中指定每个功能,因此如果您的应用程序需要多个功能,它将声明多个<uses-feature>元素。

例如,在设备中需要蓝牙和相机功能的应用程序将声明这两个元素:

<uses-feature android:name="android.hardware.bluetooth" />
<uses-feature android:name="android.hardware.camera" />

还要检查您是否已添加

 <uses-permission android:name="android.permission.BLUETOOTH">

BLUETOOTH   Allows applications to connect to paired bluetooth devices
BLUETOOTH_ADMIN Allows applications to discover and pair bluetooth devices

蓝牙功能的特殊处理

于 2012-06-28T08:34:22.160 回答
0

正如我在回答您的其他问题时所说,未经用户明确同意,您不应禁用蓝牙适配器。

于 2012-06-28T19:25:55.757 回答