GLBasic forum

Codesnippets => Userlibs [ *.gbal] => Topic started by: trucidare on 2010-Apr-05

Title: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2010-Apr-05
Hi There,

here is some Bluetooth Multiplayer blah

Add these lines into your Project.
Code: (glbasic) [Select]
IMPORT "C" void GLB_iBT_Init()
IMPORT "C" void GLB_iBT_Start(const char* sessID)
IMPORT "C" int GLB_iBT_Send(const char* strValue)
IMPORT "C" const char* GLB_iBT_Recv()
IMPORT "C" int GLB_iBT_Connected()

use this functions in your game. Init at start of your Program, Start opens the Bluetooth search dialog.

to compile add attached lib and GameKit.framework to your xcode project.



[attachment deleted by admin]
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Marmor on 2010-Apr-05
great ! :happy:
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: bigsofty on 2010-Apr-05
Another fine release, well done Truci!  :good:

Can anyone recommend a good link on Bluetooth networking? Its completely new to me.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: fjsantosb on 2010-Apr-05
Hi there,

As usual another great work coming from trucidare!

Thanks for sharing!

King Regards, fjsantos.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: codegit on 2010-Apr-06
Trucidare iz da man  8)
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-06
Thats brilliant. Nice one! Got a simple effort working no worries. A couple of questions though:-

1. How do you close a connection so you can start a new one with a different device? I tried using the init function again, which works but I end up with multiple versions of the same device on the menu list. Tried the start function and it picks up the old connection straight away. Can't see how to stop the connection to search for a new one (without using init again)
2. Is it possible to allow more than two players? I have heard elsewhere of certain games (eg. Guitar Hero) using 4 player for example. Does this current lib handle it, or is it only for 2 players?

Thanks for the library, brilliant stuff!

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-06
i dont know if the new gamekit allows more than 2 players - i will have a look in the next days. i think closing and create a new connection should be no problem
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Moebius on 2011-Feb-07
First of all, brilliantly done trucidare!  I know this is a bit late, but I only noticed this thread and library recently  :whistle:

It's been a long time since this was released, but I have found two problems in my testing:

1)  It seems that after a while and many sends and receives, the app will crash.  The best I've been able to get out of the use of GDB is the error  'EXC_BAD_ACCESS', which seems to happen during either the receive or send function - I'm unsure of which ATM (in other words I forgot which one sry).

2)  After the devices go out of range, or disconnect for some other reason, if the app is left open, the flag that remembers whether the device is 'busy' or not seems to be left set, and it cannot connect with any other devices due to this.

Apart from these problems, everything seems to work outstandingly well.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-07
Thanks Trucidare, that would be great!

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Moebius on 2011-Feb-07
Okay apparently, at some point in the receive function the call:
[CFString _fastCharacterContents]
is made to a deallocated instance.
More specifically, the function listed on the stack in the debugger is "-[iBluetooth _GLB_iBT_Recv]".

I can't offer anything more than this, and the assembly code that the program stops on (I doubt that would help much but ask if you want it), as I don't have the actual code...
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-08
ok i will have a look, sounds like the var to store informations to receive are broken after some readings.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Moebius on 2011-Feb-08
okay thanks!
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-08
hmm i´m thinking about a xcode template for writing external libs.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-08
Really? With also a bit of a tutorial on how to create our own XCode libs that link to GLB then it would surely expand much quicker. I mean you do amazing stuff, but there is only so much one man can do!

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-11
Yeah, I can get one of the devices to crash out after a while too. Don't know why it happens just after a few minutes of knocking data back and fore one of the devices just dies off.

Any news on the closing a link down? Would totally clean mine up if I could close an existing link down and effectively start again without calling init.

Admittingly I don't know much about bluetooth communication, but I send an update message once a second, and try to retreive a message every single loop incase I miss anything. Do I need to worry about that, or will it stay on a queue and when I next receive it will pick up the last one? What I am getting at the minute is now and again a move is missed by the receiver. Before I start debugging more (for example I am 99% sure it should always send a move to the other device when a move is made, but perhaps it's not for some reason) then have you any better advice on how to use this library in the middle of a game loop? My game is fairly simple, and knowing of a move once a second or even longer is fine.

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-19
Here the source of the Project. Added close function.



[attachment deleted by admin]
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-19
Nice one mate! I'll try it out tomorrow. Did you find why it dies off after a while?

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-20
sorry no, didnt have enough time to check and debug, i think its a overflow in recv function.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-20
Just trying to implement the new .a file. So I overwrote the original .a file with the one held in the latest release, and then added the extra close function in GLB like this:-
Code: (glbasic) [Select]
?IFDEF IPHONE
IMPORT "C" void GLB_iBT_Init()
IMPORT "C" void GLB_iBT_Start(const char* sessID)
IMPORT "C" int GLB_iBT_Send(const char* strValue)
IMPORT "C" const char* GLB_iBT_Recv()
IMPORT "C" int GLB_iBT_Connected()
IMPORT "C" void GLB_iBT_Close()
?ELSE
FUNCTION GLB_iBT_Init:
ENDFUNCTION

