Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.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 Bluetooth设备未获取可用设备_Android_Android Bluetooth - Fatal编程技术网

Android Bluetooth设备未获取可用设备

Android Bluetooth设备未获取可用设备,android,android-bluetooth,Android,Android Bluetooth,我正在尝试制作一个应用程序,其中列出了所有已配对的设备,一旦用户单击“搜索”,它将搜索新设备 代码如下: package com.example.bluetoothfinder; import androidx.appcompat.app.AppCompatActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.B

我正在尝试制作一个应用程序,其中列出了所有已配对的设备,一旦用户单击“搜索”,它将搜索新设备

代码如下:

package com.example.bluetoothfinder;

import androidx.appcompat.app.AppCompatActivity;

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.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    BluetoothAdapter bluetoothAdapter;
    ListView listView;
    ArrayList<String> bluetoothDevices;
    ArrayAdapter<String> arrayAdapter;
    Button buttonFind;
    TextView textView;

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("actions", intent.getAction());
            String action = intent.getAction();
            if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                textView.setText("Finished");
                buttonFind.setEnabled(true);
            }
        }
    };

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

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        listView = findViewById(R.id.listView);
        bluetoothDevices = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, bluetoothDevices);
        listView.setAdapter(arrayAdapter);
        buttonFind = findViewById(R.id.buttonFind);
        textView = findViewById(R.id.textView);

        Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();

        for (BluetoothDevice d : devices) {
            bluetoothDevices.add(d.getName());
        }
        arrayAdapter.notifyDataSetChanged();
        if (bluetoothDevices.size() == 0) {
            bluetoothDevices.add("no devices");
            arrayAdapter.notifyDataSetChanged();
        }

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        registerReceiver(broadcastReceiver, intentFilter);

        buttonFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bluetoothAdapter.startDiscovery();
                textView.setText("Searching...");
                buttonFind.setEnabled(false);
            }
        });
    }
}

可能是什么问题?

您是否已在设置中启用权限?即使您已经声明应用程序需要这些权限,但默认情况下不会启用。你也需要请求许可。它成功了!您能告诉我应该向用户请求哪些权限以及默认情况下将启用哪些权限吗?您必须始终检查权限是否已授予,并要求系统向您的应用授予权限。看看这是怎么回事:这不是我要问的。例如,默认情况下,像internet这样的权限将授予应用程序,而像位置这样的权限则应由用户明确授予。我要求列出用户应该授予appIt在Android文档页面上的所有权限:您在设置中启用了权限了吗?即使您已经声明应用程序需要这些权限,但默认情况下不会启用。你也需要请求许可。它成功了!您能告诉我应该向用户请求哪些权限以及默认情况下将启用哪些权限吗?您必须始终检查权限是否已授予,并要求系统向您的应用授予权限。看看这是怎么回事:这不是我要问的。例如,默认情况下,像internet这样的权限将授予应用程序,而像位置这样的权限则应由用户明确授予。我要求在Android文档页面上列出用户应授予appIt的所有权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>