Szerkesztés

Megosztás a következőn keresztül:


CoreBluetooth Namespace

The CoreBluetooth namespace provides classes for Bluetooth communication.

Classes

AdvertisementData
CBAdvertisement

Keys used to lookup dictionary values from the NSDictionary received as a parameter in DiscoveredPeripheral(CBCentralManager, CBPeripheral, NSDictionary, NSNumber).

CBATTErrorExtensions

Extension methods for the CoreBluetooth.CBATTError enumeration.

CBATTRequest

An Attribute Protocol request for reading or writing.

CBATTRequestEventArgs

Provides data for the ReadRequestReceived event.

CBATTRequestsEventArgs

Provides data for the WriteRequestsReceived event.

CBAttribute

Class that uniquely identifies a Bluetooth device.

CBCentral

Used to identify centrals (that are not the current device).

CBCentralInitOptions

Contains options for initializing a Bluetooth central manager.

CBCentralManager

Represents the local central device in Bluetooth LE.  Use the CBCentralManager to scan, discover and connect to remote peripherals.

CBCentralManagerDelegate

Delegate objects for CBCentralManager objects.

CBCentralManagerDelegate_Extensions

Extension methods to the ICBCentralManagerDelegate interface to support all the methods from the CBCentralManagerDelegate protocol.

CBCharacteristic

Characteristics of a remote peripheral.

CBCharacteristicEventArgs

Provides data for the WroteCharacteristicValue, E:CoreBluetooth.CBCharacteristicEventArgs.UpdatedCharacterteristicValue, E:CoreBluetooth.CBCharacteristicEventArgs.UpdatedNotificationState and E:CoreBluetooth.CBCharacteristicEventArgs.WroteCharacteristicValue events.

CBDescriptor

An immutable description of a peripheral's characteristic. See also CBMutableDescriptor.

CBDescriptorEventArgs

Provides data for the WroteDescriptorValue and E:CoreBluetooth.CBDescriptorEventArgs.WroteDescriptorValue events.

CBDiscoveredPeripheralEventArgs

Provides data for the DiscoveredPeripheral event.

CBErrorExtensions

Extension methods for the CoreBluetooth.CBError enumeration.

CBL2CapChannel
CBManager
CBMutableCharacteristic

A mutable CBCharacteristic.

CBMutableDescriptor

A mutable T:Corebluetooth.CBDescriptor.

CBMutableService

A mutable CBService.

CBPeer

Class that represents a central or perhipheral Bluetooth device.

CBPeripheral

Represents a CoreBluetooth peripheral.

CBPeripheralDelegate

Delegate object for CBPeripheral. Provides methods called on events relating to discovery, exploration, and interaction with a remote peripheral.

CBPeripheralDelegate_Extensions

Extension methods to the ICBPeripheralDelegate interface to support all the methods from the CBPeripheralDelegate protocol.

CBPeripheralErrorEventArgs

Provides data for the FailedToConnectPeripheral and E:CoreBluetooth.CBPeripheralErrorEventArgs.FailedToConnectPeripheral events.

CBPeripheralEventArgs

Provides data for the ConnectedPeripheral event.

CBPeripheralManager

Manages published services per the T:Coreblutooth.CBPeripheral device's GATT database.

CBPeripheralManagerDelegate

Delegate object for CBPeripheralManager. Adds methods for events relating to availability, publishing, advertising, and subscription.

CBPeripheralManagerDelegate_Extensions

Extension methods to the ICBPeripheralManagerDelegate interface to support all the methods from the CBPeripheralManagerDelegate protocol.

CBPeripheralManagerL2CapChannelOperationEventArgs

Provides data for the DidUnpublishL2CapChannel and E:CoreBluetooth.CBPeripheralManagerL2CapChannelOperationEventArgs.DidUnpublishL2CapChannel events.

CBPeripheralManagerOpenL2CapChannelEventArgs

Provides data for the DidOpenL2CapChannel event.

CBPeripheralManagerServiceEventArgs

Provides data for the ServiceAdded event.

CBPeripheralManagerSubscriptionEventArgs

Provides data for the CharacteristicUnsubscribed and E:CoreBluetooth.CBPeripheralManagerSubscriptionEventArgs.CharacteristicUnsubscribed events.

CBPeripheralOpenL2CapChannelEventArgs

Provides data for the DidOpenL2CapChannel event.

CBPeripheralServicesEventArgs

Provides data for the ModifiedServices event.

CBPeripheralsEventArgs

Provides data for the RetrievedPeripherals and E:CoreBluetooth.CBPeripheralsEventArgs.RetrievedPeripherals events.

CBRssiEventArgs

Provides data for the RssiRead event.

CBService

Represents the services of a remote peripheral.

CBServiceEventArgs

Provides data for the DiscoveredIncludedService and E:CoreBluetooth.CBServiceEventArgs.DiscoveredIncludedService events.

CBUUID

Universal Unique Identifiers for the Bluetooth stack.

CBWillRestoreEventArgs

Provides data for the WillRestoreState and E:CoreBluetooth.CBWillRestoreEventArgs.WillRestoreState events.

PeripheralConnectionOptions

Peripheral connection options.

PeripheralScanningOptions

Possible values for the options parameter in calls to ScanForPeripherals(CBUUID[], NSDictionary).

RestoredState
StartAdvertisingOptions

Manages access to options used by M:CoreBluetooth.StartAdvertising* method.

Interfaces

ICBCentralManagerDelegate

Interface representing the required methods (if any) of the protocol CBCentralManagerDelegate.

ICBPeripheralDelegate

Interface representing the required methods (if any) of the protocol CBPeripheralDelegate.

ICBPeripheralManagerDelegate

Interface representing the required methods (if any) of the protocol CBPeripheralManagerDelegate.

Enums

CBATTError

Errors returned by a GATT server.

CBAttributePermissions

Enumerates the read, write, and encryption permissions for a characteristic's values.

CBCentralManagerState

Enumerates possible states of a CBCentralManager.

CBCharacteristicProperties

The possible properties of a characteristic. A characteristic may have multiple properties.

CBCharacteristicWriteType

Enumerates the possible types of writes to a characteristic's value.

CBError

Errors possible during Bluetooth LE transactions.

CBManagerState
CBPeripheralManagerAuthorizationStatus

Enumerates the possible states of the CBPeripheralManager.

CBPeripheralManagerConnectionLatency

The connection latency of the CBPeripheralManager.

CBPeripheralManagerState

Enumerates the possible states of the CBPeripheralManager.

CBPeripheralState

Enumerates the possible connection states of a CBPeripheral.

Remarks

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

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

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

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

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

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 DiscoveredPeripheral method may be called multiple times for a single physical device. Once application developers have a reference to the CBPeripheral in which they are interested, they should maintain that reference for further work and call StopScan.

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

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

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);