Пытаюсь передать массив со списком bluetooth в fragmentdialog
Пытаюсь передать массив со списком bluetooth в fragmentdilog, во время запуска приложение вылезает список fragmentdiаlog и если нажать на список с именем блютуз то приложение вылетает с ошибкой.
Attempt to invoke virtual method 'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)' on a null object reference
public class DeviceSelectDialogFragment extends DialogFragment implements OnItemClickListener {
private ListView mDeviceListView;
private List<BluetoothDevice> mDevices;
private BluetoothAdapter bluetoothAdapter;
public static BluetoothSocket clientSocket;
//BluetoothDevice device;
public interface BluetoothDeviceSelectListener {
public void onDeviceSelected(BluetoothDevice device);
public void noBluetoothDevices();
}
public DeviceSelectDialogFragment() {
// empty constructor required
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
if (defaultAdapter != null) {
mDevices = new ArrayList<BluetoothDevice>(defaultAdapter.getBondedDevices());
}
if (mDevices.size() == 0) {
BluetoothDeviceSelectListener activity = (BluetoothDeviceSelectListener) getActivity();
activity.noBluetoothDevices();
this.dismiss();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_device, container);
mDeviceListView = (ListView) view.findViewById(R.id.item_device_textView);
List<String> deviceNames = new ArrayList<String>();
for (BluetoothDevice device : mDevices) {
deviceNames.add(device.getName());
}
ListAdapter adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
android.R.id.text1, deviceNames);
mDeviceListView.setAdapter(adapter);
mDeviceListView.setOnItemClickListener(this);
getDialog().setTitle("Select Bluetooth Device");
return view;
}
@Override
public void onItemClick(AdapterView<?> adapterView, final View view, int position, final long l) {
final String itemMAC = mDeviceListView.getItemAtPosition(position).toString().split("/", 2)[0];
new Thread() {
@Override
public void run() {
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(itemMAC);
try {
//генерируем socket - поток, через который будут посылаться данные
BluetoothSocket socket = device.createRfcommSocketToServiceRecord
(device.getUuids()[0].getUuid());
socket.connect();
if (socket.isConnected()) {
//если соединение установлено, завершаем поиск
Log.e("eee", "socket get");
clientSocket = socket;
Intent intent = new Intent();
// Toast.makeText(, "Подключено", Toast.LENGTH_SHORT).show();
} else {
// Toast.makeText(getApplicationContext(), "Не удалось подключиться", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private ListView listView;
private ArrayList<String> devicesList;
private ArrayAdapter<String> pairedDeviceAdapter;
public static BluetoothSocket clientSocket;
private ImageView btnImageBluetooth;
private ImageView btnImageUp;
private ImageView btnImageDown;
private ImageView btnImageLeft;
private ImageView btnImageRight;
private ImageView btnSettings;
DialogFragment bltDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSettings = (ImageView) findViewById(R.id.setting_img);
listView = (ListView) findViewById(R.id.list_device);
//myDialogFragment.show(manager, "myDialog");
btnImageBluetooth = (ImageView) findViewById(R.id.btnImageBluetooth);
btnImageBluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DeviceSelectDialogFragment deviceSelectDialogFragment = new DeviceSelectDialogFragment();
FragmentManager manager = getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putStringArrayList("devices_list", devicesList);
deviceSelectDialogFragment.setArguments(bundle);
FragmentTransaction transaction = manager.beginTransaction();
deviceSelectDialogFragment.show(transaction, "dialog");
if (permissionGranted()) {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothEnabled()) {
}
}
}
});
}
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() {
//если оба разрешения получены, вернуть true
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;
}
}