FUNCTION GLB_iBT_Start: iBTID$
ENDFUNCTION

FUNCTION GLB_iBT_Send: iBTMessage$
ENDFUNCTION

FUNCTION GLB_iBT_Recv:
ENDFUNCTION

FUNCTION GLB_iBT_Connected:
ENDFUNCTION

FUNCTION GLB_iBT_Close:
ENDFUNCTION
?ENDIF

And then I reference GLB_iBT_Close in my GLB code, but in Xcode I am getting an error saying Symbol(s) not found for _GLB_iBT_Close. Where am I going wrong?

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-20
uhm oh check the extern "C" functions i think the alcoholic drinks made me making a typo.

// EDIT:// or you forgot to replace the iBluetooth.a try again to overwrite
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-20
Ok. Originally I copied the function name from a C header file I found. I will try again a little later. It should definitely be using the correct .a file although I will double check. Out of interest is it the .a file from the release directory or the debug directory? I assumed release.

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-20
check the creation date.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-20
It says today. I removed the .a file from the Mac, and made sure the .a file from the zip file was used instead. But I still get the same error messages about symbol(s) not found for _GLB_iBT_Close.

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-21
uhm i think its because i compiled for armv6 only??? could anyone check?
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Kitty Hello on 2011-Feb-21
Try this.
Beware, when 2 string arrive before you try to receive data, you only get the latest received string - no queue is established, yet.

[attachment deleted by admin]
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-21
Rats going out tonight. Will try tomorrow. Is it the .a file in the release directory or debug directory?

No worries, that should be fine with the way I've coded it already.

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Kitty Hello on 2011-Feb-21
don't use the .a library. Use the .mm source code file.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-21
Ah, now you are losing me slightly. I have only got to the using .a files in XCode stage of things. So far I have incorporated Trucidare's .a file he originally posted at the start of this thread, and it is working pretty well (2 idevices talk to each other pretty nicely apart from the mentioned lack of closing ability and that it crashes after a while). As far as .mm files go then you have lost me. So I take it I remove the use of Trucidare's .a file from XCode? But then what do I do with the .mm file?

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-21
choose the .a file or .mm file if you wont provide source code of bluetooth wrapper choose the .a both have the same result.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Kitty Hello on 2011-Feb-22
no - I did not compile the .mm into a .a file. If you try my fix, you must use hte .mm file.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-25
Finally got round to testing! Ok I must be doing something wrong. I took the .mm and .h files out of your zip file and put them in the classes directory (assume this from iAds thread) on XCode. I then completely removed the .a file from XCode and any reference to it. GLB code stayed the same but when I hit compile I get these errors (see attached image).

Obviously I'm doing something wrong here, but with my extremely limited knowledge of XCode (getting the .a to work in the first place was about the limit of what I know at the minute) I don't know where to go from here.

Cheers

Ps. What is even weirder is that I removed the bluetooth code from GLB but it still errored in XCode with references to the Bluetooth functions. Turns out that the libs I normally scp over (/XCode/GLBasic/Lib) are not being updated (specifically libPROGRAM.a). Infact libPROGRAM.a is now being updated in /XCode/Lib. Copy that manually over and it works again. Has the path changed then or something? Is the XCode/GLBasic directory now not needed, and everything is just in XCode?

[attachment deleted by admin]
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Moebius on 2011-Feb-26
Quote
Is the XCode/GLBasic directory now not needed, and everything is just in XCode?
That seems to be exactly what has happened.  I remember someone posting that the extra subfolder wasn't really necessary.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-26
#import <GameKit/GameKit.h>
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Crivens on 2011-Feb-27
Ok I sorted out all my folders and the like. Hmm, that's weird. My XCode project doesn't allow me anymore to add the existing ibluetooth.h file into the classes folder (like I have done for iAds with the AppViewController.h file), only the .mm file.... hmm, that will be a problem right? Also with the #import line you posted where does that go exactly? Is it in the .h file or the .mm file?

Cheers
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Feb-27
right under Foundation/Foundation.h

