Android蓝牙:ArrayAdapter不填充。 (旋转器)

时间:2018-11-20 01:36:55

标签: android bluetooth spinner

此代码不起作用,它应该在微调器中显示所有设备。我包括清单代码,mRecieve和Oncreate。

我的清单代码:

if (isset($_POST['post_get_tags']))

我的mReceiver:

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

我创建时:

`private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String deviceName = device.getName();
            String deviceHardwareAddress = device.getAddress();
            spinnerArrayAdapter.add(deviceName+ "\n"+deviceHardwareAddress);
            spinnerArrayAdapter.notifyDataSetChanged();
        }
    }`

2 个答案:

答案 0 :(得分:0)

我没明白你要做什么

首先请确保已将 IntentFilter 发送到此 BroadCast

如果您不这样做,此代码段将为您提供帮助

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver, filter);

如果第一步完成,请确保您已经开始发现

mBluetoothAdapter.startDiscovery();

也不要忘记完成时间(不要参与性能问题)

mBluetoothAdapter.cancelDiscovery();

确保声明所需的所有权限

如果上述步骤不能解决您的问题,请更详细地描述您的问题,因为您的问题不明确。

答案 1 :(得分:0)

您不应该为notifyDataSetChanged()调用Adapter吗?

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            spinnerArrayAdapter.add(bluetoothDevice.getName() + "\n"
                    + bluetoothDevice.getAddress());
            spinnerArrayAdapter.notifyDataSetChanged() //OVER HERE!
        }
    }
};