Bluetooth connection between pc and android smartphone (VB.net, 32feet.net, Android)

Topics: Bluetooth - Microsoft, Bluetooth - Other
Feb 28, 2012 at 8:24 AM

Hi!
I need to implement a bluetooth micronet formed by one pc as server and two client: another pc and an android smartphone. For pc's i'm using 32feet.net libraries.

|android phone|<------BT------>|server pc|<-----BT---->|client pc|

This three elements need to exchange text data in a low format mode.

Now i'm developing the connection from server pc and smartphone. Before bluetooth connection, the phone pass his information
(like GUID or MAC address) by NFC message, so the connection can be estabilished directly, without discovering.
Which procedure i need to use?

Thanks a lot
Gianluca


I'm working on visual basic express 2010 under windows 7 64bit, and android 4.03

Developer
Feb 29, 2012 at 4:24 PM

For the client side use BluetoothClient http://32feet.codeplex.com/wikipage?title=General%20Bluetooth%20Data%20Connections&referringTitle=Documentation so

class MyConsts
{
    static readonly Guid MyServiceUuid
      = new Guid("{00112233-4455-6677-8899-aabbccddeeff}");
}

    BluetoothAddress addr
      = BluetoothAddress.Parse("001122334455");
    Guid serviceClass;
    serviceClass = BluetoothService.SerialPort;
    // - or - etc
    // serviceClass = MyConsts.MyServiceUuid
    //
    var ep = new BluetoothEndPoint(addr, serviceClass);
    var cli = new BluetoothClient();
    cli.Connect(ep);
    Stream peerStream = cli.GetStream();
    peerStream.Write/Read ...

Take the Device Address ('addr') from the NFC comms and optionally the Service Class too -- but you may want just to pre-define that for your service and have it constant in the three programs.

For the server-side use BluetoothListener, http://32feet.codeplex.com/wikipage?title=Bluetooth%20Server-side&referringTitle=General%20Bluetooth%20Data%20Connections

Hope that helps.

class MyConsts
{
static readonly Guid MyServiceUuid
= new Guid("{00112233-4455-6677-8899-aabbccddeeff}");
}

BluetoothAddress addr
= BluetoothAddress.Parse("001122334455");
Guid serviceClass;
serviceClass = BluetoothService.SerialPort;
// - or - etc
// serviceClass = MyConsts.MyServiceUuid
//
var ep = new BluetoothEndPoint(addr, serviceClass);
var cli = new BluetoothClient();
cli.Connect(ep);
Stream peerStream = cli.GetStream();
peerStream.Write/Read ...
Mar 1, 2012 at 9:26 AM
Edited Mar 1, 2012 at 9:38 AM

Thank you very much. I reached my first goal to connect server-pc with smartphone.  I have another question: since my work is intended for public and automatic service, there is a way to bypass the pairing phase?

Thanks for your job

Developer
Mar 8, 2012 at 3:20 PM

On desktop Windows with the Microsoft stack use BluetoothWin32Authentication and set e.Pin or e.Confirm etc in the callback event.

Other platforms don't have as good support... :-,(

May 13, 2012 at 3:19 PM
Edited May 13, 2012 at 3:31 PM

Hi! Thanks again for reply... 

I wrote this function for connection with phone:

 

 Public Function BeginConnectionPhone() As Boolean Implements NetManagerServer.BeginConnectionPhone
        If Not PhoneConnected Then
            phoneListener = New BluetoothListener(UUID_PHONE)
            phoneListener.Start()
        End If
        Try
            'if the phone is not present, program blocks here
            clientPhone = phoneListener.AcceptBluetoothClient()
            clientPhone.GetStream()

            PhoneConnected = True

            RaiseEvent Connect(Me, clientPhone.GetStream())
        Catch ex As Exception
            PhoneConnected = False
        End Try
        Return PhoneConnected
  End Function

 

