我创建了一个适配器,以在“自定义对话框”中显示扫描的设备。出现自定义对话框,但即使没有任何设备出现。 每当我检查logcat时,Bluetooth Le Scanner都会显示设备。 自定义对话框的代码如下
public class CustomDialog extends Dialog{
private LeDeviceListAdapter mLeDeviceListAdapter;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private AppCompatButton Scan;
private AppCompatButton Stop;
Context mContext;
TextView textStatus;
BluetoothLeScanner mLeScanner;
private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
ProgressBar contentLoadingProgressBar;
public CustomDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog);
contentLoadingProgressBar = (ProgressBar) findViewById(R.id.contentLoading);
contentLoadingProgressBar.setVisibility(View.GONE);
textStatus = findViewById(R.id.textStatus);
textStatus.setVisibility(View.GONE);
Scan = (AppCompatButton) findViewById(R.id.scanButton);
Scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
contentLoadingProgressBar.setVisibility(View.VISIBLE);
Scan.setVisibility(View.GONE);
textStatus.setVisibility(View.VISIBLE);
BlueteethManager.with(getContext()).scanForPeripherals(10000, blueteethDevices -> {
for (BlueteethDevice device : blueteethDevices) {
if (!TextUtils.isEmpty(device.getBluetoothDevice().getName())) {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
Timber.d("%s - %s", device.getName(), device.getMacAddress());
}
}
contentLoadingProgressBar.setVisibility(View.GONE);
textStatus.setVisibility(View.GONE);
Scan.setVisibility(View.VISIBLE);
});
}
});
final BluetoothManager bluetoothManager =
(BluetoothManager) getContext().getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BlueteethDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BlueteethDevice>();
mInflator = getLayoutInflater();
}
public void addDevice(BlueteethDevice device) {
if (!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
CustomDialog.viewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.custom_dialog, null);
viewHolder = new CustomDialog.viewHolder();
viewHolder.cardDevice=view.findViewById(R.id.cardDevice);
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (CustomDialog.viewHolder) view.getTag();
}
BlueteethDevice device = mLeDevices.get(i);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText("Unknown Device");
viewHolder.deviceAddress.setText(device.getMacAddress());
viewHolder.deviceAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
device.connect(true, isConnected -> {
Timber.d("Is the peripheral connected? %s", Boolean.toString(isConnected));
if (isConnected){
Toast.makeText(mContext, "BLE device Connected", Toast.LENGTH_SHORT).show();
textStatus.setText("Connected");
textStatus.setVisibility(View.VISIBLE);
Scan.setVisibility(View.GONE);
contentLoadingProgressBar.setVisibility(View.GONE);
}
else {
Toast.makeText(mContext, "Failed to connect", Toast.LENGTH_SHORT).show();
}
});
}
});
return view;
}
}
static class viewHolder {
TextView deviceName;
TextView deviceAddress;
CardView cardDevice;
}
}
我正在使用第三方库扫描设备。尽管它可以正常工作,但是没有在定制对话框中显示任何设备名称或地址。 Layout Inflator是否对我或造成什么问题?