列出了ListView中可用的Bluetooth设备,但没有显示ListView

时间:2018-01-02 22:09:19

标签: android listview bluetooth

我想创建一个可以将蓝牙数据发送到其他设备的应用。首先,我制作了一个按钮来打开和关闭蓝牙。这很顺利!现在我添加了一个ListView,它应该列出可用的未配对设备,但ListView没有显示。我搜索了一个解决方案,但我找不到任何东西。我没有收到任何事件日志,但我不知道该怎么做。我尝试了很多不同的东西,但最后我的应用程序一直在崩溃。这是我的代码:

<Button
    android:text="On/Off"
    android:id="@+id/btnONOFF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    tools:ignore="MissingConstraints" />

<ListView
    android:id="@+id/lvNewDevices"
    android:layout_width="162dp"
    android:layout_height="196dp"
    app:layout_constraintTop_toBottomOf="@id/btnONOFF"
    tools:layout_editor_absoluteX="3dp"
    tools:layout_editor_absoluteY="96dp"
    tools:ignore="MissingConstraints" />

MainActivity

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private static final String TAG = "Main Activity";

BluetoothAdapter mBluetoothAdapter;

public ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();       //hold BluetoothDevices that are discovered
public DeviceListAdapter mDeviceListAdapter;
public ListView lvNewDevices;

private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        Log.d(TAG, "onReceive: ACTION FOUND.");

        if(action.equals(BluetoothDevice.ACTION_FOUND)){
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);   //throws an parcelable extra device in the former device
            mBTDevices.add(device);     //stores the new device
            Log.d(TAG, "onReceive: " + device.getName()+ ": " + device.getAddress());
            mDeviceListAdapter = new DeviceListAdapter(context, R.layout.device_adapter_view, mBTDevices);   //attach DeviceList to DeviceListAdapter
            lvNewDevices.setAdapter(mDeviceListAdapter);        //set the List to AdapterList
            //remember device_adapter_view.xml in res -> layout!
        }
    }
};

protected void onDestroy(){
    Log.d(TAG, "onDestroy: called");
    super.onDestroy();
    unregisterReceiver(mBroadcastReceiver3);
}

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

    Button btnONOFF = (Button) findViewById(R.id.btnONOFF);
    lvNewDevices = (ListView) findViewById(R.id.lvNewDevices);
    mBTDevices = new ArrayList<>();

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    lvNewDevices.setOnItemClickListener(MainActivity.this);

    btnONOFF.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "onClick: enabling/disabling bluetooth.");       //tell when Button is clicked
            enableDisableBT();
        }
    });
}

/* if Bluetooth is enabled and Button is clicked, Bluetooth will turn off
   if Bluetooth is not enabled and Button is clicked, Bluetooth will turn on
 */
public void enableDisableBT() {

    if (mBluetoothAdapter == null) {      //means device has no Bluetooth Adapter
        Log.d(TAG, "enableDisableBT: Does not have BT capabilities.");
        finish();
    }
    if (!mBluetoothAdapter.isEnabled()) {     //if Bluetooth is not enabled
        Log.d(TAG, "enableDisableBT: enabling BT.");
        Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(enableBTIntent);
    }
    if (mBluetoothAdapter.isEnabled()) {      //if Bluetooth is enabled
        Log.d(TAG, "enableDisableBT: disabling BT.");
        mBluetoothAdapter.disable();        //disable Bluetooth
    }
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    //first cancel discovery because it's very memory intensive
    mBluetoothAdapter.cancelDiscovery();
}

}

DeviceListAdapter类

public class DeviceListAdapter extends ArrayAdapter<BluetoothDevice> {

private LayoutInflater mLayoutInflater;
private ArrayList<BluetoothDevice> mDevices;
private int mViewResourceId;

public DeviceListAdapter(Context context, int tvResourceId, ArrayList<BluetoothDevice> devices){
    super(context, tvResourceId, devices);
    this.mDevices = devices;
    mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = tvResourceId;
}

public View getView(int position, View convertView, ViewGroup parent){
    convertView = mLayoutInflater.inflate(mViewResourceId, null);
    BluetoothDevice device = mDevices.get(position);

    if(device != null){
        TextView deviceName = (TextView)convertView.findViewById(R.id.tvDeviceName);
        TextView deviceAdress = (TextView)convertView.findViewById(R.id.tvDeviceAddress);

        if(deviceName != null){
            deviceName.setText(device.getName());
        }
        if(deviceAdress != null){
            deviceAdress.setText(device.getAddress());
        }
    }
    return convertView;
}

}

我制作了一个蓝牙教程,因为我之前从未编程过蓝牙。我希望你能帮助我!

1 个答案:

答案 0 :(得分:0)

在您的onReceive中,您必须在适配器列表中添加新的蓝牙设备,然后调用notifyDataChanged(),而不是每次找到设备时都不创建适配器

<强>已更新 您可以在适配器中创建一个set方法,在添加新设备后设置更新列表,然后调用adapter.notifyDataChanged()

您开始发现的代码在哪里? onReceive是否会被触发?