是否可以使用adb协议从Android手机发送短信

时间:2016-01-13 12:39:22

标签: android adb usb4java android-host-api

我想通过USB连接到我的电脑的Android手机发送短信。我在起诉usb4Java库。我已根据链接usb4java-javax-examples访问了手机并将adb命令发送到手机。这是代码和输出。

public static void main(String[] args) throws Exception {
    ....
    AdbDevice device = devices.get(0);
    device.open();
    try {
        // Send the connect message
        Message message = new ConnectMessage(ConnectMessage.SYSTEM_TYPE_HOST, "12345678", "ADB Demo");
        System.out.println("Sending: " + message);
        device.sendMessage(message);
        boolean connected = false;
        while (!connected) {
            message = device.receiveMessage();
            System.out.println("Received: " + message);

            // If connect message has been received then we are finished
            if (message instanceof ConnectMessage) {
                connected = true;
            }
            ....
        } // end of while
        // Open "sync:"
        message = new OpenMessage(1, "sync:");
        System.out.println("Sending: " + message);
        device.sendMessage(message);
        message = device.receiveMessage();
        System.out.println("Received: " + message);
        if (!(message instanceof OkayMessage)) {
            System.err.println("Open failed");
            System.exit(1);
        }
        int remoteId = ((OkayMessage) message).getRemoteId();

        // Close
        message = new CloseMessage(1, remoteId);
        System.out.println("Sending: " + message);
        device.sendMessage(message);
        message = device.receiveMessage();
        System.out.println("Received: " + message);    
    }
}

以下是我收到的输出

Sending: CONNECT(0x01000000, 4096, "host:12345678:ADB Demo")
Received: CONNECT(0x01000000, 4096, "device::")
Sending: OPEN(1, "sync:")
Received: OKAY(1, 1)
Sending: CLOSE(1, 1)
Received: CLOSE(0, 1)

有没有办法使用Adb protpcol向sms发送Sms命令并发送短信。像SENDSMS(int number, String textMessage)一样。

或者还有其他办法吗?

由于

**修改

这里我正在做什么,但我得到了关闭(0,1)。不知道出了什么问题

message = new OpenMessage(1, "shell:command");
System.out.println("Sending: " + message);
device.sendMessage(message);
message = device.receiveMessage();
System.out.println("Received: " + message);
if (!(message instanceof OkayMessage)) {
    System.err.println("Open failed");
    System.exit(1);
}
int remoteId = ((OkayMessage) message).getRemoteId();
int localId = ((OkayMessage) message).getLocalId();

String num = "00923424700007";
String text = "Hello World";

message = new WriteMessage(remoteId, localId, "am start -a android.intent.action.SENDTO -d sms:"+num+" --es sms_body \""+text+"\" --ez exit_on_sent true");
System.out.println("Sending: " + message);
device.sendMessage(message);
message = device.receiveMessage();
System.out.println("Received: " + message);

if (!(message instanceof OkayMessage)) {
    System.err.println("Open failed");
    System.exit(1);
}

remoteId = ((OkayMessage) message).getRemoteId();
localId = ((OkayMessage) message).getLocalId();

我在行

做错了什么
message = new WriteMessage(remoteId, localId, "am start -a android.intent.action.SENDTO -d sms:"+num+" --es sms_body \""+text+"\" --ez exit_on_sent true");

由于

1 个答案:

答案 0 :(得分:2)

试试这个:

adb shell am start -a android.intent.action.SENDTO -d sms:CCXXXXXXXXXX --es sms_body "SMS BODY GOES HERE" --ez exit_on_sent true
adb shell input keyevent 22
adb shell input keyevent 66

请参阅the entire answer by arpz