2012-06-28 86 views
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 [email protected] 
    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 

谁能帮助?

+0

请确保您的 权限 –

+0

权限是设置 – Loshi

回答

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 

Special handling for Bluetooth feature

+0

“<使用的许可机器人:名称=‘android.permission.BLUETOOTH_ADMIN’/> <使用的许可机器人:名称=‘android.permission.BLUETOOTH’/>”是这两个是正在设置 – Loshi

0

由于我在回答您的other question时指出,未经用户明确同意,您不应禁用蓝牙适配器。

+0

但是,如果没有用户交互,我们可以启用蓝牙如果禁用....... – Loshi

+0

即便如此,你不能指望这个来作为工作文件明确规定,你不应该做你想要做什么。 –

+0

Hmmmm应该解释一下我的老板吧........ :( – Loshi