Android AT command

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>

 

 

 

 

 

 

Android 中获取 AT 命令的响应可以通过串口通信实现。以下是一个简单的示例代码: ```java public class ATCommand { private static final String TAG = "ATCommand"; private SerialPort mSerialPort; private OutputStream mOutputStream; private InputStream mInputStream; private ReadThread mReadThread; public ATCommand(String device, int baudrate) throws IOException { mSerialPort = new SerialPort(new File(device), baudrate, 0); mOutputStream = mSerialPort.getOutputStream(); mInputStream = mSerialPort.getInputStream(); mReadThread = new ReadThread(); mReadThread.start(); } public void send(String command) { try { mOutputStream.write((command + "\r\n").getBytes()); } catch (IOException e) { e.printStackTrace(); } } private class ReadThread extends Thread { @Override public void run() { while (!isInterrupted()) { int size; try { byte[] buffer = new byte[64]; if (mInputStream == null) return; size = mInputStream.read(buffer); if (size > 0) { String response = new String(buffer, 0, size); Log.d(TAG, "Response: " + response); } } catch (IOException e) { e.printStackTrace(); return; } } } } } ``` 使用上述代码,你可以通过以下方式来获取 AT 命令的响应: ```java ATCommand atCommand = new ATCommand("/dev/ttyS0", 115200); atCommand.send("AT"); ``` 在上述代码中,我们通过 `mOutputStream.write()` 方法将 AT 命令发送给串口设备,并通过 `mInputStream.read()` 方法读取响应结果。最后,我们将响应结果输出到日志中。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值