In the Android 2.3.3 BluetoothChat example with with createInsecureRfcommSocketToServiceRecord() API, users are still prompted to accept the pairing request, even though no PIN code is presented.
Is there a way to automate Bluetooth pairing request without user intervention? Or is this never possible due to security concerns? I have been looking online for 2 days now and haven't really found much, so if anybody knows, please post.
Thanks!
So, I had this cuestion, if some one needs the answer to this working in android 4.4.2
IntentFilter filter = new IntentFilter(
"android.bluetooth.device.action.PAIRING_REQUEST");
/*
* Registering a new BTBroadcast receiver from the Main Activity context
* with pairing request event
*/
registerReceiver(
new PairingRequest(), filter);
and the code for the Receiver
public static class PairingRequest extends BroadcastReceiver {
public PairingRequest() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
try {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
//the pin in case you need to accept for an specific pin
Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));
//maybe you look for a name or address
Log.d("Bonded", device.getName());
byte[] pinBytes;
pinBytes = (""+pin).getBytes("UTF-8");
device.setPin(pinBytes);
//setPairing confirmation if neeeded
device.setPairingConfirmation(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
and in the manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
and the broadcastReceiver
<receiver android:name=".MainActivity$PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
</intent-filter>
</receiver>
Not with the standard API, no: if the MAC address is not already in the pairing database there will always be the prompt. I'm told that if you have a device that has been rooted and have public read/write access to the bluetooth service's DBus endpoint you can work around that but I've never seen that actually implemented.
i came across the same problem, i hope the following code will help:
firsly we need:
<receiver android:name=".broadcast.PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
</intent-filter></receiver>
secondly we need the BluetoothDevice class, and:
public class PairingRequest extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("ACTION_PAIRING_REQUEST")) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
device.setPin(pinBytes);
}
}
}
Related
I am trying to implement Twilio SDK and i have done it, It is working fine when APP is in memory not killed..
Now I have to implement Twilio to get calls any time if my app is is killed or not.
How can i achieve this like by any service in background or any other solution.
Thanks in advance.
I have not worked with twillo api, but you can achieve events of calling from android system using following code. You should try this out.
To register Broadcast Receiver, write below codes in AndroidMainifest.xml file.
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
to give phone state permission to your app use below code in your AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Below is Broadcast Receiver
public class PhoneStateReciver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
try
{
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
Toast.makeText(context,"Received State",Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context,"Idle State",Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
I'm trying to have my app's service listen for Bluetooth connection and disconnection attempts, so I can dynamically check/support Bluetooth tethering network communication.
First I have two Samsung S4s (running CyanogenMod 10.2, which is Android 4.3.1 based) which I can pair just fine. If I set one device to Bluetooth tether, when the other connects, a new bt-pan network interface is created and DHCP is used to assign IPs. I confirmed this using iwconfig and ifconfig in shell.
I have the following perms in my app: (there's more, I'm just pointing out the BT perms I added)
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Here's my Service's onCreate where I set my IntentFilters: (note I've got Toasts here, but I was working with Logging originally)
#Override
public void onCreate() {
super.onCreate();
...
this.mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);
}
Here's my BroadcastReceiver implementation:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_FOUND)) {
Toast.makeText(getApplicationContext(), "BT found", Toast.LENGTH_SHORT).show();
} else if(action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
Toast.makeText(getApplicationContext(), "BT Connected",
} else if(action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "BT Disconnect requested", Toast.LENGTH_SHORT).show();
}
}
}
Now, when I turn Bluetooth off/on, connect/disconnect to a paired device, nothing fires. I've payed around with the devices from both ends. Nothing is broadcast.
Anyone have a suggestion? I really need to receive these bluetooth events. Please don't point to another post/site with the same perms and intent filters. Thanks.
I have a very similar code which works, the only major difference i found was this:
mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);
i beleive that registerReceiver should be called from the context you want to get intents to.
try calling the method from this. i.e remove the mLocalBroadcastManager like:
registerReceiver(mMessageReceiver, filter);
If you Register the Receiver on the Manifest instead of the Activity, you can receive broadcasts even if you do not have the app opened, you can do this:
<receiver android:name=".BluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.BOND_STATE_CHANGED" />
</intent-filter>
</receiver>
Basically im trying 2 things here, Im trying to start a toast when my bluetooth device is connected to a particular device (so need to check if that is the particular bluetooth name), if that is the particular device then i want to show a toast when connected to that particular bluetooth device. I also want to show a toast when my bluetooth is disconnected to that particular bluetooth device.
Here's my code:
in manifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<receiver android:name=".MyBluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
</intent-filter>
</receiver>
Class's code:
public class MyBluetoothReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "RECEIVER CALLED!!", Toast.LENGTH_LONG).show();
if(intent.getAction().equals(
"android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){
// code for Bluetooth connect
Toast.makeText(context, "CONNECTED!!", Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(
"android.bluetooth.device.action.ACL_DISCONNECTED")){
//code for Bluetooth disconnect;
Toast.makeText(getApplicationContext(),"DISCONNECTED",Toast.LENGTH_LONG).show();
}
}
}
In my code im getting receiver called toast properly and even the toast for disconnected is also working but toast of connected never works.
Please let me know why CONNECTED toast doesn't work and how to make this code work when connected to a particular device ( I don't want to show this toast for all the devices ).
Change your broadcast receiver to:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//you can get name by device.getName()
} else if (BluetoothAdapter.ACL_DISCONNECTED
.equals(action)) {
}
}
};
I wanna know how to execute a particular code when my fone's bluetooth device is connected to any device and also i want to execute another code when my fone disconnects from that bluetooth connection?
Please provide the code with proper comments.
You can create a BroadcastReceiver to handle that:
In on manifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<receiver android:name=".MyBluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
</intent-filter>
</receiver>
Write class MyBluetoothReceiver derived from BroadcastReceiver. Override the onReceive method and for each action implement your code:
public class MyBluetoothReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(
"android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){
// code for Bluetooth connect
}
if(intent.getAction().equals(
"android.bluetooth.device.action.ACL_DISCONNECTED")){
//code for Bluetooth disconnect;
}
}
}
Here's the proper answer I found :
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//you can get name by device.getName()
} else if (BluetoothAdapter.ACL_DISCONNECTED
.equals(action)) {
}
}
};
I want to receive notification when Mobile network connection is lost or received. Through following code, I can receive notification for Wi-Fi (Data) connection but not for Mobile (Voice) connection.
Manifest :
<uses-permission android:name="android.permission.ACCESS_NETWORK_ST ATE"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".notifier">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE " />
</intent-filter>
</receiver>
</application>
Java :
public class notifier extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast toast = Toast.makeText(context, "Network Changed !!", Toast.LENGTH_LONG);
toast.show();
}
}
Please tell me, how can I receive notification for Mobile Network (Voice).
Pseudocode!!
PhoneStateListener myListener = new PhoneStateListener() {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
// Handle different service states here.
}
};
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE))
.listen(myListener, PhoneStateListener.LISTEN_SERVICE_STATE);
http://developer.android.com/reference/android/telephony/TelephonyManager.html#listen(android.telephony.PhoneStateListener, int)
Check the permission in the AndroidManifest.xml, first.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
And try this code below.
ConnectivityManager manager = (ConnectivityManager)appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mobile.isConnected()) {
//Do Sth.
}
If this is not work, check your phone's brand. If the phone's developer blocked some codes or not implement some code, it doesn't work. What is your phone?