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); }
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 void SetCharacteristicNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, string UUIDClient) { //Servizi trovati e compatibili //Attiva richiesta notifiche thread = new Thread(new ThreadStart(() => { bool notificationState = false; for (int i = 0; i < 3; i++) { gatt.SetCharacteristicNotification(characteristic, true); BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(Java.Util.UUID.FromString(UUIDClient), GattDescriptorPermission.Write | GattDescriptorPermission.Read); characteristic.AddDescriptor(descriptor); descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray()); gatt.WriteDescriptor(descriptor); /* * BluetoothGattDescriptor desc = characteristic.GetDescriptor(UUID.FromString(UUIDClient)); * desc.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray<Byte>()); * gatt.WriteDescriptor(desc); * gatt.SetCharacteristicNotification(characteristic, true); */ if (WaitCharRecive.WaitOne(5000)) { notificationState = true; if (Notification_Event != null) { Notification_Event(true); } break; } } if (notificationState == false) { if (Notification_Event != null) { Notification_Event(false); } } })); thread.Start(); }
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()); }