32feet icon indicating copy to clipboard operation
32feet copied to clipboard

Implement Android BluetoothListener

Open royben opened this issue 3 years ago • 3 comments

I am trying to connect and send data from my PC to my android phone.

Service side (Xamarin Forms)

listener.AcceptBluetoothClient(); is not blocking and always returns null.

BluetoothListener listener = new BluetoothListener(BluetoothService.SerialPort);
listener.Start();

BluetoothClient client = null;

while (client == null)
{
    Thread.Sleep(10);
    client = listener.AcceptBluetoothClient();
}

var stream = client.GetStream();

do
{
    byte[] data = new byte[1];
    stream.Read(data, 0, data.Length);
    Debug.WriteLine(Encoding.UTF8.GetString(data));
} while (stream.DataAvailable);

listener.Dispose();

PC side (Console App)

The Connect method throws exception: System.Net.Sockets.SocketException: 'The requested address is not valid in its context FCDE90B69C4C:00001101-0000-1000-8000-00805f9b34fb (0)'

var client = new BluetoothClient();

BluetoothDeviceInfo info = null;

foreach (var device in client.PairedDevices)
{
    if (device.DeviceName.Contains("Galaxy"))
    {
        info = device;
        break;
    }
}

client.Connect(info.DeviceAddress, BluetoothService.SerialPort);
var stream = client.GetStream();

byte[] data = Encoding.UTF8.GetBytes("Hello World");
stream.Write(data, 0, data.Length);

client.Dispose();

If I run the service side on a PC, the AcceptBluetoothClient(); will block as expected.

Any advice would be appreciated. Thanks!

royben avatar May 17 '21 03:05 royben

Now I see...

// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Net.Sockets.BluetoothListener (Android)
// 
// Copyright (c) 2018-2020 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License

using Android.Bluetooth;
using InTheHand.Net.Bluetooth;
using System;

namespace InTheHand.Net.Sockets
{
    partial class BluetoothListener
    {
        void DoStart()
        {

        }

        void DoStop()
        {

        }

        bool DoPending()
        {
            return false;
        }

        BluetoothClient DoAcceptBluetoothClient()
        {
            return null;
        }
    }
}

Are there any plans on implementing the listener on Android ?

royben avatar May 17 '21 04:05 royben

Here is a very rough implementation..

partial class BluetoothListener
{
    private BluetoothAdapter _adapter;
    private BluetoothServerSocket _server;

    void DoStart()
    {
        _adapter = BluetoothAdapter.DefaultAdapter;

        _server = _adapter.ListenUsingRfcommWithServiceRecord(ServiceName, UUID.FromString(serviceUuid.ToString()));
    }

    void DoStop()
    {
        _server?.Dispose();
        _adapter?.Dispose();
    }

    bool DoPending()
    {
        return false;
    }

    BluetoothClient DoAcceptBluetoothClient()
    {
        return new BluetoothClient(_server.Accept());
    }
}

royben avatar May 17 '21 06:05 royben

Thanks, I'm looking into adding this shortly.

peterfoot avatar May 17 '21 09:05 peterfoot