Reputation: 8606
I want to send SMS from android phone connecting to my computer using USB. I am suing usb4Java library. I have accessed the phone and send adb commands to phone according to the link usb4java-javax-examples. Here is the code and output.
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);
}
}
Here is the output that I received
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)
Is there any way that I send Sms command to android phone using Adb protpcol and sms has been send. Like SENDSMS(int number, String textMessage)
.
Or is there any other way to do it ?
Thanks
**Edit
Here What I am doing but I am getting CLOSE(0, 1). Don't know what is doing wrong
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();
What I am doing wrong at line
message = new WriteMessage(remoteId, localId, "am start -a android.intent.action.SENDTO -d sms:"+num+" --es sms_body \""+text+"\" --ez exit_on_sent true");
Thanks
Upvotes: 1
Views: 5059
Reputation: 2671
Try with this :
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
See the entire answer by arpz.
Upvotes: 2