• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 

Bi-directional communication (client/serve) with Android devices over Blouetooth

Started by jaguard, September 20, 2011, 04:09:04 am

Previous topic - Next topic

jaguard

Hi,

I'm looking for a VCL library for one of my projects that need to interact with Android devices over BT, using bi-directional communication. The usage scenario is as follows:
- The main Win32 app (Delphi XE2) includes a BT server that can handle up to 16 (BT limitation) Android clients, sending them on demand various packages of information like text and images (ex: jpg from USB camera).
- The Android clients are used to send commands to the server (like a remote control) and pulling for information (ex: images, status)

I was looking into the nrComm documentation and demos an seems that TnrBT is centered around synchronous communication and mostly string based focused. Additionally the BT devices, services and classes support seams to be lacking. In comparison, in Android, the communication is exposed over I/O streams and a server can interact with multiple clients at once on a dedicated BluetoothSocket ...
So could you please detail, how nrComm handles multiple BT clients ?  Should you have multiple TnrBT components - dynamically created at runtime per connected device ? How the BT authentication is handled/remembered for known devices ?

For the Bluetooth stack in Android you can take a look here: http://developer.android.com/guide/topics/wireless/bluetooth.html

I'm just wondering if nrComm can support this kind of scenario and if you have it used with success for Android communication.

Roman Novgorodov

Hello

Thank you for your information.

I hope you know but let me remind some basics.

0) Windows provides special Bluetooth API (like android).
1) You need attach BT adapter to PC with Windows.
2) Very often modern BT adapters include original drivers (from CD), that are not compatible with Windows BT API. Very often there is situation when manufacturer of BT adapter provides original BT API, original soft (and SDK for developers) and Windows BT API can not work with this BT adapter. We know such hardware with following BT frameworks: Toshiba, Broadcom, Sony and others.
3) TnrBt component wraps built-in Bluetooth Windows API only. It does not understand 3rd party BT APIs

Windows BT api provide almost same functionality as android api. You can see MS descriptions here:
http://msdn.microsoft.com/en-us/library/aa362932%28v=VS.85%29.aspx

Usual we used following scheme for such tasks:

1) Create global instance of TnrBt class (singletone). It enumerates available BT devices. You can handle WM_DEVICECHANGE system message for monitor devcie appearing.
2) Create run time copy TnrBt for needed connected device. It allows make I/O operations.

Please note that if your BT device provides RFCOMM profile, Windows creates virtual serial port for paired device. In this case you can use TnrComm component for make necessary I/O operations.

If your BT device is visible as HID you can try TnrHid.

I hope you understand my terrible English :-)

Anyway if you will have exact question, we will try to help you wit h nrComm BT.

Roman Novgorodov
DeepSoftware LLC
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

jaguard

Hi Roman,

10x again for your fast and detailed response ...

Android API is built to support RFCOMM profile so as you mentioned in this case, should I may use TnrComm (dynamically built at runtime for all clients) for asynch communication mixed with specific DataProcessors to code the commands request/responses to emulate a high-level protocol ?
IMHO TnrBT device discovery part should be in a static/singleton kind of module (or class variables/methods) to leverage the usage of the instances for communication.

Any hints/pointers to get me started will be quite helpful since I was looking for other VCL libraries as well but no one seams to have ANY usage with Android devices.... So a lot of experimentation should be done here, without any actual sample to help :(

I'm currently in the "investigation" phase for the needed components sets for a new project, since I'm willing to standardize as much as possible to a single VCL I/O platform that provide serial, gps and BT support and nrComm looks quite good in this regard, but I have troubles with the samples and the documentation (since I have no source code to delve more into it) :(
An important aspect is the XE2 support and x64 readiness and you've been quite fast at delivering it :)

So here are, as you suggested, some additional and more direct/focused questions:
1) Since I'll use Delphi XE2, are all the Strings (PChar) really Unicode (or just ASCII/byte "wide" wrappers) as seen in others codes ?
2) Is nrComm x64 friendly (asside of being compilable with XE2) ? Any known limitations here ?
3) Considering the already presented scenario (Win/XP app with BT server ove nrComm -> multiple Android clients) what's your proposed "architecture" to let me easily support connect/disconnect clients (this may imply), secure clients persistence (auto-connect, Address/ID storage?), easy command based request/response interaction ... I'm asking for your help here (as direction lines, maybe some small starting sample) just because the current documentation is sparse and the available samples does not cover this kind of usage.
4) Do you have any additional samples with client/server scenarios over BT (that I can adapt for Android) ? 

BTW, your English is very good, so no problems here ... hopefully my English is not much broken either :)

Roman Novgorodov

Hello

Thank you for your information.

Please see my answers after your questions below:

Quote1) Since I'll use Delphi XE2, are all the Strings (PChar) really Unicode (or just ASCII/byte "wide" wrappers) as seen in others codes ?


XE2 uses unicode (two-byte chars) by default for STRING type.

Quote2) Is nrComm x64 friendly (asside of being compilable with XE2) ? Any known limitations here ?


Today we have fixed some bugs for BT implementation on Win64 platform.
Please download and try updated version of nrComm Lib from here:
http://deepsoftware.com/nrcomm/nrcomm_pro.zip

Quote3) Considering the already presented scenario (Win/XP app with BT server ove nrComm -> multiple Android clients) what's your proposed "architecture" to let me easily support connect/disconnect clients (this may imply), secure clients persistence (auto-connect, Address/ID storage?), easy command based request/response interaction ... I'm asking for your help here (as direction lines, maybe some small starting sample) just because the current documentation is sparse and the available samples does not cover this kind of usage.
4) Do you have any additional samples with client/server scenarios over BT (that I can adapt for Android) ? 


Unfortunately, current version of library does not offer something useful and ready for described C/S architecture. You can implement such task by using nrComm Lib but it will request serious work, deep class design, debug and tests. I'm sorry. Possible we will add something useful into library in the future.

Roman Novgorodov
DeepSoftware LLC
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

jaguard

Hi Roman,

10x again, see my replies bellow
Hello

Please see my answers after your questions below:

Quote from: Roman
Quote from: Jaguard1) Since I'll use Delphi XE2, are all the Strings (PChar) really Unicode (or just ASCII/byte "wide" wrappers) as seen in others codes ?


XE2 uses unicode (two-byte chars) by default for STRING type.

I'm quite an old Delphi user ... so definitely know this :) ... But my point here was regarding the handling of string reads and writes (since over COM we have just bytes) ... and some libraries don't encode/decode the byte stream in the right form, but simply "expand" them ... So, if I write an Unicode string, will it be read it correctly on the "other" side ?

Quote from: Roman
Quote from: Jaguard2) Is nrComm x64 friendly (asside of being compilable with XE2) ? Any known limitations here ?


Today we have fixed some bugs for BT implementation on Win64 platform.
Please download and try updated version of nrComm Lib from here:
http://deepsoftware.com/nrcomm/nrcomm_pro.zip

Great to know ... you're moving fast, I like it !

Quote from: Roman
Quote from: Jaguard3) Considering the already presented scenario (Win/XP app with BT server ove nrComm -> multiple Android clients) what's your proposed "architecture" to let me easily support connect/disconnect clients (this may imply), secure clients persistence (auto-connect, Address/ID storage?), easy command based request/response interaction ... I'm asking for your help here (as direction lines, maybe some small starting sample) just because the current documentation is sparse and the available samples does not cover this kind of usage.
4) Do you have any additional samples with client/server scenarios over BT (that I can adapt for Android) ? 


Unfortunately, current version of library does not offer something useful and ready for described C/S architecture. You can implement such task by using nrComm Lib but it will request serious work, deep class design, debug and tests. I'm sorry. Possible we will add something useful into library in the future.

Hmm ... I didn't expected to work magically ... but I'm just counting on your experience to suggest me a path to start with. Even that I could work over the virtual com ports associated to the BT I don't see how without a BT API/Lib (like nrComm) I can easily to device discovery, pairing ... etc ...
So definitely, nrComm can speedup my development allot ... Just looking for some hints from you to put me on the right track on a minimal C/S architecture as described above ... 

And putting the cherry on the top, a VERY ANNOYING issue pops out during my tests:
All my development tools are HP laptops, (ex: HP nw8240) and all of them have HP/Broadcom Integrated Module with Bluetooth Wireless and I can successfully connect/pair them with my Android or other BT devices (for file transfer or remote keyboard..etc) .... but, using nrComm BTDemo I can't see any local BT adapter, so the combo listing them is empty and similarly the search dialog does not finding it either.
So what's wrong with this ? There are any issues with my systems or the device enumeration is buggy? Or should I use external USB-BT adapters instead ?

Roman Novgorodov

Hello

About UNICODE

Usual device operates BYTES, device know nothing about UNICODE, UTF and etc. Only byte stream.
nrComm gets bytes from connected device as is. You nrComm class returns STRING it is expanded AnsiString (zeros in high 8bits of 16bit char)

About BT problem with your HP.
See my first reply. HP provides original API and driver. You can try BT USB adapter from manufacturers listed on MS site (see link in my reply too)

Roman Novgorodov
DeepSoftware LLC
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

jaguard

Hi Roman, 10x again for you reply ...

Quote from: Roman Novgorodov on September 21, 2011, 03:43:49 pm
About UNICODE
Usual device operates BYTES, device know nothing about UNICODE, UTF and etc. Only byte stream.
nrComm gets bytes from connected device as is. You nrComm class returns STRING it is expanded AnsiString (zeros in high 8bits of 16bit char)

That was my point too ;) ...  no magic behind expected ... Anyway, in this case the API should state this Unicode widening "conversion", otherwise you should interpret the bytes with the proper encoding (ex: UTF8, etc..). In this case, there is any other callback that I can use to access the raw bytes, or I just make the conversion from String -> AnsiString -> bytes array

Quote from: Roman Novgorodov on September 21, 2011, 03:43:49 pm
About BT problem with your HP.
See my first reply. HP provides original API and driver. You can try BT USB adapter from manufacturers listed on MS site (see link in my reply too)

I missed that, I'll take a look. I may use the Window BT Stack for now, with dummy USB dongles where needed, but just wondering if there is any chance to support Broadcom API too (since is quite popular) in near future ?

Roman Novgorodov

Hello

It seems like Windows 7 supports Broadcom BT adapters by default. Please try.

Roman Novgorodov
DeepSoftware LLC
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

jaguard

Quote from: Roman Novgorodov on September 22, 2011, 02:21:08 pm
It seems like Windows 7 supports Broadcom BT adapters by default. Please try.

Good to know, 10x for the tip; currently my targeted platform is on XP/Vista, but I'll take a try to Win7 once needed for migration.