Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm creating an app and I have a problem with getting all bluetooth paired devices. Here is my DeviceListActivity that is supposed to get and put all the devices in a listview.

public class DeviceListActivity extends AppCompatActivity {

    private BluetoothAdapter mBtAdapter;

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

        ArrayAdapter<String> pairedDeviceArrayAdapter = new ArrayAdapter<>(this, R.layout.device_name);

        ListView pairedDevicesListView = findViewById(R.id.paired_devices);
        pairedDevicesListView.setAdapter(pairedDeviceArrayAdapter);

        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

        if (pairedDevices.size() > 0)
        {
            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
            for (BluetoothDevice device : pairedDevices)
            {
                pairedDeviceArrayAdapter.add(device.getName()+"
"+device.getAddress());
            }

        }
        else
        {
            pairedDeviceArrayAdapter.add("no paired devices "+pairedDevices.size());
        }
    }

Unfortunately no devices are recognized and it goes directly in the else condition ("no paired devices" is displayed). My phone, near my laptop, with the Bluetooth enable is not recognized and I don't know why. I also verified that the android studio phone emulator had the bluetooth enable. Furthermore i have added those two permissions in the manifest file :

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

I don't know where the issue should come from.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
193 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...