Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Create a BroadcastReceiver for ACTION_FOUND, ACTION_STATE_CHANGED, ACTION_DISCOVERY_FINISHED
- private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (ScannerService.ACTION_SELECT_SCANNER.equals(action)) {
- //display a list of scanners to the user
- String[] deviceArray = intent.getStringArrayExtra("scannerArray");
- if(deviceArray == null) {
- //show "no scanners found" dialog
- Log.i(LOG_TAG, "no scanners found");
- } else {
- Log.i(LOG_TAG, "intent contents: " + deviceArray.toString());
- Bundle b = new Bundle();
- b.putStringArray("deviceListArray", deviceArray);
- removeDialog(DIALOG_BT_LIST);
- showDialog(DIALOG_BT_LIST, b);
- }
- }
- if (ScannerService.ACTION_READ_SCANNER.equals(action)) {
- String extra = intent.getStringExtra("scannerRead");
- Log.i(LOG_TAG, "read message: " + extra);
- TextView newTextView = new TextView(context);
- newTextView.setText(extra+"\n");
- mRootView.addView(newTextView);
- }
- if(ScannerService.ACTION_REQUEST_RECONNECT.equals(action)) {
- //ask the user real nice if they want the bluetooth scanner reconnected after a disconnect
- Log.i(LOG_TAG, "requested reconnect!");
- TextView newTextView = new TextView(context);
- newTextView.setText("reconnection request from service, connection to scanner lost");
- mRootView.addView(newTextView);
- Intent reconnectIntent = new Intent();
- reconnectIntent.setAction(ScannerService.ACTION_RECONNECT);
- sendBroadcast(reconnectIntent);
- }
- }
- };
- //in onResume:
- @Override
- public void onResume() {
- super.onResume();
- IntentFilter filter = new IntentFilter();
- filter.addAction(ScannerService.ACTION_SELECT_SCANNER);
- filter.addAction(ScannerService.ACTION_READ_SCANNER);
- filter.addAction(ScannerService.ACTION_REQUEST_RECONNECT);
- registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
- }
- //onPause
- @Override
- public void onPause() {
- super.onPause();
- unregisterReceiver(mReceiver);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement