MonoTouch.CoreBluetooth Namespace

APIs to interact with Bluetooth devices.

Remarks

The CoreBluetooth namespace allows developers to work with Bluetooth Low Energy (Bluetooth LE) devices.

The Bluetooth LE model revolves around MonoTouch.CoreBluetooth.CBPeer objects. There are two kinds of MonoTouch.CoreBluetooth.CBPeer: MonoTouch.CoreBluetooth.CBCentral objects that scan and consume data provided by MonoTouch.CoreBluetooth.CBPeripheral objects that correspond to data-providing Bluetooth LE devices.

Generally, the iOS device will be in the MonoTouch.CoreBluetooth.CBCentral role, but it is also possible to create apps in which the device is a MonoTouch.CoreBluetooth.CBPeripheral.

The following example shows the typical initialization behavior of an app that consumes data from external Bluetooth LE devices. Applications that have a MonoTouch.CoreBluetooth.CBCentral must have an associated MonoTouch.CoreBluetooth.CBCentralManagerDelegate delegate object and must override it's MonoTouch.CoreBluetooth.CBCentralManagerDelegate.UpdatedState method. The override must check the state of the MonoTouch.CoreBluetooth.CBCentralManager and confirm that it is MonoTouch.CoreBluetooth.CBCentralManagerState.PoweredOn.

Typically, applications will want to scan for Bluetooth LE peripherals. This is a high-energy function, so developers should call MonoTouch.CoreBluetooth.CBManager.StopScan after finding the MonoTouch.CoreBluetooth.CBPeripheral in which they are interested or after a certain amount of time.

C# Example

public class MySimpleCBCentralManagerDelegate : CBCentralManagerDelegate
{
		override public void UpdatedState (CBCentralManager mgr)
		{
			if (mgr.State == CBCentralManagerState.PoweredOn) {
				//Passing in null scans for all peripherals. Peripherals can be targeted by using CBUIIDs
				CBUUID[] cbuuids = null;
				mgr.ScanForPeripherals (cbuuids); //Initiates async calls of DiscoveredPeripheral
				//Timeout after 30 seconds
				var timer = new Timer (30 * 1000);
				timer.Elapsed += (sender, e) => mgr.StopScan();
			} else {
				//Invalid state -- Bluetooth powered down, unavailable, etc.
				System.Console.WriteLine ("Bluetooth is not available");
			}
		}

		public override void DiscoveredPeripheral (CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
		{
			Console.WriteLine ("Discovered {0}, data {1}, RSSI {2}", peripheral.Name, advertisementData, RSSI);
		}
}    

public partial class HelloBluetoothCSharpViewController : UIViewController
{
    MySimpleCBCentralManagerDelegate myDel;

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			
      //Important to retain reference, else will be GC'ed
			myDel = new MySimpleCBCentralManagerDelegate ();
			var myMgr = new CBCentralManager (myDel, DispatchQueue.CurrentQueue);

		}
//...etc... 
}     
          

The MonoTouch.CoreBluetooth.CBCentralManagerDelegate.DiscoveredPeripheral method may be called multiple times for a single physical device. Once application developers have a reference to the MonoTouch.CoreBluetooth.CBPeripheral in which they are interested, they should maintain that reference for further work and call MonoTouch.CoreBluetooth.CBCentralManager.StopScan.

With a reference to a discovered MonoTouch.CoreBluetooth.CBPeripheral, developers can then connect to it, as shown in the following example.

A MonoTouch.CoreBluetooth.CBPeripheral has zero or more MonoTouch.CoreBluetooth.CBServices, each of which has zero or more MonoTouch.CoreBluetooth.CBCharacteristics. If the characteristic represents a measurement (such as a heartbeat, time, temperature, etc.), the value will be in the MonoTouch.CoreBluetooth.CBCharacteristic.Value property.

C# Example

public class SimplePeripheralDelegate : CBPeripheralDelegate
{
	public override void DiscoveredService (CBPeripheral peripheral, NSError error)
	{
		System.Console.WriteLine ("Discovered a service");
		foreach (var service in peripheral.Services) {
			Console.WriteLine (service.ToString ()); 
			peripheral.DiscoverCharacteristics (service);
		}
	}

	public override void DiscoveredCharacteristic (CBPeripheral peripheral, CBService service, NSError error)
	{
		System.Console.WriteLine ("Discovered characteristics of " + peripheral);
		foreach (var c in service.Characteristics) {
			Console.WriteLine (c.ToString ());
			peripheral.ReadValue (c);
		}
	}

