2016-08-18 41 views
1

與藍牙接口我做了一個代碼,由API級別8藍牙設備但現在附近掃描時,我將使用相同的應用程序它只是失敗的API級別23掃描。除了藍牙活動,其他的事情都可以正常工作。任何人都可以告訴工作出錯的地方嗎? 我連着樣本代碼的API 8的作品,但不能在API 23差異在Android 2.2和Android 6

public class MyBluetoothScanActivity extends AppCompatActivity { 
Button bt,bt_count; 
ListView listView; 
BluetoothAdapter btAdapter; 
Set<BluetoothDevice> devicesArray; 
IntentFilter filter; 
BroadcastReceiver receiver; 
ArrayAdapter<String> listAdapter; 
ArrayList<String> pairedDevices; 
ArrayList<BluetoothDevice> devices; 
int count=0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my_bluetooth_scan); 

    bt=(Button) findViewById(R.id.bT_scan2); 
    bt.setTransformationMethod(null); 

    bt_count=(Button) findViewById(R.id.bt_count); 
    bt_count.setTransformationMethod(null); 

    bt_count.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(getBaseContext(),"Count: "+count,Toast.LENGTH_SHORT).show(); 
     } 
    }); 




    listView=(ListView) findViewById(R.id.listViewscan); 

    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 

    if(!btAdapter.isEnabled()){ 
     turnOnBT(); 
    } 


    init(); 

    bt.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      newScan(); 
     } 
    }); 


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      if(btAdapter.isDiscovering()){ 
       btAdapter.cancelDiscovery(); 
      } 
      if(!listAdapter.getItem(i).contains("Paired")){ 
       try { 
        BluetoothDevice selectedDevice = devices.get(i); 
        pairDevice(selectedDevice); 
        Thread.sleep(500); 
        newScan(); 
       } 
       catch (Exception e){} 
      } 
      else{ 
       BluetoothDevice selectedDevice = devices.get(i); 
       Intent intent=new Intent(getBaseContext(),BtIns.class); 
       intent.putExtra("MAC",selectedDevice.getAddress()); 
       startActivity(intent); 
      } 
     } 
    }); 
} 

private void newScan(){ 
    btAdapter.cancelDiscovery(); 
    Toast.makeText(getBaseContext(),"New Scan Start",Toast.LENGTH_SHORT).show(); 

    listAdapter= new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,0); 
    listView.setAdapter(listAdapter); 

    devices = new ArrayList<BluetoothDevice>(); 
    btAdapter.startDiscovery(); 
} 
private void getPairedDevices() { 
    devicesArray = btAdapter.getBondedDevices(); 
    if(devicesArray.size()>0){ 
     for(BluetoothDevice device:devicesArray){ 
      pairedDevices.add(device.getName()); 

     } 
    } 
} 

void turnOnBT(){ 
    Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
    startActivity(intent); 
} 

void init(){ 

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

      String action = intent.getAction(); 

      //Toast.makeText(getBaseContext(),"new br: "+action,Toast.LENGTH_LONG).show(); 

      if(BluetoothDevice.ACTION_FOUND.equals(action)){ 

       pairedDevices=new ArrayList<String>(); 
       getPairedDevices(); 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

       //Toast.makeText(getBaseContext(),"Dev: "+device.getName(),Toast.LENGTH_LONG).show(); 
       devices.add(device); 
       String s = ""; 
       for(int a = 0; a < pairedDevices.size(); a++){ 
        if(device.getName().equals(pairedDevices.get(a))){ 
         //append 
         s = "(Paired)"; 
         break; 
        } 
       } 

       listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress()); 

      } 
      else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){ 
       if(btAdapter.getState() == btAdapter.STATE_OFF){ 
        turnOnBT(); 
       } 
      } 


     } 
    }; 
} 

private void pairDevice(BluetoothDevice device) { 
    try { 
     Method m = device.getClass().getMethod("createBond", (Class[]) null); 
     m.invoke(device, (Object[]) null); 
    } catch (Exception e) { 
     Toast.makeText(getBaseContext(),"Exception: "+e.getMessage(),Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    try { 
     unregisterReceiver(receiver); 
     btAdapter.cancelDiscovery(); 
    } 
    catch (Exception e){} 
} 


@Override 
public void onResume() { 
    super.onResume(); 
    try { 
     Toast.makeText(getBaseContext(),"Registration",Toast.LENGTH_SHORT).show(); 
     registerReceiver(receiver, filter); 
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
     registerReceiver(receiver, filter); 
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     registerReceiver(receiver, filter); 
     filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 
     registerReceiver(receiver, filter); 
    } 
    catch (Exception e){} 
}} 

回答

0

掃描對API 23及以上的藍牙或Wi-Fi設備需要先被授予位置權限。另外,您還必須確保在「設置」中啓用了「位置」。

使用谷歌播放服務,您可以使用SettingsApi.checkLocationSettings() API檢查位置設置並要求啓用它們,例如

final LocationRequest locationRequest = LocationRequest.create(); 
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER); 

final LocationSettingsRequest request = new LocationSettingsRequest.Builder() 
     .addLocationRequest(locationRequest) 
     .setAlwaysShow(true) 
     .setNeedBle(true) 
     .build(); 

mPendingResult = SettingsApi.checkLocationSettings(mClient, request); 

參見:https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

對於請求許可,您首先需要聲明的許可,您的清單,並在掃描之前要求的權限。看到這裏的文檔:https://developer.android.com/training/permissions/requesting.html

+0

我設置權限使用<使用權限android:name =「android.permission.LOCATION_HARDWARE」/>但仍然存在的問題 –

+0

這不是正確的權限 - android.permission .ACCESS_COARSE_LOCATION'或'android.permission.ACCESS_FINE_LOCATION'是必需的。您還必須在運行時請求API 23及更高版本(如文檔中所述) – kcoppock

+0

仍然存在問題。發現開始,因此完成,但出來沒有找到設備。 –