public BattByteTestServer() { _serverCallback = new ServerCallback(); _advertiseCallback = new AdvertiseCallback(); var appContext = Android.App.Application.Context; var manager = (BluetoothManager)appContext.GetSystemService(Application.BluetoothService); var server = manager.OpenGattServer(appContext, _serverCallback); _serverCallback.GattServer = server; _adapter = manager.Adapter; var battByteService = new BluetoothGattService(UUID.FromString("BEEF".ToGuid().ToString()), GattServiceType.Primary); var voltageCharacteristic = new BluetoothGattCharacteristic(UUID.FromString("BEF0".ToGuid().ToString()), GattProperty.Read, GattPermission.Read); voltageCharacteristic.SetValue(new byte[] { 0xB0, 0x0B }); battByteService.AddCharacteristic(voltageCharacteristic); var tempCharacteristic = new BluetoothGattCharacteristic(UUID.FromString("BEF1".ToGuid().ToString()), GattProperty.Read, GattPermission.Read); tempCharacteristic.SetValue(9876, GattFormat.Sint16, 0); battByteService.AddCharacteristic(tempCharacteristic); server.AddService(battByteService); _server = server; StartAdvertising("Blah!"); }
protected override async Task InitializeAsync() { await base.InitializeAsync(); // BLE requires location permissions if (await SensusServiceHelper.Get().ObtainPermissionAsync(Permission.Location) != PermissionStatus.Granted) { // throw standard exception instead of NotSupportedException, since the user might decide to enable location in the future // and we'd like the probe to be restarted at that time. string error = "Geolocation is not permitted on this device. Cannot start Bluetooth probe."; await SensusServiceHelper.Get().FlashNotificationAsync(error); throw new Exception(error); } _deviceIdCharacteristic = new BluetoothGattCharacteristic(UUID.FromString(DEVICE_ID_CHARACTERISTIC_UUID), GattProperty.Read, GattPermission.Read); _deviceIdCharacteristic.SetValue(Encoding.UTF8.GetBytes(SensusServiceHelper.Get().DeviceId)); _deviceIdService = new BluetoothGattService(UUID.FromString(Protocol.Id), GattServiceType.Primary); _deviceIdService.AddCharacteristic(_deviceIdCharacteristic); _bluetoothAdvertiserCallback = new AndroidBluetoothServerAdvertisingCallback(_deviceIdService, _deviceIdCharacteristic); if (ScanMode.HasFlag(BluetoothScanModes.Classic)) { _bluetoothBroadcastReceiver = new AndroidBluetoothDeviceReceiver(this); IntentFilter intentFilter = new IntentFilter(); intentFilter.AddAction(BluetoothDevice.ActionFound); Application.Context.RegisterReceiver(_bluetoothBroadcastReceiver, intentFilter); } }
public void Start(Context context, TracingInformation tracingInformation) { if (!Initialized) { _logger.LogError("Advertiser - Starting failed - not initialized."); return; } _tracingInformation = tracingInformation; try { _logger.LogDebug("Advertiser - starting."); var serviceUuid = ParcelUuid.FromString(_tracingInformation.ServiceId); var characteristicUuid = ParcelUuid.FromString(_tracingInformation.CharacteristicId); _logger.LogDebug($"Advertiser - initializing. ServiceId: {_tracingInformation.ServiceId}. CharacteristicId: {_tracingInformation.CharacteristicId}"); BtGattServerCallback bluetoothGattServerCallback = new BtGattServerCallback(_tracingInformation); _gattServer = _bluetoothManager.OpenGattServer(context, bluetoothGattServerCallback); bluetoothGattServerCallback._gattServer = _gattServer; BluetoothGattService service = new BluetoothGattService(serviceUuid.Uuid, GattServiceType.Primary); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(characteristicUuid.Uuid, GattProperty.Read, GattPermission.Read); service.AddCharacteristic(characteristic); _gattServer.AddService(service); _logger.LogDebug("Advertiser - started."); } catch (Exception ex) { _logger.LogError(ex, "Advertiser - failed advertising initialization."); } }
private void CharacteristicsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) { foreach (ICharacteristic newItem in notifyCollectionChangedEventArgs.NewItems) { switch (notifyCollectionChangedEventArgs.Action) { case NotifyCollectionChangedAction.Add: NativeService.AddCharacteristic((BluetoothGattCharacteristic)newItem.NativeCharacteristic); break; case NotifyCollectionChangedAction.Remove: // remove characteristic break; case NotifyCollectionChangedAction.Replace: // create & remove break; case NotifyCollectionChangedAction.Reset: // Remove all break; case NotifyCollectionChangedAction.Move: default: break; } } }
public void AddReadOnlyService( Guid serviceName, Dictionary <Guid, Func <byte[]> > characteristics) { var manager = (BluetoothManager)context.GetSystemService( Context.BluetoothService); var adapter = manager.Adapter; if (!adapter.IsEnabled) { // TODO: restarts the server when the BLE adapter goes down. adapter.Enable(); Thread.Sleep(10 * 1000); } // Creates a Gatt server with the requested service { var callback = new AndroidBLEGattServerCallback( characteristics); var server = manager.OpenGattServer(context, callback); callback.Server = server; var service = new BluetoothGattService(AsJavaUUID(serviceName), GattServiceType.Primary); foreach (var i in characteristics) { var ch = new BluetoothGattCharacteristic(AsJavaUUID(i.Key), GattProperty.Read | GattProperty.Notify, GattPermission.Read); service.AddCharacteristic(ch); } server.AddService(service); } // Starts advertising for the availaible services over BLE. { var advertiser = adapter.BluetoothLeAdvertiser; var setts = new AdvertiseSettings.Builder() .SetAdvertiseMode(AdvertiseMode.Balanced) .SetConnectable(true) .SetTimeout(0) .SetTxPowerLevel(AdvertiseTx.PowerMedium) .Build(); var data = new AdvertiseData.Builder() //.AddServiceUuid(AsParcelUuid(ser)) .Build(); var callback = new AndroidBLEAdvertiseCallback(); advertiser.StartAdvertising(setts, data, callback); } }
private BluetoothGattService createService() { var service = new BluetoothGattService(Const.UUID_SERVICE, GattServiceType.Primary); var write_ch = new BluetoothGattCharacteristic(Const.UUID_DATA, GattProperty.WriteNoResponse, GattPermission.Write); service.AddCharacteristic(write_ch); return(service); }
public BleServer(Context ctx) { _bluetoothManager = (BluetoothManager)ctx.GetSystemService(Context.BluetoothService); _bluetoothAdapter = _bluetoothManager.Adapter; _bluettothServerCallback = new BleGattServerCallback(); _bluetoothServer = _bluetoothManager.OpenGattServer(ctx, _bluettothServerCallback); var service = new BluetoothGattService(UUID.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"), GattServiceType.Primary); _characteristic = new BluetoothGattCharacteristic(UUID.FromString("d8de624e-140f-4a22-8594-e2216b84a5f2"), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write); _characteristic.AddDescriptor(new BluetoothGattDescriptor(UUID.FromString("28765900-7498-4bd4-aa9e-46c4a4fb7b07"), GattDescriptorPermission.Write | GattDescriptorPermission.Read)); service.AddCharacteristic(_characteristic); _characteristic = new BluetoothGattCharacteristic(UUID.FromString("d8de624e-140f-4a22-8594-e2216b84a5f3"), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write); _characteristic.AddDescriptor(new BluetoothGattDescriptor(UUID.FromString("28765900-7498-4bd4-aa9e-46c4a4fb7b08"), GattDescriptorPermission.Write | GattDescriptorPermission.Read)); service.AddCharacteristic(_characteristic); _bluetoothServer.AddService(service); _bluettothServerCallback.CharacteristicReadRequest += _bluettothServerCallback_CharacteristicReadRequest; _bluettothServerCallback.DescriptorWriteRequest += _bluettothServerCallback_DescriptorWriteRequest; _bluettothServerCallback.CharacteristicWriteRequest += _bluettothServerCallback_CharacteristicWriteRequest; _bluettothServerCallback.NotificationSent += _bluettothServerCallback_NotificationSent; _bluettothServerCallback.ConnectionStateChange += _bluettothServerCallback_ConnectionStateChange; // MessagingCenter.Send<BLE202.App, string>((BLE202.App)Xamarin.Forms.Application.Current, "Hi", "Server created!"); // _bluetoothAdapter.SetName("Concungungoc"); BluetoothLeAdvertiser myBluetoothLeAdvertiser = _bluetoothAdapter.BluetoothLeAdvertiser; var builder = new AdvertiseSettings.Builder(); builder.SetAdvertiseMode(AdvertiseMode.LowLatency); builder.SetConnectable(true); builder.SetTimeout(0); builder.SetTxPowerLevel(AdvertiseTx.PowerHigh); AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder(); dataBuilder.SetIncludeDeviceName(true); //dataBuilder.AddServiceUuid(ParcelUuid.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5")); dataBuilder.SetIncludeTxPowerLevel(true); myBluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), new BleAdvertiseCallback()); }
public BluetoothGattService CreateService() { var serviceUuid = UUID.FromString(ServiceUuid); var service = new BluetoothGattService(serviceUuid, GattServiceType.Primary); service.AddCharacteristic(new BluetoothGattCharacteristic(UUID.FromString(WriteCharacteristic), GattProperty.WriteNoResponse, GattPermission.Write)); return(service); }
private BluetoothGattService CreateTimeService() { BluetoothGattService service = new BluetoothGattService(TIME_SERVICE, BluetoothGattService.ServiceTypePrimary); BluetoothGattCharacteristic currentTime = new BluetoothGattCharacteristic(CURRENT_TIME, GattProperty.Read | GattProperty.Notify, GattPermission.Read); BluetoothGattDescriptor configDescriptor = new BluetoothGattDescriptor(CLIENT_CONFIG, GattDescriptorPermission.Read | GattDescriptorPermission.Write); currentTime.AddDescriptor(configDescriptor); service.AddCharacteristic(currentTime); return(service); }
protected override void Initialize() { base.Initialize(); // BLE requires location permissions if (SensusServiceHelper.Get().ObtainPermission(Permission.Location) != PermissionStatus.Granted) { // throw standard exception instead of NotSupportedException, since the user might decide to enable location in the future // and we'd like the probe to be restarted at that time. string error = "Geolocation is not permitted on this device. Cannot start Bluetooth probe."; SensusServiceHelper.Get().FlashNotificationAsync(error); throw new Exception(error); } _deviceIdCharacteristic = new BluetoothGattCharacteristic(UUID.FromString(DEVICE_ID_CHARACTERISTIC_UUID), GattProperty.Read, GattPermission.Read); _deviceIdCharacteristic.SetValue(Encoding.UTF8.GetBytes(SensusServiceHelper.Get().DeviceId)); _deviceIdService = new BluetoothGattService(UUID.FromString(DEVICE_ID_SERVICE_UUID), GattServiceType.Primary); _deviceIdService.AddCharacteristic(_deviceIdCharacteristic); }
public BleServer(Context ctx) { _bluetoothManager = (BluetoothManager)ctx.GetSystemService(Context.BluetoothService); _bluetoothAdapter = _bluetoothManager.Adapter; _bluettothServerCallback = new BleGattServerCallback(); _bluetoothServer = _bluetoothManager.OpenGattServer(ctx, _bluettothServerCallback); var service = new BluetoothGattService(UUID.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"), GattServiceType.Primary); _characteristic = new BluetoothGattCharacteristic(UUID.FromString("d8de624e-140f-4a22-8594-e2216b84a5f2"), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write); _characteristic.AddDescriptor(new BluetoothGattDescriptor(UUID.FromString("28765900-7498-4bd4-aa9e-46c4a4fb7b07"), GattDescriptorPermission.Read | GattDescriptorPermission.Write)); service.AddCharacteristic(_characteristic); _bluetoothServer.AddService(service); _bluettothServerCallback.CharacteristicReadRequest += _bluettothServerCallback_CharacteristicReadRequest; _bluettothServerCallback.NotificationSent += _bluettothServerCallback_NotificationSent; Console.WriteLine("Server created!"); BluetoothLeAdvertiser myBluetoothLeAdvertiser = _bluetoothAdapter.BluetoothLeAdvertiser; var builder = new AdvertiseSettings.Builder(); builder.SetAdvertiseMode(AdvertiseMode.LowLatency); builder.SetConnectable(true); builder.SetTimeout(0); builder.SetTxPowerLevel(AdvertiseTx.PowerHigh); AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder(); dataBuilder.SetIncludeDeviceName(true); byte[] bytes = Encoding.ASCII.GetBytes("jp"); dataBuilder.AddManufacturerData(1984, bytes); dataBuilder.SetIncludeTxPowerLevel(true); myBluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), new BleAdvertiseCallback()); }
public BleServer(Context ctx) { _bluetoothManager = (BluetoothManager)ctx.GetSystemService(Context.BluetoothService); _bluetoothAdapter = _bluetoothManager.Adapter; _bluettothServerCallback = new BleGattServerCallback(); _bluetoothServer = _bluetoothManager.OpenGattServer(ctx, _bluettothServerCallback); var service = new BluetoothGattService(UUID.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"), GattServiceType.Primary); _characteristic = new BluetoothGattCharacteristic(UUID.FromString("d8de624e-140f-4a22-8594-e2216b84a5f2"), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write); service.AddCharacteristic(_characteristic); _bluetoothServer.AddService(service); SensorSpeed speed = SensorSpeed.UI; OrientationSensor.Start(speed); OrientationSensor.ReadingChanged += OrientationSensor_ReadingChanged; _bluettothServerCallback.CharacteristicReadRequest += _bluettothServerCallback_CharacteristicReadRequest; Console.WriteLine("Server created!"); BluetoothLeAdvertiser myBluetoothLeAdvertiser = _bluetoothAdapter.BluetoothLeAdvertiser; var builder = new AdvertiseSettings.Builder(); builder.SetAdvertiseMode(AdvertiseMode.LowLatency); builder.SetConnectable(true); builder.SetTimeout(0); builder.SetTxPowerLevel(AdvertiseTx.PowerHigh); AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder(); dataBuilder.SetIncludeDeviceName(true); dataBuilder.SetIncludeTxPowerLevel(true); myBluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), new BleAdvertiseCallback()); }
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); } }
// use predefined Bluetooth SIG definitions for well know characteristics on UWP- Windows project. //public static Guid ManufacturerName = GattCharacteristicUuids.ManufacturerNameString; public BleServer(Context ctx) { _isRunning = false; try { _bluetoothManager = (BluetoothManager)ctx.GetSystemService(Context.BluetoothService); _bluetoothAdapter = _bluetoothManager.Adapter; _bluettothServerCallback = new BleGattServerCallback(); _bluetoothServer = _bluetoothManager.OpenGattServer(ctx, _bluettothServerCallback); var service = new BluetoothGattService(UUID.FromString(App.ServiceId), GattServiceType.Primary); //_characteristicDataExchange = new BluetoothGattCharacteristic(UUID.FromString(App.DataExchange), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write); _characteristicInitData = new BluetoothGattCharacteristic(UUID.FromString(App.InitData), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write); _characteristicFirmwareVersion = new BluetoothGattCharacteristic(UUID.FromString(App.FirmwareVersion), GattProperty.Read, GattPermission.Read); _characteristicDeviceNickname = new BluetoothGattCharacteristic(UUID.FromString(App.DeviceNickname), GattProperty.Read, GattPermission.Read); _descriptorDeviceNickname = new BluetoothGattDescriptor(UUID.FromString(App.DeviceNicknameDescriptor), GattDescriptorPermission.Read); _characteristicDeviceContact = new BluetoothGattCharacteristic(UUID.FromString(App.DeviceContact), GattProperty.Read, GattPermission.Read); _characteristicDeviceInfo = new BluetoothGattCharacteristic(UUID.FromString(App.DeviceInfo), GattProperty.Read, GattPermission.Read); _characteristicInfected = new BluetoothGattCharacteristic(UUID.FromString(App.IsInfected), GattProperty.Read, GattPermission.Read); _characteristicManufacturerName = new BluetoothGattCharacteristic(UUID.FromString(App.ManufacturerName), GattProperty.Read, GattPermission.Read); _characteristicMAC = new BluetoothGattCharacteristic(UUID.FromString(App.DeviceMAC), GattProperty.Read, GattPermission.Read); //_characteristicDataExchange.SetValue("Data exchange"); //service.AddCharacteristic(_characteristicDataExchange); _characteristicFirmwareVersion.SetValue(App.FirmwareVersionValue); service.AddCharacteristic(_characteristicFirmwareVersion); _characteristicDeviceNickname.SetValue(App.DeviceNicknameValue); service.AddCharacteristic(_characteristicDeviceNickname); _characteristicDeviceContact.SetValue(App.DeviceContactValue); service.AddCharacteristic(_characteristicDeviceContact); _characteristicDeviceInfo.SetValue(App.DeviceInfoValue); service.AddCharacteristic(_characteristicDeviceInfo); _characteristicMAC.SetValue(App.DeviceMACValue); service.AddCharacteristic(_characteristicMAC); //_characteristicInitData.SetValue("SPACE - Copyright 2020"); //service.AddCharacteristic(_characteristicInitData); _characteristicInfected.SetValue(App.IsInfectedValue); service.AddCharacteristic(_characteristicInfected); _characteristicManufacturerName.SetValue(App.ManufacturerNameValue); service.AddCharacteristic(_characteristicManufacturerName); _bluetoothServer.AddService(service); _bluettothServerCallback.CharacteristicReadRequest += _bluettothServerCallback_CharacteristicReadRequest; _bluettothServerCallback.CharacteristicWriteRequest += _bluettothServerCallback_CharacteristicWriteRequest; //_bluettothServerCallback.NotificationSent += _bluettothServerCallback_NotificationSent; Console.WriteLine("Server created!"); BluetoothLeAdvertiser myBluetoothLeAdvertiser = _bluetoothAdapter.BluetoothLeAdvertiser; var builder = new AdvertiseSettings.Builder(); builder.SetAdvertiseMode(AdvertiseMode.LowLatency); builder.SetConnectable(true); builder.SetTimeout(0); builder.SetTxPowerLevel(AdvertiseTx.PowerHigh); AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder(); //dataBuilder.SetIncludeDeviceName(false); dataBuilder.AddServiceUuid(ParcelUuid.FromString(App.ServiceId)); dataBuilder.SetIncludeTxPowerLevel(true); //byte[] data = Encoding.UTF8.GetBytes(App.ManufacturerNameValue); //dataBuilder.AddManufacturerData(0x00E0, data); AdvertiseData.Builder dataResponseBuilder = new AdvertiseData.Builder(); dataResponseBuilder.SetIncludeDeviceName(true); //AdvertiseData.Builder dataManufacturerBuilder = new AdvertiseData.Builder(); //byte[] data = Encoding.UTF8.GetBytes(App.ManufacturerNameValue); //dataManufacturerBuilder.AddManufacturerData(49177, data); myBluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), dataResponseBuilder.Build(), new BleAdvertiseCallback()); _isRunning = true; } catch (System.Exception ex) { } }