Using Bluetooth

Bluetooth is a well-known wireless communication standard and almost all phones embed a Bluetooth controller. In this recipe, we'll connect ourselves to a Bluetooth object and send/receive data from it.

Getting ready

To follow this recipe, create a new project called Bluetooth, and you'll need to deploy your application on a physical phone as the emulator doesn't support Bluetooth. Also, you'll need to bond your phone to the target Bluetooth device of your choice—using the Bluetooth Manager of Android—prior to launching the app.

How to do it...

Here are the steps to complete this recipe:

  1. Add using Android.Bluetooth and Java.Util at the top of your MainActivity class. In addition, you will need the Bluetooth permissions.
  2. Add the following attribute to your MainActivity class:
    BluetoothAdapter myBTAdapter;
    BluetoothDevice myBTDevice;
    BluetoothSocket myBTSocket;
    Byte[] buffer = new Byte[124];
  3. Construct a reference to the BluetoothAdapter instance in your OnCreate() method:
    myBTAdapter = BluetoothAdapter.DefaultAdapter;
    if (myBTAdapter == null || myBTAdapter.IsEnabled) {
      Console.WriteLine ("Can't do anything");
    }
  4. Create a connectToDevice()method, which takes the name of the device to which you want to send data to in an argument:
    protected void connectToDevice(String name) {
      myBTDevice = (from bd in myBTAdapter.BondedDevices
      where bd.Name == name select bd).FirstOrDefault();
    }
  5. Create an openSocket() method:
    async protected void openSocket() {
      myBTSocket = myBTDevice.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
      await myBTSocket.ConnectAsync();
    }
  6. Create a readData() method:
    async protected void readData() {
      await myBTSocket.InputStream.ReadAsync (buffer, 0, 124);
    }
  7. Create a WriteData() method:
    async protected void writeData() {
      //Fill buffer w/ something
      await myBTSocket.OutputStream.WriteAsync (buffer, 0, 124);
    }
  8. Use these methods to read, write and to communicate with your paired device.

How it works...

Bluetooth communication can occur by socket reading and writing. To create the socket, or open such a socket, we have to retrieve the default Bluetooth adapter with the following code:

BluetoothAdapter.DefaultAdapter;
if (myBTAdapter == null || myBTAdapter.IsEnabled) {
    Console.WriteLine ("Can't do anything");
}

Test that the Bluetooth adapter is not null, meaning that the phone has a Bluetooth controller and that the Bluetooth is enabled.

Tip

Note that you have to ask permission to use Bluetooth like we did with the NFC in the Android Manifest.xml file.

Then, we use the query form we saw in Chapter 5, Using On-Phone Data, to retrieve a reference to the Bluetooth device we've paired using its name:

myBTDevice = (from bd in myBTAdapter.BondedDevices
  where bd.Name == name select bd).FirstOrDefault();

Now that we have our Bluetooth device, we can open a socket using the CreateRfcommSocketToServiceRecord() method:

myBTSocket = myBTDevice.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
myBTSocket.ConnectAsync();

Tip

The UUID instance used in this recipe is the default UUID for RFCOMM. Note that you could either search the specification of your devices for non-default UUID if any or generate your own.

The ConnectAsync()method is asynchronous, as its name suggests, and we use the C# keywords async and await to secure our statements.

Finally, we can read and write on the socket using the myBTSocket.InputStream.ReadAsync() and myBTSocket.OutputStream.WriteAsync() methods.

See also

Go to https://github.com/xamarin/monodroid-samples/tree/master/BluetoothChat for a very interesting, yet lengthy to write and understand, chat based on Bluetooth.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.170.174