How to control the handset using AT commands in java - java

I know that by using AT commands we can control the handset.As example unlocking screen we can give a specific AT command or moving right to the menu or left or bottom or up we can give specific AT commands. What all are the AT commands for doing this kind of control.
Thank you.

From what I understand, the AT commands are more used for phone-type functions (making calls, or sending SMS, etc), rather than menu navigation, etc.
I'm not entirely sure if that was your end-goal after menu navigation, but you can find more details here: http://en.wikipedia.org/wiki/Hayes_command_set (the original +AT command set)
If you wanted to send SMS from a handset connected to your computer you might want to take a peek at this page: http://www.developershome.com/sms/atCommandsIntro.asp
If you wanted more control when performing functions, like sending SMS, etc, you might want to investigate "PDU Mode."
It is entirely possible that some handset manufacturers may have implemented additional +AT commands to allow other functions to be performed, so you might do better by specifically searching for the commands related to the handset you are using.
(Of course, if you're having issues connecting to the handset hardware itself, you need to ensure you have either the javax.comm extension or some favoured Java USB API installed)
If post doesn't help, perhaps you could provide more details in your question? (eg. what you are ultimately trying to do, if you think it would help)

List of AT commands
sample java code to use AT command
public void servicesDiscovered(int transID, ServiceRecord serviceRecord[])
{
String url = serviceRecord[0].getConnectionURL(1, false);
try
{
//ClientSession conn= (ClientSession)Connector.open(url);
StreamConnection meineVerbindung = (StreamConnection) Connector.open(url);
if(conn== null)
System.out.println("Kann Service URL nicht oeffnen\n");
else
{
OutputStream out = conn.openOutputStream();
InputStream in = conn.openInputStream();
String message = "AT+CGMI\r\n";
// send AT-command
System.out.println("send AT Comand request: "+message);
out.write(message.getBytes());
out.flush();
out.close();
byte buffer[] = new byte[10000];
// read the response from mobile phone
in.read(buffer);
System.out.println("AT Comand response: "+buffer.toString());}
}
catch(IOException e)
{
System.out.println("Service Error(3): "+e.getMessage());
}
}

Related

Using java to open a web browser on another computer

So, this is a rather unusual question, and I can't find anything else anywhere which has been helpful on how to do this, or if its even possible to do so.
I'm working on a game server wrote in java, and I'm trying to get the users default web browser to open to a specific link, when a command is typed into the chat box and sent to the server.
The current Issue I have is, when a user issues the command, it opens the browser on the host system, and not the players system.
I haven't been able to try any other methods, as I am unable to find any information regarding my specific situation!
#CommandHandlerMethod(accessLevel = EAccessLevel.USER)
public static Object[] vote(final Player player, final String... params) {
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("www.example.com");
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
return AbstractCommandHandler.getAcceptResult("");
}
What I was hoping for via this code, was to open the web browser on the players system to allow them to view a specific webpage, but this has not been the case, and opens it on the server host system.

How to programmatically connect internet via datacard with AT commands?

I have a datacard ZTE MF190. I want to use AT commands to register in 2G or 3G and access internet via datacard. Found this article about how to make data call:
AT+cgatt=1
AT+CGDCONT=1,”IP”,”epc.tmobile.com” //I used my operator PDP context
AT+CGACT=1,1
But ping from OS terminal shows 100% package loss.
I've tried on Ubuntu 14 and Windows 7.
How can I connect internet with AT commands using datacard on Ubuntu?
UPDATE
I gave bounty to #tripleee's answer because it's more full than first one and answered all my questions. But I'm not satisfied with answers, so I'll answer my own question in a week.
In my answer I'll show how to handle this process with Java. So, please do not move this question to other Stack Exchange websites.
Creating a connection between the card and your provider is not sufficient. You need some mechanism for creating a network interface out of this connection, and set up your network stack to route packets over this interface.
Traditionally, the pppd daemon has been a popular choice for this task. You would create a "chat script" with the commands for establishing a data call (these days, pppd might come packaged with a suitable canned script) and the daemon would handle the entire process of placing the call, authenticating, setting up a network interface over the circuit, and configuring the system to route packets over it, as well as configuring DNS etc to use it for resolver queries, etc.
I tried to sniff USB port but on this case dashboard can not connect because of busy port
It is certainly possible. See this question
Found this article about how to make data call
What that article is about is how to set up the call, not how to make it.
After you made correct setup, connect to internet with this command:
ATD*99***1#
UPDATE1: After a bit of research I believe that article was written only to promote their software and has no practical use. In reality dialing is made with pppd or wvdial
UPDATE2: We discussed ways to solve the problem in a chat room (in Russian). It turned out cnetworkmanager will be the way to go
As far as I know wvdial uses ppp daemon to connect to the internet using modem. wvdial is preinstalled on desktop version of Ubuntu.
wvdial uses a config file located /etc/wvdial.conf. Let's edit this file. Type in your terminal
sudo nano /etc/wvdial.conf
and you will see something like this
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2
Stupid Mode = yes
ISDN = 0
Modem Type = Analog Modem
New PPPD = yes
Phone = *99#
Modem = /dev/ttyUSB2
Username = ''
Password = ''
Baud = 9600
Dial Timeout = 30
Dial Attempts = 3
Explanation of all keys you can find in wvdial.conf(5) - Linux man page. If you need to change your provider dial number, username, password or any other information about connection and device you can change file content and save it.
There are 3 serial ports for ZTE MF190. Normally it's ttyUSB0, ttyUSB1 and ttyUSB2. And in my case ttyUSB2 is for internet connection. It would not work on other ports. So you need to find the right serial port for your modem.
There is an automatic configurator which edits wvdial.conf file, sets serial port baud rate etc. Since it is not always configure correctly I would not recommend to use it:
sudo wvdialconf /etc/wvdial.conf
It would be better if you configure wvdial manually.
Now, when your device connected and wvdial configured to work with device, you can execute this line from terminal:
wvdial
You will see a lot of lines. But if you see those lines - you have succeeded.
local IP address XX.XX.XX.XX
remote IP address XX.XX.XX.XX
primary DNS address XX.XX.XX.XX
secondary DNS address XX.XX.XX.XX
Now, how we can use it in programming? I'll provide some code to work with it on Java. You can use this code to dial.
public int dialer() {
// status for debug. If status == 4 then you connected successfully
int status;
// create process of wvdial
ProcessBuilder builder = new ProcessBuilder("wvdial");
try {
// start wvdial
final Process process = builder.start();
// wvdial listener thread
final Thread ioThread = new Thread() {
#Override
public void run() {
try {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
// wvdial output line
String line;
while ((line = reader.readLine()) != null) {
// if "local IP address" line detected set status 1
if (line.contains("local IP address")) {
status = 1;
}
if (line.contains("remote IP address")) {
status = 2;
}
if (line.contains("primary DNS address")) {
status = 3;
}
if (line.contains("secondary DNS address")) {
status = 4;
}
}
reader.close();
} catch (final Exception e) {
}
}
};
// start listener
ioThread.start();
// wait 6 secs and return status. Some kind of timeout
Thread.sleep(6000);
} catch (Exception e) {
}
return status;
}
And here is a disconnector method. All you need is to kill wvdial process and thread will be destroyed:
public boolean disconnect() {
ProcessBuilder builder = new ProcessBuilder("pkill", "wvdial");
try {
builder.start();
return true;
} catch (IOException e) {
return false;
}
}

Checking if a Twitch.tv stream is online and receive viewer counts using Pircbot

I'm working on my IRC Bot for Twitch.tv (pircbot API) and want to make the bot connect automatically to the stream's chat as soon the stream is going live.
I've found a Twitch API (here), but I have no clue
how I can implement this into my bot since I'm pretty new to Java.
Would be cool if someone could tell me any hints on how I could retrieve if a stream is online and how I can check the amount of viewers watching right now.
Greetings
I realize that this question was posted about a year ago, but I feel its important to have it answered just in case others stumble on this post. What Cam.Stokes said is spot on. It's the answer to the question. However the questioner indicated he is new to Java so I want to use code snippets to work out what Cam.Stokes said.
Catching if a stream is live isn't too difficult. The following code is a snippet from my bot. I got a thread that periodically receives the JSON data from the twitch API and then loads it into a JSON object. The JSON library that I'm using is called "minimal-json". Excellent lightweight lib if you ask me. When you review the code you can see what I'm doing; call twitch-api, get the JSON from the target stream, see if the "stream" object is filled with data. if so, stream is live, otherwise, not live.
import com.eclipsesource.json.JsonObject; // minimal-json specific
private static String TWITCH_STREAM = "https://api.twitch.tv/kraken/streams/$c$";
private static String insertChannel(String url, String channel)
{
return url.replace("$c$", channel );
}
public boolean isStreamLive()
{
try
{
URL url = new URL( insertChannel(TWITCH_STREAM, targetChannel) );
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ));
String inputLine = br.readLine();
br.close();
JsonObject jsonObj = JsonObject.readFrom(inputLine);
return ( jsonObj.get("stream").isNull() )?false:true;
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
Connecting the bot is rather straight forward with PircBot. Answered by Nicolai. Make sure you have an Oauth key from twitch that's linked to your bot account. The target channel is in lower case and with a # in front. And you should be good to go.
Good luck.
you need to get a auth key first
Link: http://twitchapps.com/tmi/
and then you need to set this in the main java file
bot.connect("irc.twitch.tv", 6667, "oauth:YOURAUTHKEY");
bot.joinChannel("#YOURCHANNEL-INLOWERCASE");
let me know if you have some other problems
Check out the chat section of the twitch API to see how to connect to the chat using PircBot:
https://github.com/justintv/Twitch-API/blob/master/IRC.md
As far as getting the live stream status, that is a little bit more difficult, but the url you want is:
https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streamschannel
My suggestion would be to set a timed thread to check the stream status every 10 seconds or so, and if the "stream: " object in the json is not null, making the connection.
A good lib for parsing json (if you are doing twitch stuff, you will need to parse lots of json backwards and forwards by the looks) is gson.
(I need more than 10 rep to post more than 2 links apparently, but copy paste this without the space http:// code.google.com/p/google-gson/ )

String serial communication

I'm just learning Java for about a couple weeks now. My end goal is to have my Arduino measure some sensors and send the results to my android via USB cable. However I'm just trying to get my Arduino to communicate with the Java console on my computer first, and then I figure it's pretty much copy and paste from there.
I can get my Arduino and Java console to communicate with each other as long as it's a simple byte. Anything passed that is giving me a result of the quadratic equation exploding.
In my Arduino serial Monitor I get this:
Engine Temp:100
Air Temp:95
Engine Speed:10000 RPM
Wheel Speed:30 MPH
and in my Java console I get this:
Normal println
[B#6f5f6479
String
€˜3Àf`Ì󀘀
UTF
??3?f`??
Here is my reader code:
#Override
public void serialEvent(SerialPortEvent arg0) {
byte[] readBuffer = new byte[1000];
try {
int availableBytes = input.available();
if (availableBytes > 0) {
// Read the serial port
input.read(readBuffer, 0, availableBytes);
// Print it out
System.out.println("Normal println");
System.out.println(readBuffer);
System.out.println("\nString");
System.out.println(new String(readBuffer, 0, availableBytes));
System.out.println("\nUTF");
System.out.println(new String(readBuffer,"UTF-8"));
System.out.println("\nDone");
}}
catch (IOException e) {
}
}
input is my InputStream.
Ensure that the Arduino and the serial port on the android are at the same speed. The provided code does NOT show how the serial ports are defined and opened/started so its impossible to know if this is the problem.
The characters displayed at the android appear like the typical garbage seen when the two end-points of a serial line are not similarly configured.
It looks like the Arduino is correctly sensing the engine's dynamics?

How can I make a java applet connect with a server?

Please excuse my noobishness as I am teaching myself Java and don't know a lot.
I'm trying to make a multiplayer game that runs from Java applets, I have a server-side program working that will accept strings of text, but all my attempts to find code for applets have failed.
My best attempt looks like it works but I think fails to connect to the server, any ideas why? (localIP is my correct IP and works fine in other tests)
public void init()
{
try
{
socket = new Socket(localIP, 5555);
inStream = new DataInputStream(socket.getInputStream());
outStream = new PrintStream(socket.getOutputStream());
}
catch(Exception e)
{
never reached
}
}
I don't mind scrapping this if someone can tell me a better way to do it or any way at all.
a java applet can only connect to the server from which it was downloaded. if you are not loading the applet from localIP, then you will not be able to connect to it.
you may be able to get around this restriction by signing the applet.
Given that you are not using the Http Protocol, One assumes that the applet is loaded from another port other than 5555. If this is the case, the applet needs to be signed in order to do this functionality.

Categories

Resources