Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BapiBroadcastReceiver extends BroadcastReceiver{
- private BapiHandler mHandler = null;
- /**
- * @author sandipj
- * @param handler -- it will receive all the messages from this broadcast receiver
- */
- public BapiBroadcastReceiver(BapiHandler handler){
- mHandler = handler;
- }
- /**
- * @author sandipj
- * It will process on received messages from broadcast receiver.
- * Bluetooth adapter states
- * 10 Bluetooth turned off
- * 12 Bluetooth turned on
- * 13 Bluetooth turning off
- * 11 Bluetooth tunring on
- */
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- String action = intent.getAction();
- if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
- if(BluetoothAdapter.getDefaultAdapter().getState() == 13){
- //HHDBTService.setState(GlobalValues.STATE_NONE);
- } else if (BluetoothAdapter.getDefaultAdapter().getState() == 10) {
- //HHDBTService.setState(GlobalValues.STATE_NONE);
- //TODO Bluetooth Turned Off
- Message msg = mHandler
- .obtainMessage(GlobalValues.MESSAGE_BLUETOOTH_CONNECTION_LOST);
- mHandler.sendMessage(msg);
- } else if (BluetoothAdapter.getDefaultAdapter().getState() == 12) {
- //TODO Bluetooth Turned On
- Message msg = mHandler.obtainMessage(GlobalValues.MESSAGE_BLUETOOTH_CONNECTION_FOUND);
- mHandler.sendMessage(msg);
- }
- }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){
- //TO Bluetooth Discovery started
- Message msg = mHandler.obtainMessage(GlobalValues.MESSAGE_SHOW_TOAST);
- Bundle bundle = new Bundle();
- bundle.putString(GlobalValues.TOAST,
- "Searching remote device...");
- msg.setData(bundle);
- mHandler.sendMessage(msg);
- }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
- //TO Bluetooth Discovery finished
- Message msg = mHandler.obtainMessage(GlobalValues.MESSAGE_BLUETOOTH_DESCOVERY_ENDED);
- mHandler.sendMessage(msg);
- }else if(action.equals(BluetoothDevice.ACTION_FOUND)){
- //TODO Bluetooth device found
- BluetoothDevice device = intent
- .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- Message msg = mHandler
- .obtainMessage(GlobalValues.MESSAGE_REMOTE_DEVICE_FOUND);
- Bundle bundle = new Bundle();
- bundle.putString(GlobalValues.KEY_CURRENT_CONNECTED_DEVICE_ADDRESS,
- device.getAddress());
- bundle.putString(GlobalValues.KEY_CURRENT_CONNECTED_DEVICE_NAME,
- device.getName());
- msg.setData(bundle);
- mHandler.sendMessage(msg);
- }else if(action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)){
- //TODO Bluetooth device connected
- Message msg = mHandler.obtainMessage(GlobalValues.MESSAGE_SHOW_TOAST);
- Bundle bundle = new Bundle();
- bundle.putString(GlobalValues.TOAST,
- "Connected Sensor: " + GlobalValues.CONNECTED_DEVICE_ADDRESS);
- msg.setData(bundle);
- mHandler.sendMessage(msg);
- }else if(action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){
- //TODO Bluetooth device disconnected
- if(BluetoothAdapter.getDefaultAdapter().isEnabled()){
- Message msg = mHandler
- .obtainMessage(GlobalValues.MESSAGE_REMOTE_DEVICE_LOST);
- Bundle bundle = new Bundle();
- bundle.putString(GlobalValues.TOAST,
- "Remote device connection lost.");
- msg.setData(bundle);
- mHandler.sendMessage(msg);
- }else{
- Message msg = mHandler
- .obtainMessage(GlobalValues.MESSAGE_BLUETOOTH_CONNECTION_LOST);
- mHandler.sendMessage(msg);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement