android is there Bluetooth receiver like sms? - android

hello everyone i face a real problem i have an arduino device that sends text information via bluetooth and i want to receive this text on my android phone but i want to check the ((text))before any reaction
is there any way to do that like SMS ((BroadcastReceiver))
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}

I don't know how exactly to do that. But in the same way you use Receiver for SMS you can use Receiver for Bluetooth.
Register receiver (Manifest or in code)
registerReceiver(ActionFoundReceiver, new IntentFilter(
BluetoothDevice.ACTION_FOUND));
And define the receiver
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name;
name = device.getName();
}
}
};
I suppose : with the communication you can catch a different pram in intent that gives to you the text.
if you want some examples take a look.
http://android-er.blogspot.com.es/2011/05/scan-bluetooth-devices.html AND
http://luugiathuy.com/2011/02/android-java-bluetooth/

Related

Receive message SmartEyeglass ControlExtension

I want to make an app for Sony SmartEyeglass where the App on the Phone and the ControlExtension exchange data on runtime.
It's pretty obvious how to send messages from the app to the extension...
public void startExtension(String msg) {
if (HelloWorldExtensionService.Object != null) {
HelloWorldExtensionService.Object
.sendMessageToExtension(msg);
}
}
but how do I get the msg in my ControlExtension, if the extension is already running?
I didn't find an onMessageReceived(String message) method for the class ControlExtension.
You can do this using Intents the same way you would for an standard Android app. For example from your Activity:
Intent intentBuzz = new Intent();
intentBuzz.setAction(buzzIntent);
mContext.sendBroadcast(intentBuzz);
Then in your Control Extension register a broadcast receiver:
buzzReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
buzzAction();
}
};
registerReceiver(buzzReceiver, new IntentFilter(buzzIntent));
This works the other way around as well if you want to pass something other than a string.

How can I detect usb connection in Android 4.2?

I used ACTION_MEDIA_MOUNTED and ACTION_MEDIA_UNMOUNTED to detect USB connection on Nexus 4, but I cannot receive any broadcast signal.
Here is my broadcast receiver code:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
debugReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
debugOn = true;
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
debugOn = false;
}
}
};
registerReceiver(debugReceiver, filter);
Any ideas? I also searched others questions; they said if I add
"filter.addDataScheme("file");"
I will get the signal, but I have tried and nothing was received.
Use UsbManager.ACTION_USB_DEVICE_ATTACHED and UsbManager.ACTION_USB_DEVICE_DETACHED for your intent filter.
To check for connection to PC use Intent.ACTION_BATTERY_CHANGED and onReceive intent.getInt(BatteryManager.EXTRA_PLUGGED) and see if the value is BatteryManager.BATTERY_PLUGGED_USB.

registering receiver to filter that can not be found

Can u help me find out why the registration of broadcast receiver returns null?
this is the code:
ScoIntent = new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
sReceiver = new ScoReceiver(context, Tmp);
if (context.registerReceiver(sReceiver, ScoIntent) == null) {
Log("FBR.GetBlueConnect:Error", "Can not find receiver ACTION_CONNECTION_STATE_CHANGED");
HFS.DisplayText("Can not connect to Bluetooth Headset, Please Exit", true);
}
and this is the reciver:
class ScoReceiver extends BroadcastReceiver {
public ScoReceiver(Context mcontext, Tools t){
bContext = mcontext;
tools = t;
}
#Override
public void onReceive(Context context, Intent arg1) {
tools.Log("ScoReceiver:onReceive", "In");
//arg1 = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
String action = arg1.getAction();
tools.Log("ScoReceiver:onReceive", ">>> Bluetooth SCO state changed !!! ");
if(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
int status = arg1.getIntExtra(BluetoothHeadset.EXTRA_STATE, AudioManager.SCO_AUDIO_STATE_ERROR );
}
The javadocs say,
Returns the first sticky intent found that matches filter, or null if
there are none.
Does this receiver have a sticky intent? Here's a post that talks about the difference between a stick and non-sticky intent,
what is the difference between sendStickyBroadcast and sendBroadcast in Android

POWER connected and disconnected listeners

I am listening to both connecting and disonnecting the power for my galaxy.
I have creatd 2 BroadCastReceivers, one for connect, and one for disconnect.
When I try to implement, I only get the connected data, even when disconnecting the power cable.
The intent is sent, but looks like it's the wrong one.
Here is the activity code:
// Handle Power On
PowerConnectedBCReceiver myPowerConnectedBCReceiver = new PowerConnectedBCReceiver();
IntentFilter intentPowerOnFilter = new IntentFilter();
intentPowerOnFilter.addAction("android.intent.action.ACTION_POWER_CONNECTED");
registerReceiver(myPowerConnectedBCReceiver, intentPowerOnFilter);
BroadcastReceiver PowerConnectedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showToast("connected");
}
};
// Handle Power Off
PowerConnectedBCReceiver myPowerDisonnectedBCReceiver = new PowerConnectedBCReceiver();
IntentFilter intentPowerDisconnectedOnFilter = new IntentFilter();
intentPowerDisconnectedOnFilter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");
registerReceiver(myPowerDisonnectedBCReceiver, intentPowerDisconnectedOnFilter);
BroadcastReceiver PowerDisconnectedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showToast("disconnected");
}
};
Registering both BC to do the work
registerReceiver(PowerDisconnectedReceiver, new IntentFilter("com.neglected.POWER_DISCONNECTED"));
registerReceiver(PowerConnectedReceiver, new IntentFilter("com.neglected.POWER_CONNECTED"));
BroadCast connected code:
public class PowerConnectedBCReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Phone was connected to power" , Toast.LENGTH_LONG).show();
Intent tIntent = new Intent("com.neglected.POWER_CONNECTED");
context.sendBroadcast(tIntent);
}
}
Broadcast disconnected code:
public class PowerDisconnectedBCReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Phone was disconnected from power" , Toast.LENGTH_LONG).show();
Intent tIntent = new Intent("com.neglected.POWER_DISCONNECTED");
context.sendBroadcast(tIntent);
}
}
IS the code wrong?
Can I listen to both actions? seperately?
I can't see extra been sent with the CONNNECTED Action, is there?
Not sure what your last two classes (*BCReeivers) are supposed to be doing. Your first block of code looks ok. It will be limited to the lifecycle of the enclosing Activity if that matters.
For the Galaxy S, you may not be able to rely on those Intents. In particular, I have found that the Verizon Fascinate (their version of the Galaxy S) to be very buggy. See here: http://devblog.bu.mp/how-to-ddos-yourself
There was an error in my Broadcast instantiation. I mistakenly used the PowerConnectedBCReceiver instead of PowerDisconnectedBCReceiver
problem solved.

How to know Android Phone is going to sleep?

How to know Android Phone is going to sleep?
Please Help me with a sample code.
Thanks for reading.
You'll need to register a broadcastreceiver for Intent.ACTION_SCREEN_OFF
So, create the broadcastreceiver
This is where you handle the screen_off intent.
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(final Context context, final Intent intent) {
/*
* dispatch screen_off
* to handler method
*/
String iAction = intent.getAction();
if (iAction.equals(Intent.ACTION_SCREEN_OFF))
handleScreenAction(iAction);
}
};
Now the filter to register the receiver.
static void registerReciever() {
IntentFilter myFilter = new IntentFilter();
// Catch screen off event
myFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, myFilter);
}

Categories

Resources