Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BluetoothDevice.Action_发现从未收到广播_Android - Fatal编程技术网

Android BluetoothDevice.Action_发现从未收到广播

Android BluetoothDevice.Action_发现从未收到广播,android,Android,我试图在ListView中列出附近的所有可用设备 我的清单包含蓝牙、蓝牙管理和访问位置的权限 我的问题是,已收到“发现”操作和“发现”操作,但未收到“发现”操作。我的电脑可以发现蓝牙,手机上的蓝牙设备可以发现,但我的应用程序无法发现。这同样适用于附近的所有设备 private BluetoothAdapter mBluetoothAdapter; private ArrayList<String> mDeviceList; private ListView mListView; pr

我试图在ListView中列出附近的所有可用设备

我的清单包含蓝牙、蓝牙管理和访问位置的权限

我的问题是,已收到“发现”操作和“发现”操作,但未收到“发现”操作。我的电脑可以发现蓝牙,手机上的蓝牙设备可以发现,但我的应用程序无法发现。这同样适用于附近的所有设备

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();
}
专用蓝牙适配器mBluetoothAdapter;
私人ArrayList mDeviceList;
私有列表视图;
私有进程对话;
私有静态最终整数请求_ENABLE_BT=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u蓝牙\u列表);
//保存找到的所有设备的列表
mDeviceList=newarraylist();
//列表以显示找到的所有设备
mListView=(ListView)findViewById(R.id.availItems);
mListView.setAdapter(新阵列适配器(此,
android.R.layout.simple_list_item_1,mDeviceList));
//本地蓝牙设备
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
//如果适配器为空,则不支持蓝牙
if(mBluetoothAdapter==null){
Toast.makeText(此“蓝牙不可用”,Toast.LENGTH_LONG.show();
}如果(!mBluetoothAdapter.isEnabled()),则为else{
Intent enableentent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用意图、请求和启用);
}
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
如果(pairedDevices.size()>0){
用于(蓝牙设备:pairedDevices){
mDeviceList.add(device.getName()+“\n”+device.getAddress());
}
}
progressDialog=新建progressDialog(此);
progressDialog.setMessage(“扫描…”);
progressDialog.setTitle(“查找设备…”);
progressDialog.setCancelable(假);
progressDialog.setButton(DialogInterface.BUTTON_否定,“取消”,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
按钮findButton=(按钮)findviewbyd(R.id.findButton);
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
IntentFilter=newintentfilter();
filter.addAction(找到BluetoothDevice.ACTION);
filter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u已启动);
filter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED);
registerReceiver(mrReceiver,过滤器);
mBluetoothAdapter.startDiscovery();
}
});
}
专用最终广播接收器mReceiver=新广播接收器(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothAdapter.ACTION\u DISCOVERY\u STARTED.equals(ACTION)){
progressDialog.show();
}else if(BluetoothDevice.ACTION_FOUND.equals(ACTION)&&mBluetoothAdapter.isDiscovering()){
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
mDeviceList.add(device.getName()+“\n”+device.getAddress());
}else if(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED.equals(ACTION)){
progressDialog.disclose();
}
}
};
@凌驾
受保护的void onResume(){
super.onResume();
registerReceiver(mReceiver,newintentfilter(BluetoothDevice.ACTION_-FOUND));
}
@凌驾
公共无效暂停(){
if(mBluetoothAdapter!=null){
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
}
super.onPause();
}
@凌驾
公共空间{
未注册接收人(mReceiver);
mListView=null;
mBluetoothAdapter=null;
mDeviceList=null;
super.ondestory();
}

正如我针对自己的问题所述:

这里的问题是,必须在运行时授予权限,而不仅仅是在清单中,因为
ACCESS\u rough\u LOCATION
ACCESS\u FINE\u LOCATION
被分组在危险权限组中

为此,请尝试以下方法:

  • 在运行时请求精细位置(或粗略位置)权限,因为我使用appcompat,所以我使用了ActivityCompat方法,但可以从Activity子类轻松调用此方法:

    ActivityCompat.requestPermissions(this, new String[{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSION_REQUEST_CONSTANT);
    
  • 实现onRequestPermissionsResult方法:

    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;
            }
        }
    }
    

  • 我现在也有同样的问题!你解决了吗?不,我还没有弄明白,我发现的所有其他相关问题的解决方案对我也不起作用。请不要只发布其他堆栈溢出问题的链接答案。相反,投票/标记以重复方式结束,或者,如果问题不是重复的,则调整此特定问题的答案。抱歉,我以前从未这样做过,我尝试在标记列表中查找我的旧问题,但没有找到它。。你能帮我吗?不管你怎么说,我只需要运行时许可。干杯