I want that this function returns 'false' if the phone is not present, even after a certain timeout. But, it remains blocked when i call AcceptBluetoothClient() method, and it isn't thrown any exception. I need to take another way to implement this request? May 

Developer
May 15, 2012 at 3:08 PM
Edited May 15, 2012 at 3:08 PM

That's no BluetoothListener's behaviour.  BluetoothListener implements the RFCOMM service model for applications on peer devices to connect to.  (Like OBEX Object transfer, serial-cable emulation, or the control channel for handsfree etc).  It does not monitor if one or more remote devices are present.

I you want two devices to monitor if they are within range of each other, then one (or both) will have to try connecting regularly to see if the remote device is in range.  The simplest way would simply be for the device to try to connect to the other device -- e.g. run BluetoothClient.Connect or BluetoothDeviceInfo.GetServiceRecords regularly.  See e.g. http://32feet.codeplex.com/wikipage?title=Testing if a device is in range

Feb 22, 2014 at 12:26 PM
Hi all,

all things I tried did not work with my current HTC One (Android 4.3) :(
What I did is the following:
  1. Created a new windowsforms-project (VB.net) using Visual Studio 2012 Express for Windows Desktop and .NET-Framework 4.5
  2. I created a button and the following method:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'define address
        Dim Address As BluetoothAddress = BluetoothAddress.Parse("001122334455")

        'fetch serial port
        Dim ServiceClass As Guid = BluetoothService.SerialPort

        'define endpoint
        Dim Ep As New BluetoothEndPoint(Address, ServiceClass)

        'define client
        Dim Client As New BluetoothClient()

        'connect to endpoint
        Client.Connect(Ep)

        'fetch stream
        Dim PeerStream As Stream = Client.GetStream()

        'write text
        PeerStream.Write(Convert.FromBase64String("test"), 0, Convert.FromBase64String("test").Length)

        'close stream
        PeerStream.Close()

        'disconnect
        Client.EndConnect(Nothing)
    End Sub
After clicking on the button a socket-exception is thrown at "Client.Connect(Ep)" with the comment: "the requested address is not valid in its context".
What´s the problem in this case?

Thanks for your help!
Best regards
Dominik
Developer
Feb 23, 2014 at 2:26 PM
See the reason for that error in the docs at https://32feet.codeplex.com/wikipage?title=Errors What listener is running on the Android??
Feb 23, 2014 at 5:08 PM
Edited Feb 23, 2014 at 5:08 PM
I only enabled bluetooth on android and set the visibility of my phone to everyone. After that I tried to connect with the code above. First of all I want to call a phone-number. If that works, I want to implement the features of the hands-free profile.

Additionally there is one more question: Is there any posibility to check whether the connection got lost?
Developer
Feb 24, 2014 at 8:59 AM
Well if you connected a new PC to the Ethernet and tried to connect to its SMTP Email server that'd fail until you added an email server the same's happening here. You need to have an active remote server/service to connect to.

Full HFP requires setting up the audio channel which requires driver level programming on Windows.
(http://www.alanjmcf.me.uk/comms/bluetooth/Bluetooth%20Profiles%20and%2032feet.NET.html
https://32feet.codeplex.com/wikipage?title=Connecting%20to%20Bluetooth%20Services&referringTitle=Documentation)

On connection loss, see https://32feet.codeplex.com/wikipage?title=Connected%20Property
Feb 24, 2014 at 7:26 PM
Edited Feb 24, 2014 at 7:27 PM
Okay, thanks for your help - so I have to skip that part now and take a look at the main-feature(s).

Is there any possibility to connect to a phone (does not matter whether it´s an Android-phone, an iPhone or an old Nokia) and just stay connected (without sending data)?
One main feature of my programm should be to check whether the connected device is in range or not. If the device is not reachable anymore the programm should do some action (specified before). I thought if this works I can include the HFP-profile too to have more functions, but it does not seem to be easy :)

Thank you in advance!
Best regards
Dominik