Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.iOS CoreBluetooth/External Accesory issue

I've looking here on Forums, on the monotouch samples GIT hub, and never found a really functional sample to use CoreBluetooth in order to achieve the following: 1.Check if is there a device that match a criteria(by name or some identifier of the device) paired and connected 2.If paired but not connected, try connect to it 3.If connection fails, then show a list of the bluetooth devices that matches the criterias on topic 1 so the user can select and connect to it

Note: The device I'm trying to connect uses SPP but is Apple MFi certified. It is a credit card reader over bluetooth and some of then even implement ExternalAccessory protocols

The CoreBluetooth samples page is empty http://developer.xamarin.com/samples/ios/CoreBluetooth/

I've trying this pretty simple sample that never get the events called after the scan:

public static class BTHelper
    {
        private static CBCentralManager manager;
        private static CBUUID UUID;

        static BTHelper()
        {
            manager =
            manager.DiscoveredPeripheral += OnDiscovery;
            manager.ConnectedPeripheral += OnConnected;
            manager.DisconnectedPeripheral += OnDisconnected;
            UUID = CBUUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
        }

        public static void CheckBluetooth()
        {
            manager.ScanForPeripherals(new[] { UUID });
        }

        static void OnDisconnected(object sender, CBPeripheralErrorEventArgs e)
        {
            Console.WriteLine("Disconnected - " + e.Peripheral.Name);
        }

        static void OnConnected(object sender, CBPeripheralEventArgs e)
        {
            Console.WriteLine("Connected - " + e.Peripheral.Name);
        }

        static void OnDiscovery(object sender, CBDiscoveredPeripheralEventArgs e)
        {
            Console.WriteLine("Found - " + e.Peripheral.Name);
        }
    }

Can anyone help? I've being really tired of googling and looking of many questions on SO with no real answer.

@XamarinTeam, you guys should provide a sample on how to use it... We are lost without reference...

Thank, really appreciate any help...

Gutemberg

like image 901
Gutemberg Ribeiro Avatar asked Sep 06 '14 00:09

Gutemberg Ribeiro


1 Answers

It seems like you are looking at wrong documents.Core Bluetooth only allows you to communicate with Bluetooth Low Energy (BLE) devices using the GATT profile. you can not scan SPP device with corebluetooth.

For your MFI device, you need to check External Accessory framework , It allows communication with 'legacy' Bluetooth devices using profiles such as the Serial Port Protocol (SPP).

To answer your question: : 1.Check if is there a device that match a criteria(by name or some identifier of the device) paired and connected

You can use showBluetoothAccessoryPicker function of EAAccessoryManager to get list of Available devices, read more here

2.If paired but not connected, try connect to it

There is not any documented way to check for this. You can not initiate connect from app without showBluetoothAccessoryPicker . You can monitor for EAAccessoryDidConnect notification. if this method is not called, and showbluetoothaccessorypicker 's complition get called, your device is not connected.

3.If connection fails, then show a list of the bluetooth devices that matches the criterias on topic 1 so the user can select and connect to it 1)

After completion of showbluetoothaccessorypicker You can check in ConnectedAccessories . If its not avaiable, call showbluetoothaccessorypicker to display list of accessories.

Sample code for using External Accessory framework in your code

EAAccessoryManager manager= EAAccessoryManager.SharedAccessoryManager;
var allaccessorries= manager.ConnectedAccessories;
foreach(var accessory in allaccessorries)
{
    yourlable.Text = "find accessory";
    Console.WriteLine(accessory.ToString());
    Console.WriteLine(accessory.Name);
    var protocol = "com.Yourprotocol.name";

    if(accessory.ProtocolStrings.Where(s => s == protocol).Any())
    {
        yourlable.Text = "Accessory  found";
        //start session
        var session = new EASession(accessory, protocol);
        var outputStream = session.OutputStream;
        outputStream.Delegate = new MyOutputStreamDelegate(yourlable);
        outputStream.Schedule(NSRunLoop.Current, "kCFRunLoopDefaultMode");
        outputStream.Open();
    }
}

and

public class MyOutputStreamDelegate : NSStreamDelegate
{
    UILabel label;
    bool hasWritten = false;

    public MyOutputStreamDelegate(UILabel label)
    {
        this.label = label;
    }
    public override void HandleEvent(NSStream theStream, NSStreamEvent streamEvent)
    {
         //write code to handle  stream.

    }
}

There is not any perticular demo for using Exeternal Accessory framework, but You can check this sample code for understanding how it works.:

Whole Project

AccessoryBrowser class

like image 160
PlusInfosys Avatar answered Nov 10 '22 20:11

PlusInfosys