if xcode strikes to add the file - the file exists in project folder. delete it or add it and uncheck the copy button.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: matchy on 2011-Mar-06
Works well!  :good: :good:
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: trucidare on 2011-Mar-06
nice demo
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Leginus on 2011-Mar-06
nice one!  :good:
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Moebius on 2011-Mar-10
I couldn't compile the iBT code - I had to remove the "self"s from in front of iGotRecvData and connectState.  It seems to be working now though - with the exception of the "EXC_BAD_ACCESS" problem - although it takes a bit of time of constant messaging between the devices to invoke this...
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Moebius on 2011-Mar-10
Now that we have the source code, the debugger highlights:
strcpy(gStrRev, [strTemp UTF8String]);

...in this block of code for the internal receive function:

Code: (glbasic) [Select]
// GF: use global string buffer
char gStrRev[2048];
const char* GLB_iBT_Recv() {
NSString* strTemp = [myBluetooth _GLB_iBT_Recv];

gStrRev[0]='\0';
strcpy(gStrRev, [strTemp UTF8String]);
prevPost = strTemp;
return gStrRev;
}

when there is the memory access problem.

Specifically, it says that 'strTemp' is an invalid CFStringRef when I hover over it...
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: matchy on 2011-Mar-10
The connection call pulses are controlled by the iOS and during debugging the connections are slow but usually, with the devices alone, connect quickly.

There were a couple of typos as mentioned although no big deal. The only time I got EXC_BAD_ACCESS is when trying to send data when not connected or something like that.

Although now it works fine and here is a non-tested GLB code sample of what I have so far:
Code: (glbasic) [Select]

TYPE _data
connected
connected_last
data_recv$
data_send$
session_id$
peer_id$
self_id$
master
toggle
time
ENDTYPE

GLOBAL tooth AS _data

bluetooth_main()

FUNCTION bluetooth_main:
LOCAL time$

tooth.session_id$ = "01"
GLB_iBT_Init()
GLB_iBT_Start(tooth.session_id$)
tooth.connected = GLB_iBT_Connected()
WHILE NOT tooth.connected
PRINT "Connecting", 1, 1
SHOWSCREEN
tooth.connected = GLB_iBT_Connected()
WEND
tooth.peer_id$ = GLB_iBT_PeerID()
tooth.data_send$ = "PEERID:" + tooth.peer_id$
GLB_iBT_Send(tooth.data_send$)
time$ = MID$(PLATFORMINFO$("TIME"),11,8)
WHILE tooth.connected
tooth.data_recv$ = GLB_iBT_Recv()
tooth.data_send$ = "TIME:" + time$
GLB_iBT_Send(tooth.data_send$)
recv_data()
PRINT "LOCAL TIME:" + time$, 1, 1
PRINT "REMOTE TIME:" + tooth.time$, 1, 20
SHOWSCREEN
WEND
GLB_iBT_Close()
ENDFUNCTION

FUNCTION recv_data:
LOCAL match$

tooth.connected = GLB_iBT_Connected()
IF tooth.connected
tooth.data_recv$ = GLB_iBT_Recv()
IF tooth.data_recv$ <> "NO_DATA"
match$ = left_match$(tooth.data_recv$, "PEERID:")
IF match$ <> ""
tooth.self_id$ = match$
IF tooth.self_id$ < tooth.peer_id$
tooth.master = MASTER_LOCAL
ELSE
tooth.master = MASTER_REMOTE
ENDIF
ENDIF
match$ = left_match$(tooth.data_recv$, "TIME:")
IF match$ <> ""
tooth.time$ = time$
ENDIF
ENDIF
tooth.data_recv$ = "NO_DATA"
ENDIF
ENDFUNCTION

FUNCTION left_match$: word_data$, match$
LOCAL length1 = LEN(word_data$)
LOCAL length2 = LEN(match$)
LOCAL result$

IF LEFT$(word_data$, length2) = match$
result$ = RIGHT$(word_data$, length1 - length2)
ELSE
result$ = ""
ENDIF
RETURN result$
ENDFUNCTION

Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Moebius on 2011-Mar-10
Do you really have no problems at all?  It's strange - even running your code I have the same problem with the same line...
We can't tell who's the odd-one-out unless others test as well...  Anyway I've only updated the two devices I'm using to 4.0 and 4.1 - what are yours using?  There must be some reason why we have different results...
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: matchy on 2011-Mar-11
I've updated my sig with spec for my devices.

Maybe it could be an issue with version but you should really post you code also.
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: Moebius on 2011-Mar-14
I'll post the version of the iBluetooth code I'm using soon - I don't really have time ATM.  I'll see if updating to a later OS version makes a difference as well - I've been meaning to update for a while...
Title: Re: [iPhone] Bluetooth Multiplayer
Post by: michael on 2012-Mar-04
i'm missing the declaration of the GLB_iBT_PeerID() in the .h or .mm file, can someone post the code with this declartion,
Thanks