I am developing an application which needs to send sms in pdu mode.
I am using this code but it gives NoSuchElementException on first line.
try {
Method m2 = sms.getClass().getDeclaredMethod("sendRawPdu", pdu.getClass(), pdu.getClass(), piSent.getClass(), piDelivered.getClass());
m2.setAccessible(true);
SmsMessage.SubmitPdu pdus = SmsMessage.getSubmitPdu(null, "", "Test", false);
Object[] arrayOfObject2 = new Object[5];
arrayOfObject2[0] = pdus.encodedScAddress;
arrayOfObject2[1] = pdus.encodedMessage;
arrayOfObject2[2] = piSent;
arrayOfObject2[3] = piDelivered;
arrayOfObject2[4] = null;
try {
m2.invoke(sms, arrayOfObject2);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Any help will be appreciated.
I tried it on Lollipop, but there is no method related to sendRawPdu
Do a little more thing just print the list of methods available to check if there is any method related to sendRawPdu
Method[] methods = sms.getClass().getDeclaredMethods();
boolean methodAvailable = false;
for(Method m : methods) {
Log.d("SmsManager", m.toString());
if(m.toString().contains("sendRawPdu")) {
methodAvailable = true;
}
}
now you have methodAvailable, if it is true you can send Raw PDU, if not then you can't. sendRawPdu was available before JellyBeans. Try to run this on Pre JellyBeans devices.
Related
I'm creating an app on Android which uses a part of speech tagger from Apache (OpenNLP). The app works okay, but for some reason there is large delay when it creates the part of speech tagger from a database file (en-pos-maxent.bin). I'm using Android Studio, and the database is in app/res/raw/en-pos-maxent.bin. Here's the code that initializes the POSModel:
public POSModel setupPOSTagger() {
InputStream modelIn = null;
POSModel model = null;
try {
modelIn = getResources().openRawResource(R.raw.en_pos_maxent);
//the huge delay is mainly on the line below
model = new POSModel(modelIn);
} catch (IOException e) {
// Model loading failed, handle the error
e.printStackTrace();
} finally {
if (modelIn != null) {
try {
modelIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return model;
}
Any ideas?
I am using the below code to create Multi user group but getting Timeout error, even if my timeout error is 10sec.
public void createGroup() {
String roomId = "Group_test003" + "#icoveri.com";
String nick = "Grouptest";
try {
MultiUserChatManager manager = multiUserChatManager.getInstanceFor(connection);
MultiUserChat muc = manager.getMultiUserChat(roomId);
muc.create(nick);
Form form = muc.getConfigurationForm();
Form submitForm = form.createAnswerForm();
List<FormField> fields = form.getFields();
for (int i = 0; i < fields.size(); i++) {
FormField field = (FormField) fields.get(i);
if (!FormField.Type.hidden.equals(field.getType()) && field.getVariable() != null) {
submitForm.setDefaultAnswer(field.getVariable());
}
}
List owners = new ArrayList();
owners.add(user1234 + "#icoveri.com");
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
muc.sendConfigurationForm(submitForm);
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
}
}
The error which I am getting is
org.jivesoftware.smack.SmackException$NoResponseException: No response
received within reply timeout. Timeout was 10000ms (~10s). Used
filter: AndFilter: (FromMatchesFilter (full):
Group_test003#iscoveri.com/Grouptest, StanzaTypeFilter:
org.jivesoftware.smack.packet.Presence).
at
org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:229)
at
org.jivesoftware.smackx.muc.MultiUserChat.enter(MultiUserChat.java:311)
at
org.jivesoftware.smackx.muc.MultiUserChat.createOrJoin(MultiUserChat.java:400)
at
org.jivesoftware.smackx.muc.MultiUserChat.createOrJoin(MultiUserChat.java:376)
I have got the solution. The problem was in my service i.e. iscoveri.com. I had to use different service name to create the group.
I also have spent a couple hours trying to correct the same error; in my case, the problem happened when I used XMPPBOSHConnection, but not when using XMPPTCPConnection.
I'm try send DTMF codes in icoming CALL. For this i'n try use Java reflection:
public void initialize(){
ClassLoader classLoader = Dtmf.class.getClassLoader();
final Class<?> classCallManager = classLoader.loadClass("com.android.internal.telephony.CallManager");
Method methodGetInstance = classCallManager.getDeclaredMethod("getInstance");
objectCallManager = methodGetInstance.invoke(null);
methodGetState = classCallManager.getDeclaredMethod(SEND_DTMF, char.class);
}
public boolean sendDtmf(char ch) {
boolean result = false;
if ( methodGetState != null) {
try {
Object res = methodGetState.invoke(objectCallManager,
new Object[]{Character.valueOf(ch)});
if (res instanceof Boolean) {
result = ((Boolean) res).booleanValue();
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return result;
}
Link for source code of class CallManager : Call Manager source code
But i'm always get "false" in method sendDtmf(). In debug, code is go into next:
Object res = methodGetState.invoke(objectCallManager,
new Object[]{Character.valueOf(ch)});
What wrong?
The method is likely throwing an InvocationTargetException if your application isn't signed with the platform certificate as conventional apps cannot execute these methods (and will not be granted the required platform permissions to do so).
In short: the method is returning false because you're catching (and ignoring) the exception.
There's an open issue (#1428) on the Android issue tracker for sending DTMF tones as it presently isn't possible.
I want to pair two android bluetooth devices (Kitkat) without any popup for passkey exchange. I tried setpin() and cancelPairingUserInput() methods inside the broadcast receiver for PAIRING_REQUEST intent using reflection, but got no results. Can anyone help me with that ?
if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
BluetoothDevice localBluetoothDevice = (BluetoothDevice)intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
try {
Log.d("setPin()", "Try to set the PIN");
Method m = localBluetoothDevice.getClass().getMethod("setPin", byte[].class);
m.invoke(localBluetoothDevice, ByteBuffer.allocate(4).putInt(1234).array());
Log.d("setPin()", "Success to add the PIN.");
} catch (Exception e) {
Log.e("setPin()", e.getMessage());
}
Class localClass = localBluetoothDevice.getClass();
Class[] arrayOfClass = new Class[0];
try {
localClass.getMethod("setPairingConfirmation", boolean.class).invoke(localBluetoothDevice, true);
localClass.getMethod("cancelPairingUserInput", arrayOfClass).invoke(localBluetoothDevice, null);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Did you try calling createBond() through reflection?
This works for me, with device being a BluetoothDevice:
Method m = device.getClass().getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);
Get the device and the PIN (or the pairing key) from the given Intent
if the given PIN is not -1, set it in the device
invoke the device's .setPairingConfirmation() method
My code (which achieves this) in the .bluetoothEventReceived() callback method, looks something like this:
private void bluetoothEventRecieved(Context context, Intent intent)
{
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int pin = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, -1);
if (pin != -1) {
byte[] pinBytes = String.format(Locale.US, "%04d", pin).getBytes();
device.setPin(pinBytes);
}
device.setPairingConfirmation(true);
}
}
Case 1: Look at the code below. I am able to get Class and Method objects and it works well. Method I am trying to access is android.view.View::dispatchPointerEvent.
Case 2: When I replace class/Method with com.android.server.pm.PackageManagerService::grantPermissionsLPw, I get NoMethodFoundException. Class was accessible though.
Case 3: When I replace class/Method with android.hardware.input.InputManager::injectInputEvent, I get NoMethodFoundException. Class was accessible though.
Question is: Why some of the android class/methods are accessible via reflection and some other not?
Class _class = null;
try {
_class = Class.forName("android.view.View");
Log.i("Test", "Class found");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
Log.i("Test", "Pre-Method found");
method = _class.getDeclaredMethod("dispatchPointerEvent",
MotionEvent.class);
Log.i("Test", "Method found");
} catch (Exception e) {
Log.i("Test","I failed."+e.getMessage()+e.toString());
//e.printStackTrace();
}
Try this
Class _class = null;
try {
_class = Class.forName("com.android.server.pm.PackageManagerService");
Log.i("Test", "Class found");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
Log.i("Test", "Pre-Method found");
Class _class2 = Class.forName("android.content.pm.PackageParser$Package");
method = _class.getDeclaredMethod("grantPermissionsLPw",
_class2, boolean.class);
Log.i("Test", "Method found");
} catch (Exception e) {
Log.i("Test","I failed."+e.getMessage()+e.toString());
e.printStackTrace();
}
(Sorry but I cannot post comments, so have to post response)
Did you put the right method parameters? Where you have MotionEvent.class.
android.hardware.input.InputManager::injectInputEvent requires the android.permission.INJECT_EVENTS permission which is a system permission not available to apps. If you need to do this you'll have to root the device and sign your app as a system app.