Guest User

Untitled

a guest
Feb 16th, 2015
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 KB | None | 0 0
  1.  
  2. public class MainFragment extends BaseFragment implements {
  3.  
  4.     @Override
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.        
  8.         this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  9.         this.bluetoothBroadcastReceiver = new BluetoothBroadcastReceiver();
  10.        
  11.         if(this.bluetoothAdapter == null) {
  12.             String message = this.getBluetoothErrorMessage();
  13.  
  14.             Toast.makeText(this.getActivity(), message, Toast.LENGTH_LONG).show();
  15.  
  16.             this.getActivity().finish();
  17.         }
  18.     }
  19.    
  20.     @Override
  21.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  22.         return inflater.inflate(R.layout.activity_main_fragment, container, false);
  23.     }
  24.  
  25.     @Override
  26.     public void onViewCreated(View view, Bundle savedInstanceState) {
  27.         super.onViewCreated(view, savedInstanceState);
  28.     }
  29.  
  30.     @Override
  31.     public void onResume() {
  32.         super.onResume();
  33.        
  34.         IntentFilter intentFilter = new IntentFilter();
  35.         intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
  36.        
  37.         this.getActivity().registerReceiver(this.bluetoothBroadcastReceiver, intentFilter);
  38.        
  39.         if(this.bluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF && !bluetoothDialog) {
  40.             this.showBluetoothEnableDialog();
  41.         } else if(this.bluetoothService == null) {
  42.             this.bluetoothService = new BluetoothService(this.handler);
  43.         }
  44.        
  45.         if(this.bluetoothService != null) {
  46.             if(this.bluetoothService.getBluetoothState() == BluetoothService.STATE_NONE) {
  47.                 this.bluetoothService.startConnection();
  48.             }
  49.         }
  50.     }
  51.    
  52.     @Override
  53.     public void onPause() {
  54.         super.onPause();
  55.        
  56.         this.getActivity().unregisterReceiver(this.bluetoothBroadcastReceiver);
  57.     }
  58.  
  59.     @Override
  60.     public void onDestroy() {
  61.         super.onDestroy();
  62.        
  63.         if (this.bluetoothService != null) {
  64.             this.bluetoothService.stopConnection();
  65.         }
  66.     }
  67.    
  68.     private void showBluetoothEnableDialog() {
  69.         this.bluetoothDialog = true;
  70.        
  71.         String title = this.getAlertDialogTitle();
  72.         String message = this.getAlertDialogMessage();
  73.         int positive = this.getAlertDialogPositive();
  74.         int negative = this.getAlertDialogNegative();
  75.        
  76.         Builder builder = new AlertDialog.Builder(this.getActivity());
  77.         builder.setTitle(title);
  78.         builder.setMessage(message);
  79.         builder.setPositiveButton(positive, new DialogInterface.OnClickListener() {
  80.             @Override
  81.             public void onClick(DialogInterface dialog, int which) {
  82.                 bluetoothDialog = false;
  83.                 enableBluetoothRequest();
  84.             }
  85.         });
  86.         builder.setNegativeButton(negative, new DialogInterface.OnClickListener() {
  87.             @Override
  88.             public void onClick(DialogInterface dialog, int which) {
  89.                 bluetoothDialog = false;
  90.                
  91.                 dialog.dismiss();
  92.                
  93.                 getActivity().finish();
  94.             }
  95.         });
  96.         builder.setCancelable(false);
  97.        
  98.         AlertDialog alertDialog = builder.create();
  99.        
  100.         alertDialog.show();
  101.     }
  102.    
  103.     private BluetoothAdapter bluetoothAdapter = null;
  104.     private BluetoothBroadcastReceiver bluetoothBroadcastReceiver = null;
  105.     private BluetoothService bluetoothService = null;
  106.     private boolean bluetoothDialog = false;
  107.     private String connectedDeviceName = null;
  108.    
  109.     @Override
  110.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  111.         if (requestCode == Globals.ENABLE_BLUETOOTH_REQUEST) {
  112.             if (!this.bluetoothAdapter.isEnabled()) {
  113.                 if(!bluetoothDialog) {
  114.                     this.showBluetoothEnableDialog();
  115.                 }
  116.             }
  117.         } else if(requestCode == Globals.CONNECT_BLUETOOTH_REQUEST) {
  118.             if (resultCode == Activity.RESULT_OK) {
  119.                 this.connectDevice(data);
  120.             }
  121.         }
  122.     }
  123.    
  124.     private void enableBluetoothRequest() {
  125.         Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  126.        
  127.         this.startActivityForResult(enableBluetoothIntent, Globals.ENABLE_BLUETOOTH_REQUEST);
  128.     }
  129.    
  130.     private void connectDevice(Intent data) {
  131.         String address = data.getExtras().getString(Globals.EXTRA_DEVICE_ADDRESS);
  132.        
  133.         BluetoothDevice device = this.bluetoothAdapter.getRemoteDevice(address);
  134.  
  135.         this.bluetoothService.connect(device);
  136.     }
  137.    
  138.     private void startDeviceListActivity() {
  139.         Intent intent = new Intent(this.getActivity(), BluetoothActivity.class);
  140.         intent.putExtra(Globals.KEY_LANGUAGE, this.language);
  141.        
  142.         this.startActivityForResult(intent, Globals.CONNECT_BLUETOOTH_REQUEST);
  143.     }
  144.    
  145.     @SuppressLint("HandlerLeak")
  146.     private final Handler handler = new Handler() {
  147.         @Override
  148.         public void handleMessage(Message msg) {
  149.             switch (msg.what) {
  150.             case Globals.MESSAGE_STATE_CHANGE:
  151.                 break;
  152.             case Globals.MESSAGE_WRITE:
  153.                 break;
  154.             case Globals.MESSAGE_READ:
  155.                 byte[] readBuf = (byte[]) msg.obj;
  156.                 String readMessage = new String(readBuf, 0, msg.arg1);
  157.  
  158.                 if (getActivity() != null) {
  159.                     Toast.makeText(getActivity(), readMessage, Toast.LENGTH_SHORT).show();
  160.                 }
  161.                
  162.                 break;
  163.             case Globals.MESSAGE_DEVICE_NAME:
  164.                 connectedDeviceName = msg.getData().getString(Globals.EXTRA_DEVICE_NAME);
  165.  
  166.                 if (getActivity() != null) {
  167.                     Toast.makeText(getActivity(), "Connected to " + connectedDeviceName, Toast.LENGTH_SHORT).show();
  168.                 }
  169.                 break;
  170.             case Globals.MESSAGE_TOAST:
  171.                 if (getActivity() != null) {
  172.                     Toast.makeText(getActivity(), msg.getData().getString(Globals.TOAST), Toast.LENGTH_SHORT).show();
  173.                 }
  174.                 break;
  175.             }
  176.         }
  177.     };
  178.    
  179.     private class BluetoothBroadcastReceiver extends BroadcastReceiver {
  180.  
  181.         @Override
  182.         public void onReceive(Context context, Intent intent) {
  183.             String action = intent.getAction();
  184.  
  185.             if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
  186.                 if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
  187.                     if(!bluetoothDialog) {
  188.                         showBluetoothEnableDialog();
  189.                     }
  190.                    
  191.                     return;
  192.                 }
  193.             }
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment