2012-07-21 116 views
4

首先,對不起我的英文不好,我是西班牙語(Android開發的新手)。我正在開發一個簡單的藍牙文件發送器,我一步一步基於BluetoothChat android示例。安卓藍牙許可問題

現在我有一個藍牙激活請求給用戶,並選擇是或否選項應用程序崩潰。

我有在Manifest中聲明的權限。

事情是,如果用戶選擇是激活藍牙,藍牙實際上激活,但應用程序仍然崩潰後。

我不知道ACRA是否與此衝突,我使用它是因爲mi設備是huawei u8650,我沒有發現usb驅動程序直接在eclipse中運行應用程序,所以我將.apk文件每次到SD卡。

這裏是清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.BTSender" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-sdk android:minSdkVersion="10" />  
    <uses-permission android:name="android.permission.BLUETOOTH"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" android:name="MyApplication"> 
     <activity 
      android:name=".BluetoothSenderActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

,這裏是主要的(唯一的)活動:

package com.BTSender; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 

public class BluetoothSenderActivity extends Activity { 

    private BluetoothAdapter BTAdapter; 
    private String TAG = "BTSender"; 
    private final int REQUEST_ENABLE_BT = 2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // Creating window layout 
     Log.v(TAG, "**Creating window**"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Log.v(TAG, "**Window created"); 
     // Get the bluetooth adapter 
     BTAdapter = BluetoothAdapter.getDefaultAdapter(); 
     // If null, that means bluetooth is not supported on the device 
     if (BTAdapter == null) { 
      Toast.makeText(this, R.string.BluetoothNotSupported, 
       Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 
     } 
    } 

    @Override 
    public void onStart() { 
     // Check if Bluetooth is enabled, otherwise request user to enable it 
     if (!BTAdapter.isEnabled()) { 
      // IT NEEDS BLUETOOTH PERMISSION 
      // Intent to enable bluetooth, it will show the enable bluetooth 
      // dialog 
      Intent enableIntent = new Intent(
        BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      // this is to get a result if bluetooth was enabled or not 
      startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 
      // It will call onActivityResult method to determine if Bluetooth 
      // was enabled or not 
     } else { 
      // Bluetooth is enabled 
     } 
    } 

    // this will be called when in onStart method startActivityForResult is 
    // executed 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Log.v(TAG, "** onActivityResult **"); 
     // determine from which activity 
     switch (requestCode) { 
     // if it was the request to enable Bluetooth: 
     case REQUEST_ENABLE_BT: 
      if (resultCode == Activity.RESULT_OK) { 
       // Bluetooth is enabled now 
       Log.v(TAG, "** Bluetooth is now enabled**"); 
      } else { 
       // user decided not to enable Bluetooth so exit application 
       Log.v(TAG, "** Bluetooth is NOT enabled**"); 
       Toast.makeText(this, R.string.BluetoothNotEnabled, 
        Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
     } 
    } 
} 

我最近在讀答案,#1,這是我的第一個問題。 我將不勝感激任何答案。還有提示,以提高我的發展。提前致謝!

java.lang.RuntimeException: Unable to start activity  
ComponentInfo{com.BTSender/com.BTSender.BluetoothSenderActivity}: 
java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10076 nor current 
process has android.permission.BLUETOOTH. 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1654) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1670) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:123) 
    at android.app.ActivityThread.main(ActivityThread.java:3695) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10076  
nor current process has android.permission.BLUETOOTH. 
    at android.os.Parcel.readException(Parcel.java:1322) 
    at android.os.Parcel.readException(Parcel.java:1276) 
    at android.bluetooth.IBluetooth$Stub$Proxy.isEnabled(IBluetooth.java:496) 
    at android.bluetooth.BluetoothAdapter.isEnabled(BluetoothAdapter.java:351) 
    at com.BTSender.BluetoothSenderActivity.onStart(BluetoothSenderActivity.java:51) 
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
    at android.app.Activity.performStart(Activity.java:3791) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1627) 
    ... 11 more 
java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10076 nor current 
process has android.permission.BLUETOOTH. 
    at android.os.Parcel.readException(Parcel.java:1322) 
    at android.os.Parcel.readException(Parcel.java:1276) 
    at android.bluetooth.IBluetooth$Stub$Proxy.isEnabled(IBluetooth.java:496) 
    at android.bluetooth.BluetoothAdapter.isEnabled(BluetoothAdapter.java:351) 
    at com.BTSender.BluetoothSenderActivity.onStart(BluetoothSenderActivity.java:51) 
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
    at android.app.Activity.performStart(Activity.java:3791) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1627) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1670) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:123) 
    at android.app.ActivityThread.main(ActivityThread.java:3695) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
    at dalvik.system.NativeStart.main(Native Method) 

如果有幫助。當安裝應用程序它說三個權限,互聯網,藍牙和bluetooth_admin,它運行BluetoothAdapter.getDefaultAdapter();沒有問題,即使它運行藍牙請求的意圖,但它是當onActivityResult發揮作用時,應用程序崩潰就像應用程序已經失去了藍牙許可,但這很奇怪。

+0

如果您的應用程序崩潰,您必須添加logcat錯誤與您的問題。 – user370305 2012-07-21 11:52:56

+0

事情是,我不直接在eclipse中測試它,因爲android模擬器沒有藍牙支持,我不知道如何從設備採取logcat錯誤,我不知道是否從ACRA stacktrace是有用。 – 2012-07-21 13:52:59

回答

1

OK都

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

<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" /> 
<permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

聲明,我已經找到了問題,我不知道爲什麼,但ACRA是給一個錯誤的消息,因爲它表示該應用程序沒有藍牙許可權,並且該應用程序擁有它們。

我創建一個新的項目並複製文件,然後ACRA說onStart方法我沒有super.onStart();

我加了它並修復了這個問題!

11

你需要在AndroidManifest.xml

+0

''''''用於定義自定義權限。系統中已經定義了android.permission.BLUETOOTH和android.permission.BLUETOOTH_ADMIN。 – philips77 2017-08-22 14:13:21