当前位置: 首页 > 知识库问答 >
问题:

如何正确显示蓝牙设备列表

沈建柏
2023-03-14

我有一个活动,我想显示配对和未配对蓝牙设备的列表。我不想要一个只显示列表的按钮,但是列表是直接显示的。我遵循android指南https://github.com/googlesamples/android-bluetoothchat/blob/master/application/src/main/java/com/example/android/bluetoothchat/deviceListactivity.java的示例,但我不明白为什么未配对的设备没有出现在列表中,而已经配对的设备却正确地出现在列表中。这是我的密码

public class BluetoothListActivity extends AppCompatActivity {

    private static final int ENABLE_REQUEST = 0;
    private ListView newDevicesList;
    private ListView pairedDevicesList;
    private ArrayList<String> stringArrayList;

    private ArrayAdapter<String> newDevicesArrayAdapter;
    private ArrayAdapter<String> pairedDevicesArrayAdapter;

    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

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

        if(bluetoothAdapter == null){
            Toast.makeText(this, "Bluetooth not supported.", Toast.LENGTH_SHORT).show();
            finish();
        }

        if(bluetoothAdapter!= null){
            if(!bluetoothAdapter.isEnabled()){
                Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(i,ENABLE_REQUEST);
            }
        }

        //do discovery
        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }

        bluetoothAdapter.startDiscovery();

        pairedDevicesArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        newDevicesArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);

        pairedDevicesList = findViewById(R.id.pairedDevicesListView);
        newDevicesList = findViewById(R.id.newDevicesListView);

        pairedDevicesList.setAdapter(pairedDevicesArrayAdapter);
        newDevicesList.setAdapter(newDevicesArrayAdapter);


        // Register for broadcasts when a device is discovered
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(broadcastReceiver, filter);

        // Register for broadcasts when discovery has finished
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(broadcastReceiver,filter);

        //Set of paired devices
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0 ){
            for(BluetoothDevice device : pairedDevices){
                pairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }else{
            pairedDevicesArrayAdapter.add("No device found");
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ENABLE_REQUEST){
            if(resultCode != RESULT_OK){
                Toast.makeText(this, "You need to enable the bluetooth.", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    newDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                if (newDevicesArrayAdapter.getCount() == 0) {
                    newDevicesArrayAdapter.add("No device found");
                }

            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }
        unregisterReceiver(broadcastReceiver);
    }
}

共有1个答案

艾安和
2023-03-14

找到了解决办法。您需要在运行时请求位置权限(ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION)

 类似资料:
  • 问题内容: 如何在使用Java的android中显示蓝牙设备名称?有什么代码可供我参考吗? 问题答案: 下面的代码将为您提供蓝牙名称,类型为。

  • 我只能找到如何显示配对的蓝牙设备,而不是当前连接的蓝牙设备。这是显示配对的代码: 有人知道如何显示当前连接的蓝牙设备和未配对的设备吗?谢谢

  • deviceName String deviceId String

  • deviceName String 设备名称 deviceId String 设备Id BluetoothDevice Object deviceName String deviceId String

  • deviceName String 设备名称 deviceId String 设备Id

  • 连接设备 接口说明 用于连接 扫描出来的蓝牙设备。 参数说明 字段 类型 必须? 说明 device RKBLEDevice 是 蓝牙设备 示例代码 Swift: RokidMobileSDK.binder.connect(device: RKBLEDevice) Objc: [RokidMobileSDK.binder connect:device]; 断开设备 接口说明 用于断开已经连接的