0

I am developing a WPF application, capable of sending, and receiving text, images, and videos to Android devices over Bluetooth. I'm using 32feet.Net library.

Currently, I can send, and receive images and videos using ObexObjectPush service but I don't know by using which service I can send text message.

I have made a sample WPF application for sending text to another WPF application. I followed the steps specified in this link. In that I used same Guid as service id in both application.

Now, My question is do I have to create an andriod application, for achieving same, which will be using same service id and can send and receive text messages over Bluetooth?

I have tried SerialPort but it didn't work.

  using (var bluetoothClient = new BluetoothClient())
   {
        var endPoint = new BluetoothEndPoint(device.DeviceAddress, 
                         BluetoothService.SerialPort); 
         bluetoothClient.Connect(ep); // this line fails

         /*
          More code
        */
   }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Mohit Bora
  • 113
  • 1
  • 10
  • The only way to send text messages is using SMS/MMS service cause there is no way to send text that appears on device over Bluetooth (except you send simple text file). To work with SMS/MMS you have to use MAP Profile (it is not implemented in 32feet but soon will be availbale in our library). The other way is to send vCard orVcal files with information you want to display. They stored in Phone book or in Calendar. But them will not appear on display any way until user opens it. – Mike Petrichenko Jun 13 '18 at 12:40

1 Answers1

0

I solved the problem of sending text over bluetooth using 32Feet. I simply created a GUID for my connection and used the same id at both(sender/receiver) end. My sender was Android client and receiver was WPF application. I'm sending string as stream of byte.Here is code:

 private void SetupBluetoothListner()
    {
        _messageServiceId = new Guid("{646171EA-EA18-4CCF-8D7A-C57D46991775}");
        _isBluetoothRecieverStarted = true;


        if (_cancelToken != null && _messageListener != null)
        {
            this.Dispose(true);
        }

        _cancelToken = new CancellationTokenSource();

        _messageListener = new BluetoothListener(_messageServiceId)
        {
            ServiceName = Constants.BluetoothStringMessageService
        };
    }



 private void BluetoothListener(CancellationTokenSource cancelToken, BluetoothListener bluetoothListener)
    {
        try
        {
            while (true)
            {
                using (var client = bluetoothListener.AcceptBluetoothClient())
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        return;
                    }

                    using (var streamReader = new StreamReader(client.GetStream()))
                    {
                        try
                        {
                          var content = streamReader.ReadToEnd();
                          if (!string.IsNullOrEmpty(content))
                          {
                             MessageReceiver(content);
                          }
                        }
                        catch (IOException ex)
                        {
                            client.Close();
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Constants.ErrorAtReceivingStringMessage, MessageBoxButton.OK);
        }
    }




   private bool SendData(Device device, byte[] content)
    {
        using (var bluetoothClient = new BluetoothClient())
        {
            try
            {
                var endpoint = new BluetoothEndPoint(device.DeviceInfo.DeviceAddress, _messageServiceId);
                bluetoothClient.Connect(endpoint);

                var bluetoothStream = bluetoothClient.GetStream();

                if (bluetoothClient.Connected && bluetoothStream != null)
                {
                    bluetoothStream.Write(content, 0, content.Length);
                    bluetoothStream.Flush();
                    bluetoothStream.Close();
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
              // TODO: handle exception
            }
        }
        return false;
    }

------------ Code for Android client-------

 private UUID serviceUUID = UUID.fromString("0e6114d0-8a2e-477a-8502-298d1ff4b4ba");



public void OpenSocket(BluetoothDevice currentDevice)    {

    try {
        BluetoothSocket socket = currentDevice.createRfcommSocketToServiceRecord(serviceUUID);
        socket.connect();
        outputStream = socket.getOutputStream();
        inputStream = socket.getInputStream();
    }
    catch(Exception ex){

        Log.d("socket","socket" + ex.getMessage());
    }
}

public void SendData(String message) {

    try{
        outputStream.write(message.getBytes());
    }
    catch(Exception ex){

    }
}

I'm still stuck at the problem of capturing data send using ObexObjectPush.

Mohit Bora
  • 113
  • 1
  • 10