【发布时间】:2021-01-26 15:17:24
【问题描述】:
我一直在通过 android studio 制作应用程序。该应用程序的理念是它通过蓝牙从 arduino 获取数据并将该数据显示给用户。 但是我为选择设备活动创建的 Java 类是启动它的一个,它没有运行。我已经使用 logcat 进行了检查。 ` 我还在 github 上发布了我的所有代码:https://github.com/OtakuDisease/Hivey 这是 SelectDeviceActivity.java 类。
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import com.example.hivey.ui.DeviceInfoModel;
import com.example.hivey.ui.DeviceListAdapter;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class SelectDeviceActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_device);
// Bluetooth Setup
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Get List of Paired Bluetooth Device
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
List<Object> deviceList = new ArrayList<>();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
DeviceInfoModel deviceInfoModel = new DeviceInfoModel(deviceName,deviceHardwareAddress);
deviceList.add(deviceInfoModel);
}
// Display paired device using recyclerView
RecyclerView recyclerView = findViewById(R.id.recyclerViewDevice);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
DeviceListAdapter deviceListAdapter = new DeviceListAdapter(this,deviceList);
recyclerView.setAdapter(deviceListAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
} else {
View view = findViewById(R.id.recyclerViewDevice);
Snackbar snackbar = Snackbar.make(view, "Activate Bluetooth or pair a Bluetooth device", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
snackbar.show();
}
}
}
【问题讨论】:
标签: java android xml android-recyclerview