2017-04-23 110 views
1

掃描藍牙設備無法在Android上運行事物DP3。任何想法/解決方法?沒有得到行動BluetoothDevice.ACTION_FOUND。掃描藍牙設備無法在Android上運行事物DP3

我已經在Android 6.0中添加了ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION授權更改,但仍無法使用。 https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html

而且android.permission.BLUETOOTH_ADMIN,android.permission.BLUETOOTH

我都檢查過,ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION以上的Android> = 6.0需要運行權限,但它給一個錯誤的Android由於物聯網的限制。

private void checkPermission() { 

    List<String> permissionsList = new ArrayList<String>(); 
    List<String> permissionsNeeded = new ArrayList<String>(); 

    // app 
    permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION); 
    permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION); 
    permissionsList.add(Manifest.permission.ACCESS_WIFI_STATE); 
    permissionsList.add(Manifest.permission.SET_TIME_ZONE); 
    // AndroidThingsLib 
    permissionsList.add(Manifest.permission.BLUETOOTH_ADMIN); 
    permissionsList.add(Manifest.permission.BLUETOOTH); 
    permissionsList.add(Manifest.permission.CHANGE_WIFI_STATE); 
    permissionsList.add(Manifest.permission.INTERNET); 
    permissionsList.add(Manifest.permission.ACCESS_NETWORK_STATE); 

    for (int i = 0; i < permissionsList.size(); i++){ 
     if (checkSelfPermission(permissionsList.get(i)) == PackageManager.PERMISSION_GRANTED) { 
      permissionsNeeded.add(permissionsList.get(i)); 
      updateDataLogs(SourceEnum.System, "GRANTED: " + permissionsList.get(i)); 
     } else { 
      permissionsNeeded.add(permissionsList.get(i)); 
      updateDataLogs(SourceEnum.Error, "NOT GRANTED: " + permissionsList.get(i)); 
     } 
    } 

    if (permissionsList.size()>0){ 
     requestPermissions(permissionsNeeded.toArray(new String[permissionsNeeded.size()]),5678); 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    if (requestCode == 5678){ 
     for (int i = 0; i < permissions.length; i++){ 
      updateDataLogs(SourceEnum.Error, permissions[i] + " result " + grantResults[i]); 
     } 
    } 
} 
private void enableBluetoothScan() { 
    updateDataLogs(SourceEnum.System, "enableBluetoothScan"); 

    IntentFilter filter = new IntentFilter(); 
    filter.addAction(BluetoothDevice.ACTION_FOUND); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    registerReceiver(mReceiver, filter); 

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (bluetoothAdapter.isEnabled()){ 
     bluetoothAdapter.startDiscovery(); 
    } else { 
     updateDataLogs(SourceEnum.Error, "Bluetooth Adapter not enabled"); 
    } 
} 

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { 
      updateDataLogs(SourceEnum.System, "DISCOVERY STARTED"); 
     } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
      updateDataLogs(SourceEnum.System, "DISCOVERY FINISHED"); 
     } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
       updateDataLogs(SourceEnum.System, "NEW DEVICE " + device.getName()); 
      } 
     } 
    } 
}; 

回答

1

正如Platform Overview指出,運行時授予的權限上不支持Android的東西,因爲該設備可能不具有交互顯示,以顯示該對話框。相反,Android的東西在安裝時權限模型上回退了清單中請求的所有權限。這意味着對像requestPermissions()這樣的方法的調用不是必需的,並且不會對您有所幫助。當前的預覽過程中遇到的

關於問題,以下摘錄是從Release Notes

通過應用要求危險未授予權限,直到下一次重啓設備。這包括現有應用中的新應用安裝和新元素。

目前,當您的應用程序請求危險權限(如位置權限)時,您需要在安裝(一次性)後重新啓動設備以在啓動過程中授予這些權限。

+0

嗨,即使啓動intel愛迪生也不起作用。 –

0

感謝你的解釋你已經做了什麼,我能夠克服這個問題。因爲你沒有標記爲問題尚未解決:

的build.gradle(APP;得益於12):

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 24 
    <!-- update this to your version of the Android SDK Tools --> 
    buildToolsVersion "25.0.3" 
    defaultConfig { 
     minSdkVersion 24 
    } 
} 

dependencies { 
    <!-- update this to your version of Android Things --> 
    provided 'com.google.android.things:androidthings:0.4-devpreview' 
} 

的AndroidManifest.xml(感謝12):

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package=" . . . "> 

    <!-- required for getState in startDiscovery --> 
    <uses-permission android:name="android.permission.BLUETOOTH"/> 
    <!-- required for startDiscovery --> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
    <!-- required for the IntentFilter ACTION_FOUND --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

    <!-- change to your label --> 
    <application android:label="Intel Edison"> 
     <uses-library android:name="com.google.android.things"/> 

     <!-- change to the name of your Main Activity --> 
     <activity android:name=".HomeActivity"> 
      <!-- Launch activity as default from Android Studio --> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 

      <!-- Launch activity automatically on boot --> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.IOT_LAUNCHER"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

HomeActivity.java:

IntentFilter filter = new IntentFilter(); 
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
filter.addAction(BluetoothDevice.ACTION_FOUND); 
filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED); 
registerReceiver(mReceiverBT, filter); 

BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); 
boolean started = mBtAdapter.startDiscovery(); 
if (false == started) { 
    <!-- in that case, catch BluetoothAdapter.STATE_ON in the receiver and start the Bluetooth Discovery then --> 
    mBtAdapter.enable(); 
} 

使用此代碼,我的應用程序在啓動英特爾Edison開發板後立即啓動,並能夠發現附近的藍牙設備。備註:

  • 在此主板上,Android Things需要69秒(開發人員預覽版4)或82秒(開發人員預覽版3)才能啓動我的應用程序。
  • 在logcat中,我看到「不授予權限android.permission.ACCESS_COARSE_LOCATION來打包......因爲它以前沒有安裝」。我發現沒有辦法;報告於Issue 62514750。其實,你可能根本不需要任何'危險'的權限 - 例如,我對ACTION_FOUND什麼都不做 - 因爲ACTION_NANE_CHANGED在我的場景中總是被觸發;報告於Issue 62524160