public async Task <bool> StartServer(e_BtMode mode) { try { if (!Init()) { return(false); } if (mode == e_BtMode.LowEnergy) { var uuid_ser = Java.Util.UUID.FromString(BleConstants.BLE_SERVER_SERVICE_UUID); var uuid_ch = Java.Util.UUID.FromString(BleConstants.BLE_SERVER_CH_UUID); /// Set advertising AdvertiseSettings settings = new AdvertiseSettings.Builder() .SetConnectable(true) .SetAdvertiseMode(AdvertiseMode.LowLatency) .SetTxPowerLevel(AdvertiseTx.PowerMedium) .Build(); AdvertiseData advertiseData = new AdvertiseData.Builder() .SetIncludeDeviceName(true) .SetIncludeTxPowerLevel(true) .Build(); AdvertiseData scanResponseData = new AdvertiseData.Builder() //.AddServiceUuid(new ParcelUuid(uuid_ser)) //otherwise AdvertiseFailure for DataTooLarge //.AddServiceData(new ParcelUuid(uuid_ser), new byte[] { 0x01, 0x02 }) .SetIncludeDeviceName(true) .SetIncludeTxPowerLevel(true) .Build(); _CentralManager.Adapter.BluetoothLeAdvertiser.StartAdvertising(settings, advertiseData, scanResponseData, _AdvertiseCallback); /// Set ble server _GattServer = _CentralManager.OpenGattServer(Android.App.Application.Context, _GattServerCallback); _Mode = mode; var write_ch = new BluetoothGattCharacteristic(uuid_ch, GattProperty.WriteNoResponse, GattPermission.Write); var service = new BluetoothGattService(uuid_ser, GattServiceType.Primary); service.AddCharacteristic(write_ch); _GattServer.AddService(service); } else if (mode == e_BtMode.Socket) { _Mode = mode; _ServerSocket = _CentralManager.Adapter.ListenUsingRfcommWithServiceRecord("server_socket", Java.Util.UUID.FromString(BleConstants.BLE_SERVER_SOCKET_UUID)); _BluetoothSocket = await _ServerSocket.AcceptAsync(); await Task.Run(ServerSocketManager); } else { SERVER_ERR("start server failed"); return(false); } return(true); } catch (Exception ex) { SERVER_ERR("start server error", ex); return(false); } }
public async Task <bool> StartConnection(string name, string address, int timeoutMs, e_BtMode mode) { try { if (!BleEnabled) { BLE_ERR("can't start connection, ble not enabled"); return(false); } if (!Init()) { return(false); } if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(address)) { BLE_ERR("can't start connection, ble device not valid"); return(false); } BluetoothDevice device = null; if (string.IsNullOrEmpty(address)) { device = _Devices.Find(x => x.Name == name); } else { device = _Devices.Find(x => x.Address == address); } if (device == null) { BLE_ERR("can't start connection, device not found"); return(false); } bool completed = false; if (mode == e_BtMode.LowEnergy) { BLE("connecting to " + name + " via ble"); _Mode = mode; device.ConnectGatt(Android.App.Application.Context, false, _ConnectCallback, BluetoothTransports.Le); _Semaphore = new SemaphoreSlim(0, 1); completed = await _Semaphore.WaitAsync(timeoutMs); _Semaphore = null; } else if (mode == e_BtMode.Socket) { BLE("connecting to " + name + " via socket"); _Mode = mode; _BluetoothSocket = device.CreateRfcommSocketToServiceRecord(Java.Util.UUID.FromString(BleConstants.BLE_SERVER_SOCKET_UUID)); await _BluetoothSocket.ConnectAsync(); completed = true; _IsConnected = _BluetoothSocket.IsConnected; } if (completed && _IsConnected) { return(true); } BLE_ERR("connection failed"); return(false); } catch (Exception ex) { BLE_ERR("start connection error", ex); return(false); } }