/// <summary> /// Analyse the scan result to find device information /// </summary> /// <param name="iScanResult">Scan result</param> /// <returns>Device information class</returns> private static DeviceInfo InterpretScanResult(ScanResult iScanResult) { Guid guid = MacAddressUtils.GuidFromMacAddress(iScanResult.Device.Address); DeviceInfo device = new DeviceInfo(iScanResult.ScanRecord.DeviceName, guid) { Rssi = iScanResult.Rssi }; // Add in manufacturer specific data Dictionary <int, byte[]> manf = new Dictionary <int, byte[]>(); SparseArray manufacturerSpecificData = iScanResult.ScanRecord.ManufacturerSpecificData; if (manufacturerSpecificData != null) { for (int i = 0; i < manufacturerSpecificData.Size(); i++) { int companyId = iScanResult.ScanRecord.ManufacturerSpecificData.KeyAt(i); byte[] data = (byte[])iScanResult.ScanRecord.ManufacturerSpecificData.ValueAt(i); manf.Add(companyId, data); } } device.ManufacturerSpecificData = manf; // Add in services List <Guid> services = new List <Guid>(); foreach (ParcelUuid uuid in iScanResult.ScanRecord.ServiceUuids ?? new List <ParcelUuid>()) { services.Add(new Guid(uuid.Uuid.ToString())); } device.Services = services; return(device); }
/// <summary> /// Find and connect to a device using its MAC address /// </summary> /// <param name="iDevice">Device info providing a MAC address</param> /// <param name="iMTU">Required MTU for connection (specify 0 if not required)</param> /// <param name="iServices">List of services and characteristic we are going to use</param> /// <param name="iBond">Optional parameter you set to true if you wish to pair (bond) with the device</param> /// <param name="iConnectionEvent">Optional callback event for connection success or failure</param> public void ConnectToDevice(DeviceInfo iDevice, int iMTU, List <ServiceAndCharacteristicsParcel> iServices, bool iBond = false, EventHandler <SuccessEventArgs> iConnectionEvent = null) { // Ensure the adaptor is present CreateAdaptor(); // Find the BluetoothDevice already scanned string mac = MacAddressUtils.MacAddressAsString(iDevice.DeviceId, true); // Clear some fields _mtu = DEFAULT_MTU; _servicesDiscovered = false; // Specify the services and characteristics we are going to use _bleManager.ServiceAndCharacteristics(iServices); // Locate the device object - must have already been scanned BluetoothDevice bluetoothDevice = FoundDevices.FirstOrDefault(d => d.Address == mac); // Create the connection request ConnectRequest request = _bleManager.Connect(bluetoothDevice); // Add in handlers if specified if (iConnectionEvent != null) { _connectionSuccessEventArgs = iConnectionEvent; request .Done(new SuccessCallBack(ConnectionRequestEvent)) .Fail(new SuccessCallBack(ConnectionRequestEvent)); } else { _connectionSuccessEventArgs = null; } // Enqueue the connection request and process it request.Retry(CONNECT_RETRIES, CONNECT_RETRY_DELAY_MS) .UseAutoConnect(false) .Enqueue(); // Bond to the device if required if (iBond) { // Request bonding with device _bleManager.BondToDevice(new SuccessCallBack(_bleManager_DeviceBondedEvent)); } // Enqueue an MTU request if required if (iMTU != 0) { Thread.Sleep(50); _bleManager.MakeMtuRequest(iMTU); } }
/// <summary> /// A callback invoked when the request has failed with status other than BluetoothGatt.GATT_SUCCESS /// </summary> /// <param name="p0">Device sending the data</param> /// <param name="p1">GATT status code</param> /// <param name="message">Optional message</param> public void OnRequestFailed(BluetoothDevice p0, int p1) { // Will wrapping this prevent these JNI errors like "use of deleted global reference"? try { _dataReadEvent?.Invoke(this, new CharacteristicReadEventArgs(MacAddressUtils.GuidFromMacAddress(p0.Address), _characteristic, new byte[0], p1)); } catch { } }
/// <summary> /// Data receiving handler /// </summary> /// <param name="p0">Device sending the data</param> /// <param name="p1">Data values received from the device</param> public void OnDataReceived(BluetoothDevice p0, Data p1) { // Will wrapping this prevent these JNI errors like "use of deleted global reference"? try { _dataReadEvent?.Invoke(this, new CharacteristicReadEventArgs(MacAddressUtils.GuidFromMacAddress(p0.Address), _characteristic, p1.GetValue())); } catch { } }
/// <summary> /// Data send handler /// </summary> /// <param name="p0">Device receiving the data</param> /// <param name="p1">Data values sent to the device</param> public void OnDataSent(BluetoothDevice p0, Data p1) { // Compare what we sent with what the adaptor thinks we sent bool success = _dataSent != null& p1.GetValue() != null?ArrayValuesSame(_dataSent, p1.GetValue()) : true; _dataWriteEvent?.Invoke(this, new CharacteristicWriteEventArgs( MacAddressUtils.GuidFromMacAddress(p0.Address), _characteristic, success, p1.GetValue() != null ? p1.GetValue().Length : 0)); }
/// <summary> /// Find a DeviceInfo instance matching the BluetoothDevice /// </summary> /// <param name="iDevice">Bluetooth device</param> /// <returns>Found device info or null if not found</returns> private DeviceInfo FindDeviceInfo(BluetoothDevice iDevice) { // Find the BluetoothDevice already scanned return(Devices.FirstOrDefault(d => d.DeviceId.ToString() == MacAddressUtils.GuidFromMacAddress(iDevice.Address).ToString())); }
public Guid ConnectedDevice() { return(ManagerPresent() ? MacAddressUtils.GuidFromMacAddress(_bleManager.BluetoothDevice?.Address) : Guid.Empty); }
/// <summary> /// A callback invoked when the request has failed with status other than BluetoothGatt.GATT_SUCCESS /// </summary> /// <param name="p0">Device receiving the data</param> /// <param name="p1">GATT status code</param> /// <param name="message">Optional message</param> public void OnRequestFailed(BluetoothDevice p0, int p1) { _dataWriteEvent?.Invoke(this, new CharacteristicWriteEventArgs(MacAddressUtils.GuidFromMacAddress(p0.Address), _characteristic, false, 0, p1)); }