	public override void UpdatedValue (CBPeripheral peripheral, CBDescriptor descriptor, NSError error)
	{
		Console.WriteLine ("Value of characteristic " + descriptor.Characteristic + " is " + descriptor.Value);
	}

	public override void UpdatedCharacterteristicValue (CBPeripheral peripheral, CBCharacteristic characteristic, NSError error)
	{
		Console.WriteLine ("Value of characteristic " + characteristic.ToString () + " is " + characteristic.Value);
	}
}

//...
mgr = new CBCentralManager (myCentralDelegate, DispatchQueue.CurrentQueue);

mgr.ConnectedPeripheral += (s, e) => {
	activePeripheral = e.Peripheral;
	System.Console.WriteLine ("Connected to " + activePeripheral.Name);

  
	if (activePeripheral.Delegate == null) {
		activePeripheral.Delegate = new SimplePeripheralDelegate ();
    //Begins asynchronous discovery of services
		activePeripheral.DiscoverServices ();
	}
};

//Connect to peripheral, triggering call to ConnectedPeripheral event handled above 
mgr.ConnectPeripheral (myPeripheral);          
          

Classes

TypeReason
CBAdvertisementKeys used to lookup dictionary values from the NSDictionary received as a parameter in MonoTouch.CoreBluetooth.CBCentralManagerDelegate.DiscoveredPeripheral.
CBATTErrorErrors returned by a GATT server.
CBATTRequestAn Attribute Protocol request for reading or writing.
CBATTRequestEventArgsProvides data for the MonoTouch.CoreBluetooth.CBATTRequestEventArgs.ReadRequestReceived event.
CBATTRequestsEventArgsProvides data for the MonoTouch.CoreBluetooth.CBATTRequestsEventArgs.WriteRequestsReceived event.
CBAttributeDocumentation for this section has not yet been entered.
CBAttributePermissionsEnumerates the read, write, and encryption permissions for a characteristic's values.
CBCentralUsed to identify centrals (that are not the current device).
CBCentralManagerRepresents the local central device in Bluetooth LE.  Use the CBCentralManager to scan, discover and connect to remote peripherals.
CBCentralManagerDelegateDelegate objects for MonoTouch.CoreBluetooth.CBCentralManager objects.
CBCentralManagerDelegate_ExtensionsExtension methods to the MonoTouch.CoreBluetooth.ICBCentralManagerDelegate interface to support all the methods from the MonoTouch.CoreBluetooth.CBCentralManagerDelegate protocol.
CBCentralManagerStateEnumerates possible states of a MonoTouch.CoreBluetooth.CBCentralManager.
CBCharacteristicCharacteristics of a remote peripheral.
CBCharacteristicEventArgsProvides data for the MonoTouch.CoreBluetooth.CBCharacteristicEventArgs.DiscoveredDescriptor, MonoTouch.CoreBluetooth.CBCharacteristicEventArgs.UpdatedCharacterteristicValue, MonoTouch.CoreBluetooth.CBCharacteristicEventArgs.UpdatedNotificationState and MonoTouch.CoreBluetooth.CBCharacteristicEventArgs.WroteCharacteristicValue events.
CBCharacteristicPropertiesThe possible properties of a characteristic. A characteristic may have multiple properties.
CBCharacteristicWriteTypeEnumerates the possible types of writes to a characteristic's value.
CBDescriptorAn immutable description of a peripheral's characteristic. See also MonoTouch.CoreBluetooth.CBMutableDescriptor.
CBDescriptorEventArgsProvides data for the MonoTouch.CoreBluetooth.CBDescriptorEventArgs.UpdatedValue and MonoTouch.CoreBluetooth.CBDescriptorEventArgs.WroteDescriptorValue events.
CBDiscoveredPeripheralEventArgsProvides data for the MonoTouch.CoreBluetooth.CBDiscoveredPeripheralEventArgs.DiscoveredPeripheral event.
CBErrorErrors possible during Bluetooth LE transactions.
CBMutableCharacteristicA mutable MonoTouch.CoreBluetooth.CBCharacteristic.
CBMutableDescriptorA mutable MonoTouch.Corebluetooth.CBDescriptor.
CBMutableServiceA mutable MonoTouch.Corebluetooth.CBService.
CBPeerDocumentation for this section has not yet been entered.
CBPeripheralRepresents a CoreBluetooth peripheral.
CBPeripheralDelegateDelegate object for MonoTouch.Corebluetooth.CBPeripheral. Provides methods called on events relating to discovery, exploration, and interaction with a remote peripheral.
CBPeripheralDelegate_ExtensionsExtension methods to the MonoTouch.CoreBluetooth.ICBPeripheralDelegate interface to support all the methods from the MonoTouch.CoreBluetooth.CBPeripheralDelegate protocol.
CBPeripheralErrorEventArgsProvides data for the MonoTouch.CoreBluetooth.CBPeripheralErrorEventArgs.DisconnectedPeripheral and MonoTouch.CoreBluetooth.CBPeripheralErrorEventArgs.FailedToConnectPeripheral events.
CBPeripheralEventArgsProvides data for the MonoTouch.CoreBluetooth.CBPeripheralEventArgs.ConnectedPeripheral event.
CBPeripheralManagerManages published services per the MonoTouch.Coreblutooth.CBPeripheral device's GATT database.
CBPeripheralManagerAuthorizationStatusEnumerates the possible states of the MonoTouch.Corebluetooth.CBPeripheralManager.
CBPeripheralManagerConnectionLatencyThe connection latency of the MonoTouch.Corebluetooth.CBPeripheralManager.
CBPeripheralManagerDelegateDelegate object for MonoTouch.Corebluetooth.CBPeripheralManager. Adds methods for events relating to availability, publishing, advertising, and subscription.
CBPeripheralManagerDelegate_ExtensionsExtension methods to the MonoTouch.CoreBluetooth.ICBPeripheralManagerDelegate interface to support all the methods from the MonoTouch.CoreBluetooth.CBPeripheralManagerDelegate protocol.
CBPeripheralManagerServiceEventArgsProvides data for the MonoTouch.CoreBluetooth.CBPeripheralManagerServiceEventArgs.ServiceAdded event.
CBPeripheralManagerStateEnumerates the possible states of the MonoTouch.Corebluetooth.CBPeripheralManager.
CBPeripheralManagerSubscriptionEventArgsProvides data for the MonoTouch.CoreBluetooth.CBPeripheralManagerSubscriptionEventArgs.CharacteristicSubscribed and MonoTouch.CoreBluetooth.CBPeripheralManagerSubscriptionEventArgs.CharacteristicUnsubscribed events.
CBPeripheralServicesEventArgsProvides data for the MonoTouch.CoreBluetooth.CBPeripheralServicesEventArgs.ModifiedServices event.
CBPeripheralsEventArgsProvides data for the MonoTouch.CoreBluetooth.CBPeripheralsEventArgs.RetrievedConnectedPeripherals and MonoTouch.CoreBluetooth.CBPeripheralsEventArgs.RetrievedPeripherals events.
CBPeripheralStateEnumerates the possible connection states of a MonoTouch.Corebluetooth.CBPeripheral.
CBServiceRepresents the services of a remote peripheral.
CBServiceEventArgsProvides data for the MonoTouch.CoreBluetooth.CBServiceEventArgs.DiscoverCharacteristic, MonoTouch.CoreBluetooth.CBServiceEventArgs.DiscoveredCharacteristic and MonoTouch.CoreBluetooth.CBServiceEventArgs.DiscoveredIncludedService events.
CBUUIDUniversal Unique Identifiers for the Bluetooth stack.
CBWillRestoreEventArgsProvides data for the MonoTouch.CoreBluetooth.CBWillRestoreEventArgs.WillRestoreState and MonoTouch.CoreBluetooth.CBWillRestoreEventArgs.WillRestoreState events.
ICBCentralManagerDelegateInterface representing the required methods (if any) of the protocol MonoTouch.CoreBluetooth.CBCentralManagerDelegate.
ICBPeripheralDelegateInterface representing the required methods (if any) of the protocol MonoTouch.CoreBluetooth.CBPeripheralDelegate.
ICBPeripheralManagerDelegateInterface representing the required methods (if any) of the protocol MonoTouch.CoreBluetooth.CBPeripheralManagerDelegate.
PeripheralConnectionOptionsPeripheral connection options.
PeripheralScanningOptionsPossible values for the options parameter in calls to MonoTouch.Corebluetooth.CBCentralManager.ScanForPeripherals.
StartAdvertisingOptionsManages access to options used by MonoTouch.CoreBluetooth.StartAdvertising method.