GPRS - Windows Mobile Development and Hacking General

Hi I am very new to C# and compact framework. I have been coding for XDA's using eVB. I want to be able to enable the GPRS mode and also be able to Hangup.
Do I use RasDial? Is this an achievable task for a C# beginer? Could someone please give me some hints as how to achieve this.

Highflyer,
Firstly, why move over to C# when you can achieve what you want using VB.NET. Like yourself, I have ported across an eVB Rasdial app across to .NET CF. Secondly, with GPRS remember that you only pay for the data that is transferred over the connection, so you dont need to worry about the disconnection, but this can be done manually by putting the XDA into FLIGHT MODE. I think you will find another thread covering how to do this programmatically in this forum.
In my app, my XDA is sending and requesting XML packets over HTTP to a web server. To do this I use the HttpWebRequest and HttpWebResponse classes.
Here is a snippet of code
*******************************
Public Function Post(ByVal sURL As String, Optional ByVal strRequest As String = "", Optional ByVal blnRetValExpected As Boolean = False, Optional ByRef strReturned As String = "", Optional ByRef intStatus As Integer = 0) As Boolean
Dim WebReq As HttpWebRequest
Dim WebRes As HttpWebResponse
Dim bOK As Boolean
Try
WebReq = CType(WebRequest.Create(sURL), HttpWebRequest)
If strRequest <> "" Then
Dim encoding As New ASCIIEncoding
Dim byte1 As Byte() = encoding.GetBytes(strRequest)
WebReq.Method = "POST"
WebReq.ContentType = "application/x-www-form-urlencoded"
WebReq.ContentLength = strRequest.Length
Dim newStream As Stream = WebReq.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
newStream.Close()
End If
WebRes = CType(WebReq.GetResponse(), HttpWebResponse)
If WebRes.StatusCode = HttpStatusCode.OK Then
bOK = True
End If
intStatus = WebRes.StatusCode
Catch ex As InvalidOperationException
bOK = False
Catch ex As WebException
bOK = False
Catch ex As ProtocolViolationException
bOK = False
End Try
If bOK And blnRetValExpected Then
If WebRes.ContentLength > 0 Then
Dim sResponse() As Byte
Dim ReceiveStream As Stream = WebRes.GetResponseStream()
Dim encode As encoding = encoding.GetEncoding("utf-8")
Dim ReadStream As New StreamReader(ReceiveStream, encode)
Dim strXML As String = ""
Try
strXML = ReadStream.ReadToEnd()
Catch ex As Exception
strXML = ""
bOK = false
Finally
ReadStream.Close()
End Try
strReturned = strXML
End If
End If
WebRes.Close()
Post = bOK
End Function
*******************************
What you will find with the above code is that there is no code for making the GPRS connection and RasDial is no longer required. This is due to the code being hidden away in the HttpWebRequest class and the GPRS connection will use the current connection if it exists or prompt the user for the connection to use. The prompting can be overcome by setting a defualt GPRS connection in Settings - Connection Manager, and saving the password along with it. HTH.

Tony,
Many thanks for your advice. The included sample code is very useful. The problem is I have to stick with C#, wether I like it or not. However I can follow your code to reporoduce something similar in C#. I have already used Post in eVB, to send data, however I changed over to using sockets (WinSock), since I understand there is lower data over head in using sockets. I appreciate any comments you may have with this choice.
One question, you mentioned that when we connect to GPRS we do not pay for actual connection. Is this right, my understanding was when the device connects to GPRS, some data is used to achieve this connection, thus just establishing a connection to GPRS would cost. I am thinking of the situation when GPRS has to be enabled and disabled hundreds of times a day.
Thanks again, looking forward to hear any comments.

Highflyer,
Regarding a connection charge - you may be right about this depending on the type of contract you are on, but as I mentioned once a connection is established you are only charged for the data that is transferred over the connection, so why not leave the connection open then there will only be the one connection charge. If the connection is lost due to poor signal then the underlying code of the web components will re-establish the connection as and when required. HTH.

Thanks for your advice.
I am still very interested to be able to perform RasDial using C#. As I said I have tried to write the code however due to my lack of experience in using C# the code does not work.
Any Ideas or advice anybody.
Thanks

WebRequest on Magician/XDA Mini?
Hi there!
I´m glad finding .NET developers here ;-)
I have a big, big problem:
On several XDA Mini´s (HTC Magician) the HttpWebRequest or WebRequest methods instantly return with an exception (ConnectFailure), even if Pocket IE or other software can reach the net. I´ve tested both, connection via ActiveSync and GPRS, but it does not work.
Any ideas on this? Is it a known issue, and is there a way to solve it?
Thanky in advance,

Related

GPRS connectivity in EVB

