quick access Bluetooth Low Energy on xamarin Forms Android and IOS
Platform | Version |
---|---|
xamarin.forms | |
xamarin.android | API LEVEL>=18 |
xamarin.ios | ios SDK>=7 |
Install-Package Quick.Xamarin.BLE -Version 1.0.4
Simple bluetooth search example. created by xamarin.forms
key features include
- Scan Bluetooth
- List Bluetooth addresses
- Bluetooth connect/disconnect
- Search service and characteristic
- List characteristic features
- Read/Write/Notify
Android AndroidManifest add permission
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Initialization
IBle ble = CrossBle.Createble();
Scan Bluetooth
ble.OnScanDevicesIn += (sender, device) =>
{
Device.BeginInvokeOnMainThread(() =>
{
//use device.Name or Uuid or Rssi or connect dev
IDev dev=device;
});
};
//start scan
ble.StartScanningForDevices();
Stop scan
ble.StopScanningForDevices();
Device connect
dev.ConnectToDevice();
Device disconnect
dev.DisconnectDevice();
Bluetooth device Status callback event
ble.AdapterStatusChange += (sender,Status) =>
{
Device.BeginInvokeOnMainThread(() =>
{
if (Status == AdapterConnectStatus.Connected)//Connect
if (Status == AdapterConnectStatus.None)//disconnect
});
};
Get device characteristics
List<IGattCharacteristic> cha= new List<IGattCharacteristic>();
dev.CharacteristicsDiscovered(value=>
{
Device.BeginInvokeOnMainThread(() =>
{
//save value to list and use
cha.Add(chvaluea);
});
});
Get characteristics properties
cha[n].CanRead();
cha[n].CanWrite();
cha[n].CanNotify();
Read
//Call once
Search.ble.ServerCallBackEvent += (uuid, value) =>
{
};
cha[n].ReadCallBack();
or
var value=await cha[n].ReadCallBack();
Write
cha[n].Write(byte[]);
Notify
cha[n].NotifyEvent += SelectCharacteristic_NotifyEvent;
cha[n].Notify();
Stop Notify
enter code here
cha[n].StopNotify();
cha[n].NotifyEvent -= SelectCharacteristic_NotifyEvent;
Notify Event
private void SelectCharacteristic_NotifyEvent(object sender, byte[] value)
{
Device.BeginInvokeOnMainThread(() => {
});
}
- Event must be added Device.BeginInvokeOnMainThread
- Characteristicsmust be 1-2 seconds after the Bluetooth connection
- Bluetooth can only connect one at a time,When you want to connect other,Must call DisconnectDevice() first