2016-03-22 156 views
7

6.01中的藍牙似乎無法正常工作,根據update + appCompat for Kitkat 4.4.4的以下代碼和權限無法正常工作。Android Marshmallow 6.0.1藍牙掃描未返回

沒有結果正在返回,我有幾個可發現的設備在附近。

任何人有任何見解,爲什麼?我在Nexus 5

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

package bluetoothscanneractivity; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 

public class BluetoothScannerActivity extends Activity { 
    //private final BroadcastReceiver FoundReceiver = null; 
    protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>(); 
    private ListView foundDevicesListView; 
    private ArrayAdapter<BluetoothDevice> btArrayAdapter; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetooth_scanner); 
     final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     final Button scanb = (Button) findViewById(R.id.button); 
     final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1); 

     btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this, 
       android.R.layout.simple_list_item_1, foundDevices); 
     foundDevicesListView.setAdapter(btArrayAdapter); 

     //Turn on Bluetooth 
     if (myBlueToothAdapter == null) 
      Toast.makeText(BluetoothScannerActivity.this, "Your device doesnot support Bluetooth", Toast.LENGTH_LONG).show(); 
     else if (!myBlueToothAdapter.isEnabled()) { 
      Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(BtIntent, 0); 
      Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show(); 
     } 
     //scan 
     scanb.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       btArrayAdapter.clear(); 
       myBlueToothAdapter.startDiscovery(); 
       Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
     IntentFilter filter = new IntentFilter(
       BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(FoundReceiver, filter); 

    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     unregisterReceiver(FoundReceiver); 
    } 

    private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      String action = intent.getAction(); 

      // When discovery finds a new device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       if (!foundDevices.contains(device)) { 
        foundDevices.add(device); 
        btArrayAdapter.notifyDataSetChanged(); 
       } 
      } 

      // When discovery cycle finished 
      if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       if (foundDevices == null || foundDevices.isEmpty()) { 
        Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show(); 
       } 
      } 

     } 
    }; 


} 
+0

你可以粘貼startDiscovery()方法代碼嗎? –

+0

不需要 - 這是標準的Android BL呼叫。 – mcdoomington

+0

你能看到吐司 - Toast.makeText(BluetoothScannerActivity.this,「掃描設備」,Toast.LENGTH_LONG).show(); –

回答

4

與所添加的權限,改變到最小SDK版本到23 - 它適用於下列:

package bluetoothscanneractivity; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.Manifest; 

import java.util.ArrayList; 

public class BluetoothScannerActivity extends AppCompatActivity { 
    //private final BroadcastReceiver FoundReceiver = null; 
    protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>(); 
    private ListView foundDevicesListView; 
    private ArrayAdapter<BluetoothDevice> btArrayAdapter; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetooth_scanner); 
     final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     final Button scanb = (Button) findViewById(R.id.button); 
     final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1); 

     btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this, 
       android.R.layout.simple_list_item_1, foundDevices); 

     foundDevicesListView.setAdapter(btArrayAdapter); 

     //Turn on Bluetooth 
     if (myBlueToothAdapter == null) 
      Toast.makeText(BluetoothScannerActivity.this, "Your device doesnt support Bluetooth", Toast.LENGTH_LONG).show(); 
     else if (!myBlueToothAdapter.isEnabled()) { 
      Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(BtIntent, 0); 
      Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show(); 
     } 

     // Quick permission check 
     int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION"); 
     permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION"); 
     if (permissionCheck != 0) { 

      this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 
     } 


     scanb.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       btArrayAdapter.clear(); 

       myBlueToothAdapter.startDiscovery(); 

       Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show(); 

      } 
     }); 

     registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
     IntentFilter filter = new IntentFilter(
       BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(FoundReceiver, filter); 

    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     unregisterReceiver(FoundReceiver); 
    } 


    private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      String action = intent.getAction(); 

      // When discovery finds a new device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       if (!foundDevices.contains(device)) { 
        foundDevices.add(device); 
        Toast.makeText(BluetoothScannerActivity.this, "name: " + device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show(); 
        btArrayAdapter.notifyDataSetChanged(); 
       } 

      } 

      // When discovery cycle finished 
      if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       if (foundDevices == null || foundDevices.isEmpty()) { 
        Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show(); 
       } 
      } 

     } 
    }; 


} 
+0

我不是應用程序開發人員,但我注意到我的「blueterm」應用程序不再連接到我的嵌入式設備,因爲我的手機已升級到Android 6.0.1。這是一個適用於這樣的應用程序的問題嗎? –

+0

我懷疑是這樣 - 你可以修改權限處理,因爲這是一個快速和骯髒的運行。 – mcdoomington

+0

謝謝,我認爲這需要我可以訪問「blueterm」應用程序的源代碼,對吧? –

13

你知道,自從Marshmallow,你需要這些權限,您的任務運行 -

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

此外,由於Marshmallow,你必須以編程方式。因爲即使你的權限要求已經在你的Manifest文件中聲明瞭它們。

所以你之前,你startDiscovery()

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 

的位置許可請求和用戶接受這些權限後,可以startDiscovery()。如果他/她否認,則無法發現設備。 你可以檢查用戶動作

onRequestPermissionsResult()回調。

+0

啊我不知道我需要以編程方式請求權限 - 我會檢查這一點。 – mcdoomington

+0

啊,我不知道我需要以編程方式請求權限,但它仍然不起作用。我甚至通過應用程序的信息許可頁面啓用了許可。 – mcdoomington

+0

我可以證實這一點解決了我的問題,我在我的主要活動中這樣做。 – Dayan

2

如果SDK版本是下面的方法將只執行>棒糖。當我試圖使用它沒有這個限制我的應用程序崩潰。 只需在使用.startDiscovery()之前調用此方法;

public void checkBTPermissions(){ 
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){ 
      int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION"); 
     permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION"); 
     if (permissionCheck != 0) { 

      this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number 
     } 
    }else{ 
     Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP."); 
    } 
}