I have an EVB application that connects to GPRS using a library from Sapphire Solutions.
This has worked reasonably well, but doesn't give me a lot of control of what is going on.
What other methods have people tried? Any hints?
here
I also was lookinf for this solution forever and never found it so i wrote the software myself. Here is the dll that you need to connect to the internet through evb it connects to the default connection no problems.
http://xdaconnect.wastedbrains.com
have fun,
Dan Mayer
Thanks for this. I'll have a play.
The stupid thing is, O2 have told me that I should be using their routines. But nobody can tell me what the routines are, how to find them or give me any information about them whatsoever!
Compare and contrast with Vodafone who actually know a thing or two about implementing mobile systems, but don't have a product to implement...
Code:
Public Declare Function AllocPointer Lib "VBPointers" Alias "VB_AllocPointer" (ByVal nSrcSizeInBytes As Long) As Long
Public Declare Function AllocStringPointer Lib "VBPointers" Alias "VB_AllocStringPointer" (ByVal str As String) As Long
Public Declare Function FreePointer Lib "VBPointers" Alias "VB_FreePointer" (ByVal Pointer As Long) As Long
Public Declare Sub SetLongAt Lib "VBPointers" Alias "VB_SetLongAt" (ByVal Pointer As Long, ByVal OffsetInBytes As Long, ByVal Value As Long)
Public Declare Function PhoneMakeCall Lib "Phone" (ByVal CallInfo As Long) As Long
Public Sub Dial(Number_to_dial As String)
CallInfo = AllocPointer(24) ' sizeof (PHONEMAKECALLINFO)
Call SetLongAt(CallInfo, 0, 24) ' cbSize
Call SetLongAt(CallInfo, 4, 1) ' dwFlags = PMCF_DEFAULT
PhoneNumber = AllocStringPointer(Number_to_dial)
Call SetLongAt(CallInfo, 8, PhoneNumber) ' pszDestAddress
PhoneMakeCall (CallInfo)
FreePointer (PhoneNumber)
FreePointer (CallInfo)
End Sub
The xdaconnect dll posted by Dan is pretty good at getting a connection. The problem is that it has to be registered on each device using eVC. My problem is that I want to make a cd so that other people who dont have eVC can install the device. Normally I would do this with dllRegisterServer or by including the file in the manifest file of dll's to be registered when a program is installed. The problem is that this dll wont register alone, an error is returned back that "dllRegsisterServer is not included in the dll".
Can anyone help?
The dll and eVC project file is available for download at http://xdaconnect.wastedbrains.com
Many thanks
Tony

TAPI weird problem with callerid

