Search code examples
xamarin.formsbluetooth-lowenergyraspberry-pi3tizentizen-wearable-sdk

Connect LE bluetooth from samsung watch active wearable app to raspberry pi, and send gattservice to it


I am developing a wearable app on Tizen.net framework for Samsung galaxy watch active, to communicate with a raspberry pi. when a button method is triggered, the wearable connects to the bluetooth address given (hardcoded) and sends an integer to that address via GATT request (haven't implemented GATT part yet). However, my wearable will not find the address I am looking for, and thus wont connect to the le Bluetooth on raspberry pi FYI I am using raspberry pi 3+ on python 2.7.

I tried debugged on VS but it throws a segmentation fault due.

static BluetoothGattClient client = null;
static bool StateChanged_flag = false;
public static string remote_addr = ("B8:27:EB:DC:D9:EC");
public static BluetoothLeDevice leDevice = null;



//Here is my buttonClick method that should trigger the connection. 
private async void ConnectBrewer_Clicked(object sender, EventArgs e)
    {
        try
        {
            if (!BluetoothAdapter.IsBluetoothEnabled)
            {
                Toast.DisplayText("Please turn on Bluetooth.");
            }
            else
            {
                ConnectBrewer.IsEnabled = false;
                BluetoothAdapter.ScanResultChanged += scanResultEventHandler;
                if (leDevice == null)
                {
                    BluetoothAdapter.StartLeScan();
                    await WaitScanFlag();
                    BluetoothAdapter.StopLeScan();
                    ConnectBrewer.IsEnabled = true;

                }

                else
                {
                    ConnectBrewer.Text = "Found";
                    ConnectBrewer.IsEnabled = false;
                    index_Page.Children[2].IsVisible = true;
                    index_Page.CurrentPage = index_Page.Children[2];
                    leDevice.GattConnectionStateChanged += LeDevice_GattConnectionStateChanged;
                    client = leDevice.GattConnect(false);
                    ConnectBrewer.Text = "Connected!";
                }

            }
        }
        catch (Exception ex)
        {
            Toast.DisplayText("Error: " + ex.Message);
        }
    }

public async Task WaitScanFlag()

    {
        int count = 0;
        do
        {
            await Task.Delay(3000);
            if (count % 4 == 0)
            { 

            }
            count++;

        } while (count < 10 && !scanFlag);

    }
public static void scanResultEventHandler(object sender, AdapterLeScanResultChangedEventArgs e)
    {

        int txLevel;
        int rssi;
        BluetoothError result;
        string address;
        BluetoothLePacketType PacketType;

        if (!e.DeviceData.Equals(null) && e.DeviceData.RemoteAddress.Equals(remote_addr))
        {
//Here is where I am setting my leDevice object,based on the remote address, but the if statement conditions are never true. 
               leDevice = e.DeviceData;
            result = e.Result;
            scanFlag = true;
        }

    }
public static void LeDevice_GattConnectionStateChanged(object sender, GattConnectionStateChangedEventArgs e)
    {
        if (e.Result != (int)BluetoothError.None)
        {
            StateChanged_flag = false;
        }
        else if (!e.RemoteAddress.Equals(remote_addr))
        {
            StateChanged_flag = false;
        }
        else if (e.IsConnected.Equals(false))
        {
            StateChanged_flag = false;
        }
        else
        {
            StateChanged_flag = true;
        }
    }

I expect it to find the device based on the remote address, but ledevice remains null and is never updated, triggering a segmentation fault. How do I connect based on a pregiven address?


Solution

  • We can check 2 things for this issue.

    1. The string value for address

      • It is possible to be caused by ( ) in address. So please remove ( ) , and test again. public static string remote_addr = "B8:27:EB:DC:D9:EC";
    2. The remote device (RPI3) is not in advertisement. So did not search really.

      • First, using android's app(nRF connect), try to scan your RPI3. and check if it is scanned or not in the mobile also.
      • Then, enter galaxy watch's shell. Scan it using scanning tool in low layer.
    sdb root on
    sdb shell
    hcitool lescan
    LE Scan ...
    E4:DB:6D:62:4B:09 (unknown)
    E4:DB:6D:62:4B:09 (unknown)
    B8:BB:AF:C2:7B:1D (unknown)