Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainFragment extends BaseFragment implements {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- this.bluetoothBroadcastReceiver = new BluetoothBroadcastReceiver();
- if(this.bluetoothAdapter == null) {
- String message = this.getBluetoothErrorMessage();
- Toast.makeText(this.getActivity(), message, Toast.LENGTH_LONG).show();
- this.getActivity().finish();
- }
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.activity_main_fragment, container, false);
- }
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- }
- @Override
- public void onResume() {
- super.onResume();
- IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
- this.getActivity().registerReceiver(this.bluetoothBroadcastReceiver, intentFilter);
- if(this.bluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF && !bluetoothDialog) {
- this.showBluetoothEnableDialog();
- } else if(this.bluetoothService == null) {
- this.bluetoothService = new BluetoothService(this.handler);
- }
- if(this.bluetoothService != null) {
- if(this.bluetoothService.getBluetoothState() == BluetoothService.STATE_NONE) {
- this.bluetoothService.startConnection();
- }
- }
- }
- @Override
- public void onPause() {
- super.onPause();
- this.getActivity().unregisterReceiver(this.bluetoothBroadcastReceiver);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (this.bluetoothService != null) {
- this.bluetoothService.stopConnection();
- }
- }
- private void showBluetoothEnableDialog() {
- this.bluetoothDialog = true;
- String title = this.getAlertDialogTitle();
- String message = this.getAlertDialogMessage();
- int positive = this.getAlertDialogPositive();
- int negative = this.getAlertDialogNegative();
- Builder builder = new AlertDialog.Builder(this.getActivity());
- builder.setTitle(title);
- builder.setMessage(message);
- builder.setPositiveButton(positive, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- bluetoothDialog = false;
- enableBluetoothRequest();
- }
- });
- builder.setNegativeButton(negative, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- bluetoothDialog = false;
- dialog.dismiss();
- getActivity().finish();
- }
- });
- builder.setCancelable(false);
- AlertDialog alertDialog = builder.create();
- alertDialog.show();
- }
- private BluetoothAdapter bluetoothAdapter = null;
- private BluetoothBroadcastReceiver bluetoothBroadcastReceiver = null;
- private BluetoothService bluetoothService = null;
- private boolean bluetoothDialog = false;
- private String connectedDeviceName = null;
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == Globals.ENABLE_BLUETOOTH_REQUEST) {
- if (!this.bluetoothAdapter.isEnabled()) {
- if(!bluetoothDialog) {
- this.showBluetoothEnableDialog();
- }
- }
- } else if(requestCode == Globals.CONNECT_BLUETOOTH_REQUEST) {
- if (resultCode == Activity.RESULT_OK) {
- this.connectDevice(data);
- }
- }
- }
- private void enableBluetoothRequest() {
- Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- this.startActivityForResult(enableBluetoothIntent, Globals.ENABLE_BLUETOOTH_REQUEST);
- }
- private void connectDevice(Intent data) {
- String address = data.getExtras().getString(Globals.EXTRA_DEVICE_ADDRESS);
- BluetoothDevice device = this.bluetoothAdapter.getRemoteDevice(address);
- this.bluetoothService.connect(device);
- }
- private void startDeviceListActivity() {
- Intent intent = new Intent(this.getActivity(), BluetoothActivity.class);
- intent.putExtra(Globals.KEY_LANGUAGE, this.language);
- this.startActivityForResult(intent, Globals.CONNECT_BLUETOOTH_REQUEST);
- }
- @SuppressLint("HandlerLeak")
- private final Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case Globals.MESSAGE_STATE_CHANGE:
- break;
- case Globals.MESSAGE_WRITE:
- break;
- case Globals.MESSAGE_READ:
- byte[] readBuf = (byte[]) msg.obj;
- String readMessage = new String(readBuf, 0, msg.arg1);
- if (getActivity() != null) {
- Toast.makeText(getActivity(), readMessage, Toast.LENGTH_SHORT).show();
- }
- break;
- case Globals.MESSAGE_DEVICE_NAME:
- connectedDeviceName = msg.getData().getString(Globals.EXTRA_DEVICE_NAME);
- if (getActivity() != null) {
- Toast.makeText(getActivity(), "Connected to " + connectedDeviceName, Toast.LENGTH_SHORT).show();
- }
- break;
- case Globals.MESSAGE_TOAST:
- if (getActivity() != null) {
- Toast.makeText(getActivity(), msg.getData().getString(Globals.TOAST), Toast.LENGTH_SHORT).show();
- }
- break;
- }
- }
- };
- private class BluetoothBroadcastReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
- if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
- if(!bluetoothDialog) {
- showBluetoothEnableDialog();
- }
- return;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment