9.0以后这个方法不能用了
工具类写好了,可以参考
https://download.csdn.net/download/aaron121314/11705363
使用framework中的Phone.java来直接发送AT command。这是个接口,我们要获得这个接口的实现对象。
invokeOemRilRequestStrings(String[] strings, Message response)
1. 获取Phone对象
Class<?> PhoneFactory = null;
try {
PhoneFactory = Class.forName("com.android.internal.telephony.PhoneFactory");
Method[] methods = PhoneFactory.getDeclaredMethods();
Method getDefaultPhone = null;
for (Method method : methods) {
if (method.getName().equals("getDefaultPhone")) {
getDefaultPhone = method;
break;
}
}
if (getDefaultPhone != null) {
getDefaultPhone.setAccessible(true);
mPhone = getDefaultPhone.invoke(null, (Object[]) null);
}
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "init exception : " + e.getMessage());
}
2.获取 invokeOemRilRequestStrings() 方法,执行。
strings 参数中的一个strings[0] 必须是"RUN_ATC" string[1]是要执行的AT command
response 是执行函数的返回结果 AsyncResult 也是隐藏的,所以要用反射,这里的message.obj可以直接获取到AsyncResult对象,所以直接用就可以了。
/**
* Invokes RIL_REQUEST_OEM_HOOK_Strings on RIL implementation.
*
* @param strings The strings to make available as the request data.
* @param response <strong>On success</strong>, "response" bytes is
* made available as:
* (String[])(((AsyncResult)response.obj).result).
* <strong>On failure</strong>,
* (((AsyncResult)response.obj).result) == null and
* (((AsyncResult)response.obj).exception) being an instance of
* com.android.internal.telephony.gsm.CommandException
*
* @see #invokeOemRilRequestStrings(java.lang.String[], android.os.Message)
*/
public void invokeOemRilRequestStrings(String[] strings, Message response) {
mCi.invokeOemRilRequestStrings(strings, response);
}
private void sendATcommand(String cmd) {
if (mPhone == null) {
Log.i(TAG, "mPhone null");
return;
}
Method invokeOemRilRequestStrings = null;
try {
String command[] = {"RUN_ATC", cmd};
Class[] param = new Class[2];
param[0] = String[].class;
param[1] = Message.class;
Class[] p = new Class[]{String[].class, Message.class};
for (Method method : mPhone.getClass().getMethods()) {
//Log.i(TAG,"name "+method.getName());
if (method.getName().equals("invokeOemRilRequestStrings")) {
invokeOemRilRequestStrings = method;
}
}
//invokeOemRilRequestStrings = mPhone.getClass().getDeclaredMethod("invokeOemRilRequestStrings", p);
invokeOemRilRequestStrings.setAccessible(true);
invokeOemRilRequestStrings.invoke(mPhone, command, mResponseHandler.obtainMessage(1));
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "send AT command exception : " + e.getMessage());
}
}
private Handler mResponseHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
/* Field[] fields=msg.obj.getClass().getDeclaredFields();
for (Field f:fields){
Log.i(TAG,"fields : "+f.getName());
}*/
try {
Field exceptionField = msg.obj.getClass().getField("exception");
Exception exception= (Exception) exceptionField.get(msg.obj);
if (exception != null) {
Log.i(TAG, " result message exception : "+exception);
mResultTextView.append(exception.toString());
mResultTextView.append("\n");
break;
}
Field result = msg.obj.getClass().getField("result");
String[] results = (String[]) result.get(msg.obj);
for (String s : results) {
Log.i(TAG, "result : " + s);
mResultTextView.append(s);
}
mResultTextView.append("\n");
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, " receive message exception : "+e.getMessage());
}
break;
default:
break;
}
}
};
3.权限问题
一定要用phone进程和sharedUserID,还要sign后才能运行。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.at_command" android:sharedUserId="android.uid.phone">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"
android:process="com.android.phone">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>