Advertisement
moonlightcheese

Untitled

Jun 30th, 2011
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. // Create a BroadcastReceiver for ACTION_FOUND, ACTION_STATE_CHANGED, ACTION_DISCOVERY_FINISHED
  2.     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  3.         public void onReceive(Context context, Intent intent) {
  4.             String action = intent.getAction();
  5.             if (ScannerService.ACTION_SELECT_SCANNER.equals(action)) {
  6.                 //display a list of scanners to the user
  7.                 String[] deviceArray = intent.getStringArrayExtra("scannerArray");
  8.                 if(deviceArray == null) {
  9.                     //show "no scanners found" dialog
  10.                     Log.i(LOG_TAG, "no scanners found");
  11.                 } else {
  12.                     Log.i(LOG_TAG, "intent contents: " + deviceArray.toString());
  13.                     Bundle b = new Bundle();
  14.                     b.putStringArray("deviceListArray", deviceArray);
  15.                     removeDialog(DIALOG_BT_LIST);
  16.                     showDialog(DIALOG_BT_LIST, b);
  17.                 }
  18.             }
  19.             if (ScannerService.ACTION_READ_SCANNER.equals(action)) {
  20.                 String extra = intent.getStringExtra("scannerRead");
  21.                 Log.i(LOG_TAG, "read message: " + extra);
  22.                 TextView newTextView = new TextView(context);
  23.                 newTextView.setText(extra+"\n");
  24.                 mRootView.addView(newTextView);
  25.             }
  26.             if(ScannerService.ACTION_REQUEST_RECONNECT.equals(action)) {
  27.                 //ask the user real nice if they want the bluetooth scanner reconnected after a disconnect
  28.                 Log.i(LOG_TAG, "requested reconnect!");
  29.                 TextView newTextView = new TextView(context);
  30.                 newTextView.setText("reconnection request from service, connection to scanner lost");
  31.                 mRootView.addView(newTextView);
  32.                 Intent reconnectIntent = new Intent();
  33.                 reconnectIntent.setAction(ScannerService.ACTION_RECONNECT);
  34.                 sendBroadcast(reconnectIntent);
  35.             }
  36.         }
  37.     };
  38.  
  39. //in onResume:
  40. @Override
  41.     public void onResume() {
  42.         super.onResume();
  43.         IntentFilter filter = new IntentFilter();
  44.         filter.addAction(ScannerService.ACTION_SELECT_SCANNER);
  45.         filter.addAction(ScannerService.ACTION_READ_SCANNER);
  46.         filter.addAction(ScannerService.ACTION_REQUEST_RECONNECT);
  47.         registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
  48.     }
  49.  
  50. //onPause
  51. @Override
  52.     public void onPause() {
  53.         super.onPause();
  54.         unregisterReceiver(mReceiver);
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement