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.
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.
Here are the steps to complete this recipe:
using Android.Bluetooth
and Java.Util
at the top of your MainActivity
class. In addition, you will need the Bluetooth permissions.MainActivity
class:BluetoothAdapter myBTAdapter; BluetoothDevice myBTDevice; BluetoothSocket myBTSocket; Byte[] buffer = new Byte[124];
BluetoothAdapter
instance in your OnCreate()
method:myBTAdapter = BluetoothAdapter.DefaultAdapter; if (myBTAdapter == null || myBTAdapter.IsEnabled) { Console.WriteLine ("Can't do anything"); }
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(); }
openSocket()
method:async protected void openSocket() { myBTSocket = myBTDevice.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb")); await myBTSocket.ConnectAsync(); }
readData()
method:async protected void readData() { await myBTSocket.InputStream.ReadAsync (buffer, 0, 124); }
WriteData()
method:async protected void writeData() { //Fill buffer w/ something await myBTSocket.OutputStream.WriteAsync (buffer, 0, 124); }
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.
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();
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.
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.
3.138.170.174