2016-05-22 3 views
1

ListViewの近くにある使用可能なすべてのデバイスを一覧表示しようとしています。Android BluetoothDevice.Action_foundブロードキャストが受信されなかった

マイマニフェストには、BLUETOOTH、BLUETOOTH_ADMIN、およびACCESS_COARSE_LOCATIONの権限が含まれています。

問題はACTION_DISCOVERY_STARTEDで、ACTION_DISCOVERY_FINISHEDは受信されましたが、ACTION_FOUNDは受信されません。私は自分のコンピュータのブルートゥースを発見可能にし、私の電話のブルートゥースデバイスはそれを発見することができますが、私のアプリはできません。近くのすべてのデバイスにも同じことが適用されます。私は自分の質問のために述べたように

private BluetoothAdapter mBluetoothAdapter; 
private ArrayList<String> mDeviceList; 
private ListView mListView; 
private ProgressDialog progressDialog; 

private static final int REQUEST_ENABLE_BT = 1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bluetooth_list); 

    //list to hold all devices found 
    mDeviceList = new ArrayList<>(); 

    //list to display all devices found 
    mListView = (ListView) findViewById(R.id.availItems); 
    mListView.setAdapter(new ArrayAdapter<>(this, 
      android.R.layout.simple_list_item_1, mDeviceList)); 

    //local bluetooth device 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    // If the adapter is null, then Bluetooth is not supported 
    if (mBluetoothAdapter == null) { 
     Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); 
    }else if(!mBluetoothAdapter.isEnabled()){ 
     Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 
    } 

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 

    if (pairedDevices.size() > 0) { 
     for (BluetoothDevice device : pairedDevices) { 
      mDeviceList.add(device.getName() + "\n" + device.getAddress()); 
     } 
    } 

    progressDialog = new ProgressDialog(this); 
    progressDialog.setMessage("Scanning..."); 
    progressDialog.setTitle("Looking for devices..."); 
    progressDialog.setCancelable(false); 
    progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
     } 
    }); 

    Button findButton = (Button) findViewById(R.id.findButton); 
    findButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      IntentFilter filter = new IntentFilter(); 

      filter.addAction(BluetoothDevice.ACTION_FOUND); 
      filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
      filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 

      registerReceiver(mReceiver, filter); 
      mBluetoothAdapter.startDiscovery(); 
     } 
    }); 
} 

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { 
      progressDialog.show(); 
     }else if (BluetoothDevice.ACTION_FOUND.equals(action) && mBluetoothAdapter.isDiscovering()) { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      mDeviceList.add(device.getName() + "\n" + device.getAddress()); 
     }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
      progressDialog.dismiss(); 
     } 
    } 
}; 

@Override 
protected void onResume() { 
    super.onResume(); 
    registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 
} 

@Override 
public void onPause() { 
    if (mBluetoothAdapter != null) { 
     if (mBluetoothAdapter.isDiscovering()) { 
      mBluetoothAdapter.cancelDiscovery(); 
     } 
    } 

    super.onPause(); 
} 

@Override 
public void onDestroy() { 
    unregisterReceiver(mReceiver); 
    mListView = null; 
    mBluetoothAdapter = null; 
    mDeviceList = null; 

    super.onDestroy(); 
} 
+0

私は今同じ問題を抱えています!あなたはそれを修正しましたか? –

+0

いいえ私はそれを理解していない、私が見つけた他のすべての関連問題は私のために働かなかった解決策を持っていた。 – Aaron

答えて

-2

は:ここ

https://stackoverflow.com/posts/comments/63810282?noredirect=1

問題をACCESS_COARSE_LOCATIONACCESS_FINE_LOCATIONがグループ化されているので、そのアクセス権は、実行時に付与されなければならなかった、とだけでなく、マニフェストのましたDANGEROUS権限グループに追加します。私はAPPCOMPATで働いていたことから、私はActivityCompatメソッドを使用しますが、この方法は、アクティビティsubclassessから簡単に呼び出すことができ、実行時に許可

  1. 要求FINE_LOCATION(または粗い場所):これを試してみてください、そうするには

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
        switch (requestCode) { 
         case MY_PERMISSION_REQUEST_CONSTANT: { 
          // If request is cancelled, the result arrays are empty. 
          if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
           //permission granted! 
          } 
          return; 
         } 
        } 
    } 
    
  2. ActivityCompat.requestPermissions(this, new String[{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSION_REQUEST_CONSTANT); 
    
  3. 方法をonRequestPermissionsResult実装

+0

他のスタックオーバーフローの質問へのリンクのみ回答を投稿しないでください。代わりに、投票/旗を複写として閉じるか、質問が重複でない場合は、*この特定の質問に対する答えを調整してください。* –

+0

申し訳ありません、これまでにこれをやったことはありません。フラグリストが見つかりませんでした。あなたは私を助けることができますか? –

+0

方向性のおかげで、ランタイムの許可は私が必要としていたすべてでした。乾杯 – Aaron

関連する問題