Hi Guys:
I'm having a weird problem with TAPI, basically I'm developing an app that intercepts incoming calls and then extract the callerid before starting recording the call.
I'm using WM 2005 and the device is an HTC Universal (T-Mobile MDA PRO) with a T-Mobile UK SIM.
All seems to work fine, the event loop receive the message(s) ,first receives an LINE_APPNEWCALL and then LINE_CALLINFO with dwParam1 set to LINECALLINFOSTATE_CALLERID, but when calling lineGetCallinfo to get the CallerID , the callerid flags (dwCallerIDFlags) returns 32 or "Unknow", even when the built-in alert in the phone is showing the caller id!
I killed cprog.exe from the process viewer (the phone didn't rang so the process was in fact killed) but not difference...Any ideas? Any tricks? Below a copy of the loop that I'm using:
void FAR PASCAL lineCallbackFunc(
DWORD hDevice,
DWORD dwMsg,
DWORD dwCallBackInstance,
DWORD dwParam1,
DWORD dwParam2,
DWORD dwParam3)
{
DWORD dwT = dwMsg;
LINECALLINFO *lpCallInfo;
size_t ci_size = sizeof(LINECALLINFO);
TCHAR *buffNum;
long Err;
OutputDebugString(_T("CB\n"));
switch (dwMsg){
case LINE_CALLINFO: {
switch(dwParam1)
{
case LINECALLINFOSTATE_CALLEDID:
{
OutputDebugString(_T("CALLID"));
break;
}
case LINECALLINFOSTATE_CALLERID:
{
OutputDebugString(_T("CALLERID"));
lpCallInfo = (LINECALLINFO *)calloc(ci_size,1);
lpCallInfo->dwTotalSize = ci_size;
Err = lineGetCallInfo((HCALL)hDevice,lpCallInfo);
if((DWORD)lpCallInfo->dwCallerIDFlags & (LINECALLPARTYID_ADDRESS | LINECALLPARTYID_PARTIAL)) {
_tcsncpy(buffNum, (LPTSTR)((BYTE*)lpCallInfo + lpCallInfo->dwCallerIDOffset), 256);
OutputDebugString(buffNum);
}
break;
}
break;
}
case LINE_APPNEWCALL:
{
OutputDebugString(_T("Newcall"));
// HCALL hCall = (HCALL)dwParam2;
break;
}
}
}
}
#define TAPI_API_LOW_VERSION 0x00020000
#define TAPI_API_HIGH_VERSION 0x00020000
#define EXT_API_LOW_VERSION 0x00010000
#define EXT_API_HIGH_VERSION 0x00010000
And here is how the TAPI starts: (in Main):
liep.dwTotalSize = sizeof(liep);
liep.dwOptions = LINEINITIALIZEEXOPTION_USEHIDDENWINDOW ;
if (lineInitializeEx(&hLineApp, 0, lineCallbackFunc, NULL,
&dwNumDevs, &dwAPIVersion, &liep))
{
goto cleanup;
}
OutputDebugString(_T("Init"));
// get the device ID
dwTAPILineDeviceID = GetTSPLineDeviceID(hLineApp, dwNumDevs,
TAPI_API_LOW_VERSION,
TAPI_API_HIGH_VERSION,
CELLTSP_LINENAME_STRING);
// error getting the line device ID?
if (0xffffffff == dwTAPILineDeviceID)
{
goto cleanup;
}
// now try and open the line
if(lineOpen(hLineApp, dwTAPILineDeviceID,
&hLine, dwAPIVersion, 0, 0,
LINECALLPRIVILEGE_MONITOR, dwMediaMode, 0))
{
goto cleanup;
}
// set up ExTAPI
if (lineNegotiateExtVersion(hLineApp, dwTAPILineDeviceID,
dwAPIVersion, EXT_API_LOW_VERSION,
EXT_API_HIGH_VERSION, &dwExtVersion))
{
goto cleanup;
Any help will be really helpfull, thanks.
Check your return value from lineGetCallInfo
check your return value from lineGetCallInfo. It will be LINEERR_STRUCTURETOOSMALL because lpCallInfo points to a buffer that's too small. (LINECALLINFO is a variably sized data structure) If you failed to make it large enough, you can allocate memory of size lpCallInfo->dwNeededSize and call lineGetcallInfo again. (look at documentation for lineGetCallInfo and LINECALLINFO)
Thanks for your reply, I checked the return value and is 0 so the size is fine (in fact dwSizeNeeded is greater than dwTotalSize so that is no problem), however I will change that bit just in case.
The interesting thing is that lpcallinfo structure returns "32" (UNKNOWN) for the dwCallerIDflags, even when the built-in notification program (cprog.exe) shows the callerid with no problem.
I even killled cprog.exe before testing and is the same, I wonder if there is something related with the Service Provider (T-Mobile) or some other setting that is interfering or no standard.
Any tip or suggestion is appreciated and welcome.
Thanks
Instead of testing the flag, test if dwCallerIDSize>0. Thats what I do. I have systematically gone through all values in the LINECALLINFO and most of them are zeroed out by the network regardless of what should be in them so I inclined to not trust it.
Also it could be your timing. Are you checking every time a LINE_CALLINFO/LINECALLINFOSTATE_CALLERID comes in or just once. The caller id is not always available on the first time but on subsequent messages is available.
Also it cant hurt to add extra memory to the LINECALLINFO. Just +1024 to be sure there is space for the caller id (or any other appended data before the caller id). You said dwSizeNeeded > dwTotalSize, that means you do not have enough memory and need to rellocate and call lineGetCallInfo again to refill the LINECALLINFO.
You do not need to kill the phone app (cprog.exe) to get caller id. It is a very bad idea to kill it without good reason. IMO the only valid reason to kill it would be to allow user interaction with your window during this time. There are side affects from killing cprog that can include uncontrollable vibration that requires a reset to stop if you have not prevented it programatically.
dwSizeNeeded is the amount needed to store the structure.
dwSizeTotal is the amount you provided.
if dwSizeNeeded > dwSizeTotal then there isn't enough room.
Are you sure that lineGetCallInfo returned 0?
If any of the dwXXXXSize members of LINECALLINFO struct are non-zero (which seams very likely), then you will need more than sizeof(LINECALLINFO) bytes.
I agree with OdeeanRDeathshead, no need to kill cprog. This will work without that. If I ever want to kill cprog, I usually replace cprog by a program with sleep(60*60*24) in a loop, then reboot. Or I replace it with the program I'm testing and reboot. On my phone, this doesn't seam to cause problems. If I just kill cprog, it will be restarted within a few minutes.
I also just check whether dwCallerIDSize > 0.
good luck
I you don't get it working, I was wondering if you could post your values for dwSizeTotal , dwSizeNeeded, and dwSizeUsed.
Thanks guys, you were totally rigth! The problem was that I was expecting the API (lineGetCallInfo) to return STRUCTURE_TOO_SMALL error and I didn't make any check on dwSizeNeeded.
Funny enough, the API returned no error at all and even the structure had some data on it, but it wasn't big enough, so basically I added a loop to check the Size and reallocate memory until is enough, it works like a charm now.
On a separate note, on outgoing calls I had to introduce a small delay, the CalledID information it doesn't become instantly, by testing in the HTC Universal seems to be slow on outgoing calls, I hope that wont be any trouble there.
Thanks again guys for your help, it saved me a lot of work.
This piece of code works on my iPAQ, but it does NOT work on a Treo 700wx. It retrieves no number on a Treo 700wx. Anybody face the same issue?
Your iPAQ is a pocket pc, and the treo is a smartphone. The smartphone has a two tier security model. ExTAPI is a privileged API, so on a smartphone it can only be run with by a trusted application. The application must be signed with a trusted certificate.
But why is the product specification advertised as "Pocket PC Phone Edition" (see http://www.palm.com/us/products/smartphones/treo700w/specs.html)
The treo 700wx is the US edition? Because the ones that I used in America had Windows Mobile 5.0 Pocket PC edition, however it sounds like a security issue as the guys mentioned above.
Take a look at :
http://msdn.microsoft.com/library/d.../wce51conWindowsMobileDeviceSecurityModel.asp
http://blogs.msdn.com/windowsmobile/archive/2005/11/03/488934.aspx
I signed the app by using the provided privileged certificate in the SDK and it works (in fact I used exTAPI), is worth to try.
Another thing, did you check the CALLERPARTYID flags to ensure that there is a "callerid" after calling getCallInfo?
And one curious thing about the Palms and Verizon, while we developed all in the UK , IMEI numbers are equally formated, in the US for some reason, both Motorola Q and Palm 700w got different formats, same for callerid sometimes, if you are doing any string operation after obtaining the number (like formating, etc,etc) be sure that is not causing any problem.
But my iPAQ is also running WM5 PPC Phone Edition. I believe the code provided above is in fact compatible with many WM5 phone devices, specifically HTC made devices like O2, Dopod, etc.
So is it because Treo 700w has special security level that we need to comply for TAPI to work properly?
Also, commands like lineInitialize, lineOpen, lineGetCallInfo does not return any error. The thing is this... same piece of code, runs on most WM5 PPC, able to retrieve incoming number. But there's just NO incoming number return on a Treo 700w/wx. Any idea?
arsenallad said:
...The treo 700wx is the US edition?...
...Another thing, did you check the CALLERPARTYID flags to ensure that there is a "callerid" after calling getCallInfo?...
Click to expand...
Click to collapse
Yes. Treo 700wx US edition.
No. I use dwCallerIDSize>0.
not sure why it's not working for you, I run simular code on an apache and a iPAQ 6215, works fine. However, I don't think it's a security problem. I was confused by palm calling the treo a smartphone. It has a touch pad, it must be a Pocket PC .
Pocket PC doesn't support the two tier model. Smartphone supports both models. If registry value HKLM\Security\Policies\Policies\0000101b is 0 then the device is two tier otherwise it's one tier. Note 0x101b=4123 so search MSDN for security policy 4123 to find out more.
Plus it's the tapi api that needs to work not exTapi. I don't think any of the tapi functions are privileged.
Just wanted to correct my mistake from yesterday. If you have VS2005 you might want to look at the CallInfo structure in the debuger.
Good luck
WangSN said:
Yes. Treo 700wx US edition.
No. I use dwCallerIDSize>0.
Click to expand...
Click to collapse
dwCallerIDSize it will tell you only the length, if for instance the structure is not properly initialized (as happened to me in this thread) dwCallerIDSize will be zero too, is not a good idea because doesn't give you too much info.
Check in your structure the dwCallerIDFlags member , if returns LINECALLPARTYID_ADDRESS or LINECALLPARTYID_PARTIAL means that callerid is there, now if LINECALLPARTYID_BLOCKED is returned is very likelly that the operator it doesn't have the callerid active or you are calling from a line that protects the "privacy" (whitheld) sometimes is the case, specially in offices that uses PBX.
If LINECALLPARTYID_UNKNOWN is returned, then probably you are having a problem either with the callerid service (the phone does show the callerid when calling?) or somethign to do with your lpcallinfo structure allocation, check that dwTotalSize >= than dwNeeded.
One more thing: It happens to me that after testing using an HP Phone I started testing in a HTC universal phone and it didn't work , particullary in outbound calls, for some reason the LINECALLPARTYID flag returned ADDRESS, but the structure was empty. After trying to discover what was going on, I introduced a small pause in the thread (Sleep) of 2 seconds and worked fine, it seems that hardware can be slow
Good luck.
Nope No luck in getting this to work...
Checked dwCallerIDFlags member, even when value is LINECALLPARTYID_ADDRESS|LINECALLPARTYID_PARTIAL still does not contain the incoming number.
Also tried Sleep for 2 seconds, but still NO number. Before the 2 seconds wait expires, the built-in PPC incoming call dialog already show up WITH caller number.
I'm running out of ideas...
problem solved, post edited...

Toggle WiFi/BT/Phone using .NET code only!!!

Hi people,
A little something primary for .NET developers but native developers can benefit from this too.
Maybe you were working really hard to make google find you a .NET code to toggle radio devices. I know I did and i was unsuccessful.
Maybe you found some sites about ossvcs.dll and undocumented functions for controlling the Radio Devices. But you were just missing that last piece - .NET code that calls them without some intermediate c++ dll.
Your search is over because i give you my .NET CF code that you can use to toggle WiFi/Bluetooth/Phone by using PInvoke of undocumented functions in mentioned ossvcs.dll!
This is a general solution, so it should work on all devices (I tried it on old ipaq 1940, Qtek 8310 and HTC s730)!
DllImports :
[DllImport("ossvcs.dll", EntryPoint = "#276")]
internal static extern int GetWirelessDevices(ref IntPtr pDevices, int dwFlags);
[DllImport("ossvcs.dll", EntryPoint = "#280")]
internal static extern int FreeDevicesList(IntPtr pDevices);
[DllImport("ossvcs.dll", EntryPoint = "#273")]
internal static extern int ChangeRadioState(IntPtr pDevices, int dwState, SAVEACTION sa);
In the attachement you will find code for 3 projects :
- RadioManager (real code is here)
- RadioManagerTester (Forms application for testing)
- RadioMngrCnsl (Console application for users)
If you have any questions feel free to post them here.
If you want to show your support for my work and encourage me to continue posting code like this you can do that by donating through
Just send me a PM and i'll tell you my e-mail address.
Hi!
Thanks for sharing your code... I pitty you didn't do it a week ago , hehehe... I found a really close solution but I've tried your's as it seems better structured . Although the wifi doesn't work on my device for some reason (HTC Herald). By mixing your solution and mine it does work. Here you've the code:
Code:
[DllImport("coredll.dll", SetLastError = true)]
public static extern int DevicePowerNotify(string name, CEDEVICE_POWER_STATE state, int flags);
public void SwitchWifiState()
{
rdm = new RadioDeviceManager();
rdm.Dispose();
}
private void _WiffiToggle(String name)
{
CEDEVICE_POWER_STATE cps = Microsoft.WindowsMobile.Status.SystemState.WiFiStatePowerOn ? 4 : 0;
DevicePowerNotify(name, cps, 1);
}
In case you find some one with the same problem you can give this a try .
Thanks again for your share!!
Hmmm....i was using that solution with DevicePowerNotify but then i had a lot of problem with comm manager. If I turn on device with this i can't turn it off with comm manager. And if i turn it off i can't turn it on with comm manager (on Qtek 8310).
Anyway, does RefreshDevicesList iin RadioDeviceManager class returns RDD structure for your radio device? Can you toggle bluetooth or phone?
amaric said:
Hmmm....i was using that solution with DevicePowerNotify but then i had a lot of problem with comm manager. If I turn on device with this i can't turn it off with comm manager. And if i turn it off i can't turn it on with comm manager (on Qtek 8310).
Anyway, does RefreshDevicesList iin RadioDeviceManager class returns RDD structure for your radio device? Can you toggle bluetooth or phone?
Click to expand...
Click to collapse
On my devie there are no such problems... Strange....
Your code detects my wifi perfectly and toggles BT and Phone... It just doesn't work with Wifi... No error is thrown...
no devices found
hi,
i am quite new to .NET programming... i have been using c++ all this while.. I tried your code on my windows mobile 6.0 professional product.. but unfortunately i keep getting "Devices not found" message..
Is there any issue that i have not taken care of..
thank you very much for any help..
thanks for your code too....
regards,
Senthil.K
wifi
am devloping a WM 6 app in VB.net (2005)
ok i have the bluetooth bit sorted and working fine.
However I can't get the wifi bit working.
It appears not to be able to find the correct device to change the power on
I also can't open the example code as its in a different VS version.
Any ideas?
Thanks
Hello everyone,
sorry for not being so professional, but I figured out that manually changing the following registry keys it makes the job done even though the 'Comm Manager' doesn't realize the wifi has turned off.
Any recommendation not for doing so?
HKEY_LOCAL_MACHINE\System\State\Hardware\Wifi (5 disabled, 23 enabled)
HKEY_LOCAL_MACHINE\Software\Drivers\WLAN\WlanStatus (0 disabled, 1 enabled)
(using HTC Touch HD)
Have a nice day.
Hey amaric,
Thanks a lot for sharing.
It works flawlessly on my Touch Diamond.
I still can't toggle bluetooth with this

[Q] Remote Bluetooth Camera

Hello everyone.
Recently, I've started developing some Android apps, and now I have a task and I don't know where to start So I guess I need some help
Ok, I need to write an app, which connects 2 phones via bluetooth (this part is done), and then, from one phone I should tell the other one to turn on its Camera and start recording video. Also I should be able to stop the recording.
And a bigger task should be able to send the recorded video to the remote phone, or stream it someway...
Is there any code/totorial/example on such a task? Only Starting recording also will be fine for now
Hope someone reads this, I need Your help!!!
In my Bluetooth apps (BlueMuze and Listables), I send little serializable meta objects. I think usually these are callted DTO for "data transfer object" in Java.
I call them something else but it doesnt matter what you call them as long as they are serializable and are exactly the same type of object with the same package name.
So I basically created my own little mini networking stack on top of the regular socket.
So the communication process might go something for this:
Let's say device1 is you control phone, device2 is the camera phone.
-> devices connected
device1 -> are you ready? -> device2
device2 -> yep, ready -> device1
device1 -> turn on camera -> device2
device2-> camera on preparing to return stream -> device 1
device2-> send byte array data for the stream -> device 1
Hopefully that should get the ball rolling and you can say if you need more detail or more basic info or whatever
I'm not really sure where you're starting from but figured I'd toss that out and you can tell me if that's too advanced or too basic or just right or whatever.
Thanks for the reply
Ok, I'm new at android development, so maybe it's gonna be a little tough
Any way. I've got that communication process "plan" in my mind already. Hust don't know how to realize it. As for a start, I'd like to know which are the classes/methods that control camera.
As I'm planning this app over Bluetooth (it's always free you know ), I have already done the connection part (little buggy, but will fix it). So I guess I can do these steps
device1 -> are you ready? -> device2
device2 -> yep, ready -> device1
Click to expand...
Click to collapse
Then goes the Camera thing.
And finally the byte stream... I guess if I manage to record something, I can find some methods for sending byte arrays??? I hope so!
So now my first task is the camera control I guess.
And what about your serializable meta objects and little mini networking stack? Please some more basic explanation on these...
Sure thing, in programming Serialization is basically just the "flattening" of an object to be passed over some data communication channel like Bluetooth/Serial or TCP/IP / Whatever. Basically it takes a full Java Object and compresses it down to it's raw byte equivalent.
To do this in Android/Java you make a class to hold some data and have it implement the Serializeable interface.
Here's an extra simple version of one of mine (you can add more complexity to yours as you need, this is just to demonstrate sending an object over the wire).
Code:
package com.alostpacket.listables.donate.vo;
import java.io.Serializable;
public class BluetoothMetaVO implements Serializable
{
//I made these codes up for myself
public static final int START_TRANSFER = 0;
public static final int TRANSFER_COMPLE = 1;
public static final int TRANSFER_ITEM = 2;
public static final int RESUME_TRANSFER = 3;
public static final int TYPE_PING = 9;
public static final int TYPE_REPLY = 10;
public static final int REPLY_OK = 11;
public static final int PING_OK = 12;
public static final int RECEIPT_OK = 13;
public static final int RECONNECT_PING = 14;
public static final int RECONNECT_REPLY = 15;
//this is the data you would end up sending
public int commandCode;
public int type;
public int numberItems;
public int id;
public BluetoothMetaVO()
{
}
}
To get the bytes you use and ObjectOutputStream and writeObject() probably to a ByteArrayOutputStream. (and later, for larger files a BufferedOutputStream).
If you're using the BluetoothChat example in the Android SDK as your starting point, you can pass the bytes to the write(byes[] b) method.
Then, on the receiving device, you use an ObjectInputStream and readObject to reconstitute the object.
You can then check the values of one of your properties (so for me I would check the commandCode) and act accordingly.
Wow this will really help. I'll start codeing first tjing in the morning
Sent from my HTC Hero using XDA App
Ok, now finally I tried some coding, but have lots of problems all the time. I steted with the BluetoothChatExample. But I think it's too mixed up for my case. So I decided to separate the Server app and the Client app. Let say, I need an app which creates a BT socket and listens to it. When a Client app connects to it, it allows the user to send a message to the client. Depending on the message, the Client should take a picture with it's camera and send it to the Server. When the Server receives a pic, it should store it on SD Card.
Anyone can help? Need some help on Client-Server connection...

File transfer over TCP sockets

I've been trying to get a simple file transfer between desktop app and phone app. The transfer works up to a point, when it simply ...stops dead.
The server(aka desktop client) enters the listening state, and the phone goes idle.
Anyone has any samples on transfers of large file (bigger than 1 MB)?
mcosmin222 said:
I've been trying to get a simple file transfer between desktop app and phone app. The transfer works up to a point, when it simply ...stops dead.
The server(aka desktop client) enters the listening state, and the phone goes idle.
Anyone has any samples on transfers of large file (bigger than 1 MB)?
Click to expand...
Click to collapse
Have you looked through GoodDayToDie's source code for the File Server? I wonder if he has anything in there that could make that work.
snickler said:
Have you looked through GoodDayToDie's source code for the File Server? I wonder if he has anything in there that could make that work.
Click to expand...
Click to collapse
lalz.
Completely forgot about that one xD
Meh he has it written in C++
Apparently, he didn't do anything that I didn't.
mcosmin222 said:
lalz.
Completely forgot about that one xD
Meh he has it written in C++
Click to expand...
Click to collapse
You can still utilize the transfer portion . I was thinking of seeing what I could do with sockets on the phone. I know it could come in handy somehow
snickler said:
You can still utilize the transfer portion . I was thinking of seeing what I could do with sockets on the phone. I know it could come in handy somehow
Click to expand...
Click to collapse
It's a pain in the ***.
It stops transfer at random points.
mcosmin222 said:
It's a pain in the ***.
It stops transfer at random points.
Click to expand...
Click to collapse
That doesn't surprise me at all for some reason.
Did you double-check your socket multithreading code?
I recently had problems with sockets and it turned out that I had the muti-threading thing wrong.
I think you shouldn't use only one connection and fail if it drops ...
ScRePt said:
Did you double-check your socket multithreading code?
I recently had problems with sockets and it turned out that I had the muti-threading thing wrong.
I think you shouldn't use only one connection and fail if it drops ...
Click to expand...
Click to collapse
What do you mean by socket multthreading code? You mean the use of async methods? or having the thread work on background, using the socket?
Take a look to the Tim Laverty's networking samples.
sensboston said:
Take a look to the Tim Laverty's networking samples.
Click to expand...
Click to collapse
That's what im doing (more or less)
@mcosmin222: The most common reason I saw for why that happened was the thread doing the transfer would crash. There's a lot of things that could cause such a crash, but because it's not the main thread or a UI thread, you don't see it. It just stops. In fact, even the debugger usually doesn't catch it (annoying as hell...)
There are a few common things that non-UI threads aren't allowed to do which you might be trying. For example, attempting to show a MessageBox on a non-UI thread will crash the thread (you can do it by adding a lambda or function to the dispatcher for the UI). In any case, feel free to use or adapt my code, or share yours here and if there's an obvious issue I'll point it out. Incidentally, you can set a larger buffer on the socket if you want the operation to complete without looping.
By the way, the only portion of my webserver that's written in C++ is the file I/O code, which I chose to do in C++ rather than .NET because the phone's stunted .NET framework makes it more difficult than I like to access arbitrary file paths. That code is all fairly clean wrappers around the Win32 calls; I suppose I could comment it more but it's very straightforward to read even if you aren't familiar with managed C++. The actual network code is entirely written in C# 4.5. You could actually simplify it a bit for a direct transfer app, too; I wrote it with a lot of multithreading in case I wanted to re-use the code somewhere that might be expected to have more than one client connecting at a time.
GoodDayToDie said:
@mcosmin222: The most common reason I saw for why that happened was the thread doing the transfer would crash. There's a lot of things that could cause such a crash, but because it's not the main thread or a UI thread, you don't see it. It just stops. In fact, even the debugger usually doesn't catch it (annoying as hell...)
There are a few common things that non-UI threads aren't allowed to do which you might be trying. For example, attempting to show a MessageBox on a non-UI thread will crash the thread (you can do it by adding a lambda or function to the dispatcher for the UI). In any case, feel free to use or adapt my code, or share yours here and if there's an obvious issue I'll point it out. Incidentally, you can set a larger buffer on the socket if you want the operation to complete without looping.
By the way, the only portion of my webserver that's written in C++ is the file I/O code, which I chose to do in C++ rather than .NET because the phone's stunted .NET framework makes it more difficult than I like to access arbitrary file paths. That code is all fairly clean wrappers around the Win32 calls; I suppose I could comment it more but it's very straightforward to read even if you aren't familiar with managed C++. The actual network code is entirely written in C# 4.5. You could actually simplify it a bit for a direct transfer app, too; I wrote it with a lot of multithreading in case I wanted to re-use the code somewhere that might be expected to have more than one client connecting at a time.
Click to expand...
Click to collapse
I am aware that some calls from background threads are not allowed, especially those that have to do with the UI thread.
This is the code for the server. It would seem this one is the problem, somewhere...I just can't see where...
I tried limiting the number of packages sent (that's what the timer is all about).
Code:
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousSocketListener
{
// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);
public static string[] TransferStages = new string[] { "sendmetadataz-length", "sendmetadataz", "file-length", "file" };
public static int Index = -1;
public static List<string> FilePaths = new List<string>();
public static long CurrentStreamPosition = 0;
public static FileStream ifs;
static int pocketspersecond = 0;
static bool LimitExceded = false;
DispatcherTimer timer = new DispatcherTimer();
public static int CurrentArraySize = 0;
public static int FileIndex = 0;
public AsynchronousSocketListener()
{
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
}
void timer_Tick(object sender, EventArgs e)
{
LimitExceded = false;
}
public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[StateObject.BufferSize];
// Establish the local endpoint for the socket.
// Note: remember to keep the portnumber updated if you change
// it on here, or on the client
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 13001);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.UTF8.GetString(
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
content.Length, content);
// Respond to the client
Send(handler, content);
}
else
{
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
public static void Send(Socket handler, String data)
{
//handler.SendBufferSize = File.ReadAllBytes(@"D:\MUZICA\Activ - Visez.mp3").Length;
// handler.BeginSendFile(@"D:\MUZICA\Activ - Visez.mp3", new AsyncCallback(SendCallback), handler);
#region cotobatura
data = data.Replace("<EOF>", "");
if (data.Contains("sendmetadataz") && data.Contains("length")==false)
{
data = MainWindow.DataContextModel.Files.ElementAt(FileIndex).ToString()+"<EOF>";
byte[] byteData = Encoding.UTF8.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
else if (data.Contains("sendmetadataz-length"))
{
Index++;
if (Index >= MainWindow.DataContextModel.Files.Count)
{
//FileIndex++;
data = "TransfersComplete<EOF>";
}
data = Encoding.UTF8.GetByteCount((MainWindow.DataContextModel.Files.ElementAt(FileIndex).ToString() + "<EOF>").ToString()).ToString();
byte[] MetaDataLength = Encoding.UTF8.GetBytes(data);
handler.SendBufferSize = MetaDataLength.Length;
handler.BeginSend(MetaDataLength, 0, MetaDataLength.Length, 0, new AsyncCallback(SendCallback), handler);
}
else if (data.Contains("file-length"))
{
ifs = File.Open(MainWindow.DataContextModel.Files.ElementAt(FileIndex).Location, FileMode.Open);
byte[] gugu = Encoding.UTF8.GetBytes(ifs.Length.ToString());
handler.SendBufferSize = gugu.Length;
handler.BeginSend(gugu, 0, gugu.Length, 0, new AsyncCallback(SendCallback), handler);
}
else if (data.Contains("file") && data.Contains("length") == false)
{
//byte[] filedata = File.ReadAllBytes(MainWindow.DataContextModel.Files.ElementAt(FileIndex).Location);
//handler.BeginSend(filedata, 0, filedata.Length, 0,
//new AsyncCallback(SendCallback), handler);
byte[] filedata = new byte[150];
for (int i = 0; i < 150; i++)
{
if (CurrentStreamPosition < ifs.Length)
{
filedata[i] = (byte)ifs.ReadByte();
CurrentStreamPosition++;
CurrentArraySize++;
}
else
{
Array.Resize(ref filedata, CurrentArraySize);
break;
}
CurrentArraySize = 0;
}
// if (pocketspersecond == 25) LimitExceded = true;
//Thread.Sleep(1000);
handler.BeginSend(filedata, 0, filedata.Length, 0, new AsyncCallback(SendCallback), handler);
}
//handler.BeginSendFile(MainWindow.DataContextModel.Files.ElementAt(FileIndex).Location, filedata, null, TransmitFileOptions.ReuseSocket, new AsyncCallback(SendCallback), handler );
// What we want to send back in this application is a game move based on what
// has been received. So we call Play on the GameLogic to give us a move to send back
// data = GameLogic.Play(data);
// Convert the string data to byte data using ASCII encoding.
//byte[] byteData = Encoding.UTF8.GetBytes(data);
// Begin sending the data to the remote device.
//handler.BeginSend(byteData, 0, byteData.Length, 0,
// new AsyncCallback(SendCallback), handler);
#endregion
}
public static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
This is basically modified from the tick tak toe over sockets sample from MSDN.
The only possible call that would affect the UI is the call to the Console, but the code works fine for a while, after it just crashes.
I tried running the whole thing synchronously on the UI thread, the result appears to be the same.
In the Send method, the first 3 stages work (file-legth, metadata, metadata-length) and a few steps in the file stage (which actually sends the file).
AT some point, I assumed the thread was guilty somehow, but I just can't prove it. Running the thing directly on UI thread does not seem to change anything.
If the async method finishes and the socket gets disposed, the thread would "die".
PS: the entire thing is hosted by a WPF application.
Hmm... OK, there are several bugs here. I'm not sure which, if any, are responsible for the problem. I'd be tempted to overuse try-catch-log (for example, on the Send() function) and debug-print statements, but here are some things I can see that could cause a crash or other unexpected failure:
There is no guarantee that "ifs" is instantiated before use. If you for some reason skip the file-length step, the file step will crash with a null pointer exception.
The entire send function is hugely thread-unsafe. For example, if a second request arrives before you're done servicing the first one (which is entirely possible due to where the event gets signaled) then the values of "ifs" and "CurrentStreamPosition" and so on will be unpredictable at any given time. Since CurrentStreamPosition seems to be monotonically increasing, that's probably not going to cause an out-of-bounds exception, but it could cause you to enter a state where the test "if (CurrentStreamPosition < ifs.Length)" always fails.
The line "data = "TransfersComplete<EOF>";" never does anything; the next line (immediately following it) overwrites that variable. If you cared about that string, too bad.
FileIndex never changes; I hope you're only ever sending one file here...
You don't actually check that the number of bytes sent is the number you meant to send (admittedly, it *should* be, but there are cases where it won't be).
The last 150-byte chunk of every file transfer is truncated to the first byte. This is because "CurrentArraySize" is reset to 0 on every iteration of the byte-read loop (why use a byte-read loop?) so whenever "CurrentStreamPosition < ifs.Length" tests false, the "filedata" array will be resized to one byte (or zero if the file is an exact multiple of 150 bytes, which presumeably be correct).
There are probably more, but that's what jumped out at me (well, and some technically correct stylistic issues, like the "... == false" test). Given that your protocol seems to rely on end-of-message flags, I'm guessing that your problem is that since the last part of the file is almost always truncated, that marker is never getting sent. This probably leads to the client concluding that the server will be sending it more data, which it does by sending another "file" request. The server attempts to respond and immedately hits the CurrentStreamPosition < ifs.Length check, fails, goes to the else case, and tries to send a 1-byte packet containing a NULL byte.
Incidentally, does your file transfer protocol really require that the client request each 150-byte chunk one at a time, using a new TCP connection each time? That's... awfully inefficient.
GoodDayToDie said:
Hmm... OK, there are several bugs here. I'm not sure which, if any, are responsible for the problem. I'd be tempted to overuse try-catch-log (for example, on the Send() function) and debug-print statements, but here are some things I can see that could cause a crash or other unexpected failure:
There is no guarantee that "ifs" is instantiated before use. If you for some reason skip the file-length step, the file step will crash with a null pointer exception.
The entire send function is hugely thread-unsafe. For example, if a second request arrives before you're done servicing the first one (which is entirely possible due to where the event gets signaled) then the values of "ifs" and "CurrentStreamPosition" and so on will be unpredictable at any given time. Since CurrentStreamPosition seems to be monotonically increasing, that's probably not going to cause an out-of-bounds exception, but it could cause you to enter a state where the test "if (CurrentStreamPosition < ifs.Length)" always fails.
The line "data = "TransfersComplete<EOF>";" never does anything; the next line (immediately following it) overwrites that variable. If you cared about that string, too bad.
FileIndex never changes; I hope you're only ever sending one file here...
You don't actually check that the number of bytes sent is the number you meant to send (admittedly, it *should* be, but there are cases where it won't be).
The last 150-byte chunk of every file transfer is truncated to the first byte. This is because "CurrentArraySize" is reset to 0 on every iteration of the byte-read loop (why use a byte-read loop?) so whenever "CurrentStreamPosition < ifs.Length" tests false, the "filedata" array will be resized to one byte (or zero if the file is an exact multiple of 150 bytes, which presumeably be correct).
There are probably more, but that's what jumped out at me (well, and some technically correct stylistic issues, like the "... == false" test). Given that your protocol seems to rely on end-of-message flags, I'm guessing that your problem is that since the last part of the file is almost always truncated, that marker is never getting sent. This probably leads to the client concluding that the server will be sending it more data, which it does by sending another "file" request. The server attempts to respond and immedately hits the CurrentStreamPosition < ifs.Length check, fails, goes to the else case, and tries to send a 1-byte packet containing a NULL byte.
Incidentally, does your file transfer protocol really require that the client request each 150-byte chunk one at a time, using a new TCP connection each time? That's... awfully inefficient.
Click to expand...
Click to collapse
I know it is inefficient, but I'm rather new to sockets. I just want it to get working in a "beta stage" then ill optimize it (hance the FileIndex never increasing, the blatant lack of try-catch blocks).
On the client side, once the bytes in the buffer are processed, the server gets another send that to send the following 150 bytes (i use 150 just for the lulz).
So basically, the workfow is as follows:
ask metadata length >server gives the length >client adjusts buffer>ask metadata
ask metdata >server gives metdata>client processes the data>asks file length
ask file length>server gives file length>client adjusts a huge array of bytes in which the file will reside (i know this is horribly inefficient, but at some point i will write directly to a file stream)>asks for the first 150 bytes in the file.
server gets the request, sends 150 bytes to client>client copies the 150 bytes in the array created earlier, the asks for the next 150.
I am using 150 just to make sure the data never splits in more than one buffer.
When the file transfer occurs, a different message is used to signal the end of transfer. Client side counts the bytes it gets, and when it is equal to the file length, it no longer asks 150 bytes.
The whole thing seems to be safe from crashing until it gets to the part where it sends the file. I am aware that in the code i gave you there's some file streams not getting closed, but i've fixed that and the problem still occurs.
Since The debugger won't help me at all, I decided to use a WCF service instead.
mcosmin222 said:
What do you mean by socket multthreading code? You mean the use of async methods? or having the thread work on background, using the socket?
Click to expand...
Click to collapse
I mean worker threads not async.You will always have to have a thread in the background to "accept". once you accept you "read" in a new thread and the parent thread "accepts" again. Accept will freeze the thread.
On the other side, you simply "connect" and "write" in the same thread.
Read and Write is done in a loop via pre-defined buffers syncronously.
But if you want the server to give a response, the above flow is the other way around, and it is then when things get complicated. (server needs to "connect" and client needs to "accept" over a different set of ports and different threads)
Probably if you want to have reliable connection you will need the server to come back with a response "give me more" or sth.
So, trying to assist, it was my guess that drops or stalls could be because the above flow is not implemented properly.
Edit Oh ho, missed a whole new page so I am sorry if the reply is irrelevant now.
I would suggest you use the sync methods of sockets and not callbacks because is super easier to debug. ThreadPool.QueueSth (ctr + space I dont remember how it's called is your friend to handle threads yourself.
And try to separate pure socket handling from domain handling (lengths, metadata, etc). Send some bytes back and forth, clean-up and then move to domain specific things!
Moving the line that resets CurrentArraySize to outside of the for loop may well sove your problem. I'd try that first.
Optimization involves, among other things, removing try blocks. Unoptimized code, when you're trying to just make thigns work, ought to be full of them.
Don't forget that exceptions will not bubble up the call stack across threads. In addition to threads you create yourself, any async callback will happen on a different thread than the one that called the async function. If an uncaught exception occurs, the thread will die. If enough threads die, the program may crash (or at least hang) due to threadpool exhaustion.
GoodDayToDie said:
Moving the line that resets CurrentArraySize to outside of the for loop may well sove your problem. I'd try that first.
Optimization involves, among other things, removing try blocks. Unoptimized code, when you're trying to just make thigns work, ought to be full of them.
Don't forget that exceptions will not bubble up the call stack across threads. In addition to threads you create yourself, any async callback will happen on a different thread than the one that called the async function. If an uncaught exception occurs, the thread will die. If enough threads die, the program may crash (or at least hang) due to threadpool exhaustion.
Click to expand...
Click to collapse
I avoid try-catch in unoptimized code to see where actual exceptions occur (sometime the debugger doesn't break on exception).
Nop still not working.
The thread still appears to be crashing, even wrapped with try catch.
Do you at least know *which* function it's crashing in? Try putting a breakpoint on each function header and then, when one of them is hit, step through the execution until the thread dies. Work backward from there to find the problem.
GoodDayToDie said:
Do you at least know *which* function it's crashing in? Try putting a breakpoint on each function header and then, when one of them is hit, step through the execution until the thread dies. Work backward from there to find the problem.
Click to expand...
Click to collapse
The send function (the one with the case switch) appears to be crashing.
It executes the function then enters the listening stage (does not execute the callback).

Categories

Resources