简体   繁体   中英

How to enable and disable Bluetooth using C#

i'm currently working on bluetooth activation ( just enable and disable the bluetooth after a certain operation automatically from Windows CE version 6 )
i'm using SmartDeviceFramework ie CAB file that i then install it in Windows CE

Below is my method i worked on used ( InTheHand.Net.Personal.dll file for Bluetooth):

  private static void setBluetoothConnection()
    {
     try
       {
           if (BluetoothRadio.IsSupported == true)
           {
               MessageBox.Show("Bluetooth Supported", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
               BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
               MessageBox.Show(radio.Mode.ToString(), "Before Bluetooth Connection", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
               radio.Mode = RadioMode.Discoverable;
               // here radio.Mode works only if the Windows Device has Bluetooth enabled otherwise gives error
               MessageBox.Show(radio.Mode.ToString(), "RadioMode Discover", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
               bluetoothClient = new BluetoothClient();
               //Cursor.Current = Cursors.WaitCursor;
               BluetoothDeviceInfo[] bluetoothDeviceInfo = bluetoothClient.DiscoverDevices();
               MessageBox.Show(bluetoothDeviceInfo.Length.ToString(), "Device Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
               foreach(BluetoothDeviceInfo device in bluetoothDeviceInfo)
               {
                 Cursor.Current = Cursors.Default;
                 MessageBox.Show(device.DeviceName, "Device Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                 bluetoothClient.Connect(new BluetoothEndPoint(device.DeviceAddress, service));
                 MessageBox.Show("Bluetooth Connected...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                 break;
               }
             }
          else
          {
               MessageBox.Show("Bluetooth Not Supported", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
          }
       }
        catch (Exception ex)
        {
           log.Error("[Bluetooth] Connection failed", ex);
           MessageBox.Show(ex.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        }
    }

so i face error here :

BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
radio.Mode = RadioMode.Discoverable; // gives error here 

Error :

Error setting BluetoothRadio.Mode

The above error occurs once when Bluetooth is disabled in Device and the above line is executed and closes the application
But as the application closes, and when i go to Bluetooth Manager in Mobile, the Bluetooth is enabled .

my problem :

i have to click a button 2 times to enable button (1st when the application closes with error (but Bluetooth is set ON) and 2nd to search Devices in range) instead of 1 click.

My Assumption

I think that there maybe some security problem when the program tries to enable the Bluetooth from Mobile from OFF to Discoverable.

So is there any Process ( System.Digonostics; dll ) by which i can set the Bluetooth ON and OFF automatically in WindowsMobile CE in C#

I tried but not getting it, so could anyone help me with this or suggest any dll files for Bluetooth connection.

Thanks

You may find this useful:

Bluetooth Device Development using C#

and the download for the Windows Embedded Source Tools for Bluetooth Technology is here:

Download

Hope this helps :)

I'm not entirely certain what that library you're using is, so I'm not sure exactly why it is giving you that error.

Here is MSFTs documentation as to how to set the Bluetooth connection mode on a device. If you can use these DLLs you may be able to have some luck.

https://msdn.microsoft.com/en-us/library/bb416244.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM