Advertisement
sandipmjadhav

BroadcastReceiver

Oct 26th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1.  
  2. public class BapiBroadcastReceiver extends BroadcastReceiver{
  3.  
  4. private BapiHandler mHandler = null;
  5.  
  6. /**
  7. * @author sandipj
  8. * @param handler -- it will receive all the messages from this broadcast receiver
  9. */
  10. public BapiBroadcastReceiver(BapiHandler handler){
  11. mHandler = handler;
  12. }
  13.  
  14. /**
  15. * @author sandipj
  16. * It will process on received messages from broadcast receiver.
  17. * Bluetooth adapter states
  18. * 10 Bluetooth turned off
  19. * 12 Bluetooth turned on
  20. * 13 Bluetooth turning off
  21. * 11 Bluetooth tunring on
  22. */
  23.  
  24. @Override
  25. public void onReceive(Context context, Intent intent) {
  26. // TODO Auto-generated method stub
  27. String action = intent.getAction();
  28. if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
  29. if(BluetoothAdapter.getDefaultAdapter().getState() == 13){
  30. //HHDBTService.setState(GlobalValues.STATE_NONE);
  31. } else if (BluetoothAdapter.getDefaultAdapter().getState() == 10) {
  32. //HHDBTService.setState(GlobalValues.STATE_NONE);
  33. //TODO Bluetooth Turned Off
  34. Message msg = mHandler
  35. .obtainMessage(GlobalValues.MESSAGE_BLUETOOTH_CONNECTION_LOST);
  36. mHandler.sendMessage(msg);
  37. } else if (BluetoothAdapter.getDefaultAdapter().getState() == 12) {
  38. //TODO Bluetooth Turned On
  39. Message msg = mHandler.obtainMessage(GlobalValues.MESSAGE_BLUETOOTH_CONNECTION_FOUND);
  40. mHandler.sendMessage(msg);
  41. }
  42. }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){
  43. //TO Bluetooth Discovery started
  44. Message msg = mHandler.obtainMessage(GlobalValues.MESSAGE_SHOW_TOAST);
  45. Bundle bundle = new Bundle();
  46. bundle.putString(GlobalValues.TOAST,
  47. "Searching remote device...");
  48. msg.setData(bundle);
  49. mHandler.sendMessage(msg);
  50. }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
  51. //TO Bluetooth Discovery finished
  52. Message msg = mHandler.obtainMessage(GlobalValues.MESSAGE_BLUETOOTH_DESCOVERY_ENDED);
  53. mHandler.sendMessage(msg);
  54. }else if(action.equals(BluetoothDevice.ACTION_FOUND)){
  55. //TODO Bluetooth device found
  56. BluetoothDevice device = intent
  57. .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  58. Message msg = mHandler
  59. .obtainMessage(GlobalValues.MESSAGE_REMOTE_DEVICE_FOUND);
  60. Bundle bundle = new Bundle();
  61. bundle.putString(GlobalValues.KEY_CURRENT_CONNECTED_DEVICE_ADDRESS,
  62. device.getAddress());
  63. bundle.putString(GlobalValues.KEY_CURRENT_CONNECTED_DEVICE_NAME,
  64. device.getName());
  65. msg.setData(bundle);
  66. mHandler.sendMessage(msg);
  67. }else if(action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)){
  68. //TODO Bluetooth device connected
  69. Message msg = mHandler.obtainMessage(GlobalValues.MESSAGE_SHOW_TOAST);
  70. Bundle bundle = new Bundle();
  71. bundle.putString(GlobalValues.TOAST,
  72. "Connected Sensor: " + GlobalValues.CONNECTED_DEVICE_ADDRESS);
  73. msg.setData(bundle);
  74. mHandler.sendMessage(msg);
  75. }else if(action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){
  76. //TODO Bluetooth device disconnected
  77. if(BluetoothAdapter.getDefaultAdapter().isEnabled()){
  78. Message msg = mHandler
  79. .obtainMessage(GlobalValues.MESSAGE_REMOTE_DEVICE_LOST);
  80. Bundle bundle = new Bundle();
  81. bundle.putString(GlobalValues.TOAST,
  82. "Remote device connection lost.");
  83. msg.setData(bundle);
  84. mHandler.sendMessage(msg);
  85. }else{
  86. Message msg = mHandler
  87. .obtainMessage(GlobalValues.MESSAGE_BLUETOOTH_CONNECTION_LOST);
  88. mHandler.sendMessage(msg);
  89. }
  90. }
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement