Can I use socket() on frameworks/native/service on Android? - android

I want to implement socket function in the Android frameworks/native/service/surfaceFlinger for sending screen information.
So, I made my source code about networking by using socket(), and inserted into the Android framework source tree.
However, it couldn't create socket descriptor.
It always returned -1 and errno is 13
So I added permission code like this:
// framework/frameworks/native/services/surfaceflinger/mynetwork.cpp
if (PermissionCache::checkCallingPermission("android.permission.INTERNET") < 0)
...
fd = socket(AF_INET, SOCK_DGRAM, 0);
But still doesn't work.
Any body know about this issue?
and know soution?
Thank you.

Related

Should I get BluetoothGatt.GATT_SUCCESS also when disconnecting from a device?

I am working with custom devices and I am struggling to manage the Bluetooth LE correctly.
My only concern is not getting 0 (BluetoothGatt.GATT_SUCCESS) when I read the status value along with value 2 on newState variable (what means BluetoothProfile.STATE_DISCONNECTED) at method onConnectionStateChange. Instead, I get an 8, what can't be tracked in the BluetoothGatt nor BluetoothProfile classes.
All connection works fine, I read and write values perfectly.
(1) Is this supposed to be like that? Why do I read an eight?
I have seen many status values at my onConnectionStateChange method: 8, 19, 133 etc.
(2) Where can I check this values?
Thanks in advance
EDIT: There are many values in the api.h file, we were looking in the wrong place.
8: 0x08 = GATT CONN TIMEOUT
19: 0x13 = GATT CONN TERMINATE PEER USER
133: 0x85 = GATT_ERROR
The int error codes need to be converted to HEX and mapped to the values in the following file:
https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.1_r13/stack/include/gatt_api.h
In the cases you mentioned:
8 = GATT_INSUF_AUTHORIZATION
19 = GATT_RSP_WRITE
133 = GATT_ERROR
Answer is no.
I wasn't getting BluetoothGatt.GATT_SUCCESS because the connection was closed by the other end. If gatt.disconnect() is executed I read status = BluetoothGatt.GATT_SUCCESS.

java.net.ProtocolException: unexpected end of stream

I am facing a strange issue, and I am not able to debug it out. I have implemented a logic for uploading stream of data and am using Volley for the same, I have customized a logic little bit in HurlStack, addBodyIfExists api,so that body of type "application/octet-stream" can be handled.
My logic is to post progress to user, so that UI can be updated indicating user progress in upload, below my logic for same.
int toRead = length; // File length
byte[] data = new byte[4096];
connection.setDoOutput(true);
if(length != -1) {
connection.setFixedLengthStreamingMode(length);
} else {
connection.setChunkedStreamingMode(4096);
}
OutputStream os;
int i;
int count;
os = connection.getOutputStream();
int progress= 0;
try {
for(i = 0; (count= is.read(data)) > 0; ++i) { // is, is not null and contains a valid input stream
os.write(data, 0, count); // at this line am getting unexpected end of stream
progress+= count;
if(i % 20 == 0) {
rs.deliverProgress(progress, 0L);
progress= 0;
}
}
os.flush();
} finally {
if(is != null) {
is.close();
}
if(os != null) {
os.close();
}
}
on executing above code am getting this, although I have verified, output stream is not null, neither do input stream, it fails in first iteration of read loop itself, am seeing it has read 4096 bytes and then trying to write the same.
java.net.ProtocolException: unexpected end of stream
at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.close(HttpConnection.java:326)
at com.android.okio.RealBufferedSink.close(RealBufferedSink.java:174)
at com.android.okio.RealBufferedSink$1.close(RealBufferedSink.java:142)
any help in debugging above will he highly appreciated.
This may help you :
That exception is thrown by FixedLengthInputStream when the expected number of bytes (usually set in the content-length header of the response) is larger than the actual data in the response.
Check that the content-length header is correct. (If you're supplying your own value for the content length, make sure it is correct.)
It would help to see your code that sets up the input stream.
Already Fixed it, please add "Accept-Encoding", "identity" in header, then the server-side will get command that it will not modify the response, then send back to Clients.
If you have checked everywhere in your code and tried every solution in stackoverflow and github but the issue still occurs, and you have only tested your code on emulator, then, you should try to run your code on your real device instead. Maybe it will work, or maybe it won't, but if you feel desperate, just have a try, seriously. I was astonished when I happened to find that my code ran with bugs on emulator everytime but successfully on my mobile phone. Besides, the code also ran sucessfully on others' android emulators. So I guess there is something wrong in my android studio configuration that I can't find out. I have no idea why this happen, just like we don't know why "Clean Project/Invalidate caches" sometimes works better than any solution.
It is a little strange that your data length might be unknown.
Is it a media file? Or a live stream?
Anyway, I tried to upload my live stream data. And it happened in the same error.
I added this setting to the Connection and solved my problem.
Transfer-Encoding : chunked
("setChunkedStreamingMode" didn't work. I still don't know why.)
This happens for me on android emulator and doesn't happen on my physical android device.
I was doing GET request to flask server running on 0.0.0.0 on my laptop from the android app.
To fix it on the emulator, add the servers ip address in the emulators proxy.
see How to set up Android emulator proxy settings
The exact problem i had was unexpected end of stream retrofit

Android Jwebsocket custom protocol

I am opening a connection setting up a custom protocol like this:
WebSocketSubProtocol d = new WebSocketSubProtocol("MyCustomProto",WebSocketEncoding.TEXT);
mJWC.addSubProtocol(d);
mJWC.open(mURL);
But... Server side, I receive tis in the protocol string
"org.jwebsocket.json MyCustomProto"
How can I remove from the string the "org.jwebsocket.json" ?
I don't wanna do it server side...
Thanks!
I will answer to my own question.
By calling the "addSubProtocol" doesn't seem to be the right solution for couple of reasons:
if you call those 3 lines of code multiple time (if the first time the connection failed for example..) well the the protocol string would be something like
"org.jwebsocket.json MyCustomProto MyCustomProto"
It just keep adding the protocol..
So I found a turn around. Now I don't use that "addSubProtocol" but instead I defined the protocol directly when I create the socket
mJWC = new BaseTokenClient("client||"+code+"||"+name,WebSocketEncoding.TEXT);
Voila.. Now no more "org.jwebsocket.json" anymore

Errno 97 with Android NDK socket programming

I've searched all over google and can't find a solution to this problem:
I'm trying to do some basic socket programming (client/server apps) on my Android device and I'm not having any luck starting the Server app. In my native code, I call bind(...) and I see from the log that it returns -1. When I check errno, the value is 97 (EAFNOSUPPORT), which indicates "Address family not supported by protocol." When I checked the values for sin_family, it was AF_INET, which is IPv4. Is this not supported by the Android NDK? If not, what else could be the cause of this error?
Socket is initialized as:
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
listenAddr is a sockaddr_in struct that's initialized with:
sin_family = AF_INET;
sin_port = htons(9099);
sin_addr.s_addr = INADDR_NONE;
Call to bind:
bindResult = bind(sock, (sockaddr *)listenAddr, sizeof(listenAddr))
Bind result returns -1. Subsequent call to errno returns 97.
Use INADDR_ANY (or a specific interface's address) rather than INADDR_NONE.
INADDR_NONE is an unsigned constant with the same bit pattern as the signed value -1, returned as an indication of error from certain calls.

Android streaming from icecast server get track information

I have a stream from an icecast server downloading, and I can grab the information in the headers by doing the following:
URLConnection cn = new URL(mediaUrl).openConnection();
cn.connect();
int pos=1;
String x;
String y;
while (cn.getHeaderField(pos) != null)
{
x=cn.getHeaderFieldKey(pos);
y = cn.getHeaderField(x);
Log.e(":::::",""+x+" : "+y);
pos++;
}
When I do this all of the headers I receive are shown as:
content-type : audio/mpeg
icy-br : 64
ice-audio-info : ice-samplerate=22050;ice-bitrate=64;ice-channels=2
icy-br : 64
icy-description : RadioStation
icy-genre : Classical, New Age, Ambient
icy-name : RadioStation Example
icy-private : 0
icy-pub : 1
icy-url : http://exampleradio.com
server : Icecast 2.3.2
cache-control : no-cache
However if I open my stream in mplayer I get:
ICY Info: StreamTitle='artist - album - trackname'
and with each time the song is changed, the new track information is sent appearing the same way in mplayer.
In android when I attempt to read the icy-info all I get returned is null. Also how would I go about retrieving the new information from the headers while I am buffering from the stream? Because even if I try to read the header of something I already know exists whilst buffering such as:
Log.e(getClass().getName()," "+cn.getHeaderField("icy-br"));
All I get returned is null.
I hope this makes sense, I can post more code on request.
I realize this question is old, but for others who are facing this challenge, I am using this project: http://code.google.com/p/streamscraper/ to get track information from an icecast stream. I'm using it on android and so far it works as expected.
All you need is to setDataSource() and pass the URL as a String, then you must prepareAsync() and with a mp.setOnPreparedListener(this); or etc. you will get noticed when the MediaPlayer is done buffering, then all you need to do is mp.start(); P.S.: Don't forget to mp.stop, mp.reset and mp.release upon destroying the application. ;) I'm still thinking of a way to read the ICY info... I must either make my own buffering mechanism and write a buffer file (init the MediaPlayer with FileDescriptor) or make a separate connection from time to time to check for ICY info tags and close the connection... Any better ideas anyone?

Categories

Resources