J
J
Johnny_Bravo2020-05-25 02:03:07
Java
Johnny_Bravo, 2020-05-25 02:03:07

Need to pass ListView with Bluetooth list to DialodFragment?

Please tell me, you need to pass a ListView with a Bluetooth list to DialodFragment, does creating a list in layout work correctly? but here in the Fragment an empty rectangle without a list is opened.

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter bluetoothAdapter;
private ListView listView;
private ArrayList pairedDeviceArrayList;
private ArrayAdapter pairedDeviceAdapter;
public static BluetoothSocket clientSocket;

private ImageView btnImageBluetooth;

DialogFragment bltDialog;

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

listView = (ListView) findViewById(R.id.list_device);

btnImageBluetooth = (ImageView) findViewById(R.id.btnImageBluetooth);
btnImageBluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

MyDialogFragment myDialogFragment = new MyDialogFragment();
FragmentManager manager = getSupportFragmentManager();
//myDialogFragment.show(manager, "dialog");

FragmentTransaction transaction = manager.beginTransaction();
myDialogFragment.show(transaction, "dialog");

if (permissionGranted()) {

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothEnabled()) {
findBluetooth();

}
}

}
});
}

private boolean bluetoothEnabled() {
if (bluetoothAdapter.isEnabled()) {
return true;
} else {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 0);
return false;
}
}

private boolean permissionGranted() {

if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH)
== PermissionChecker.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.BLUETOOTH_ADMIN) == PermissionChecker.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN}, 0);
return false;
}
}

public void findBluetooth() {
Set pairedDevice = bluetoothAdapter.getBondedDevices();

if (pairedDevice.size() > 0) {
pairedDeviceArrayList = new ArrayList<>();
for (BluetoothDevice device : pairedDevice) {

pairedDeviceArrayList.add(device.getAddress() + "/" + device.getName());
}

pairedDeviceAdapter = new ArrayAdapter(this,
R.layout.item_device, R.id.item_device_textView, pairedDeviceArrayList);

Dialog dialog = new Dialog(MainActivity.this);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

final String itemMAC = listView.getItemAtPosition(i).toString().split( "/", 2)[0];

new Thread() {
@Override
public void run() {

BluetoothDevice device = bluetoothAdapter.getRemoteDevice(itemMAC);
try {

BluetoothSocket socket = device.createRfcommSocketToServiceRecord
(device.getUuids()[0].getUuid());
socket.connect();
if (socket.isConnected()) {

Log.e("eee", "socket get");
clientSocket = socket;

} else {

}
} catch (Exception e) {
e.printStackTrace();
}
}

}.start();

}
});
}
}
}
public class MyDialogFragment extends DialogFragment {

ListView listView;

@SuppressLint("ResourceType")
public Dialog onCreateDialog(Bundle savedInstanceState) {
// MainActivity ;

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// builder.setView(R.id.list_device);
builder.setTitle("Select ")
.setItems(R.layout.item_device, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

}
});

return builder.create();
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question