public Device(BluetoothManager manager, BluetoothDevice native, GattCallbacks callbacks) : base(native.Name, ToDeviceId(native.Address)) { this.connSubject = new Subject <ConnectionStatus>(); this.connFailSubject = new Subject <GattStatus>(); this.context = new DeviceContext(native, callbacks); this.manager = manager; }
public Device(BluetoothManager manager, BluetoothDevice native, GattCallbacks callbacks, TaskScheduler scheduler) : base(native.Name, ToDeviceId(native.Address)) { this.context = new GattContext(native, callbacks); this.manager = manager; this.scheduler = scheduler; // this is the thread that the device was scanned on (required by some devices) this.connSubject = new Subject <ConnectionStatus>(); }
protected override Task ConnectAsync() { if (bluetoothAdapter == null) { return(Task.FromException(new BluetoothLEException("BluetoothAdapter not initialized."))); } if (bluetoothGatt == null) { gattCallback = new GattCallbacks( (gatt, status, newState) => // onConnectionStateChange { if (newState == ProfileState.Connected) { //intentAction = BluetoothLeService.ACTION_GATT_CONNECTED; //BluetoothLeService.this.mConnectionState = 2; //BluetoothLeService.this.broadcastUpdate(intentAction); System.Diagnostics.Debug.WriteLine("Connected to GATT server."); System.Diagnostics.Debug.WriteLine($"Attempting to start service discovery:{gatt?.DiscoverServices()}"); } else if (newState == ProfileState.Disconnected) { //intentAction = BluetoothLeService.ACTION_GATT_DISCONNECTED; //BluetoothLeService.this.mConnectionState = 0; System.Diagnostics.Debug.WriteLine("Disconnected from GATT server."); //BluetoothLeService.this.broadcastUpdate(intentAction); } }, (gatt, status) => // onServicesDiscovered { if (status == GattStatus.Success) { //BluetoothLeService.this.broadcastUpdate(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED); foreach (var service in gatt.Services) { System.Diagnostics.Debug.WriteLine($"GattService, UUID=[{service.Uuid}]"); } Services.AddRange(gatt.Services.Select(S => new BluetoothGattServiceAndroid(this, S))); RaiseServicesUpdated(); } else { System.Diagnostics.Debug.WriteLine($"onServicesDiscovered received: {status}"); } }, (gatt, characteristic, status) => // onCharacteristicRead { //BluetoothLeService.this.broadcastUpdate(BTConstants.responseMapMapLookup(status, "UnKown Response"), characteristic); }, (gatt, characteristic) => // onCharacteristicChanged { //BluetoothLeService.this.broadcastUpdate(BluetoothLeService.ACTION_CHAR_CHANGED, characteristic); }, (gatt, characteristic, status) => // onCharacteristicWrite { //BluetoothLeService.this.broadcastUpdate(BTConstants.responseMapMapLookup(status, "UnKown Response"), characteristic); }, (gatt, descriptor, status) => // onDescriptorRead { //BluetoothLeService.this.broadcastUpdate(BTConstants.responseMapMapLookup(status, "UnKown Response"), descriptor); }, (gatt, descriptor, status) => // onDescriptorWrite { //BluetoothLeService.this.broadcastUpdate(BTConstants.responseMapMapLookup(status, "UnKown Response"), descriptor); }, (gatt, status) => //onReliableWriteCompleted { //BluetoothLeService.this.broadcastUpdate(BTConstants.responseMapMapLookup(status, "UnKown Response")); }, (gatt, rssi, status) => //onReadRemoteRssi { //BluetoothLeService.this.broadcastUpdate(BTConstants.responseMapMapLookup(status, "UnKown Response"), rssi); }); bluetoothGatt = device.ConnectGatt(Android.App.Application.Context, false, gattCallback); //Log.d(TAG, "Trying to create a new connection."); //this.mBluetoothDeviceAddress = address; //this.mConnectionState = 1; //return true; return(Task.CompletedTask); } else { //Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); if (bluetoothGatt.Connect() != true) { return(Task.FromException(new BluetoothLEException("BluetoothGatt connect fail."))); } else { //this.mConnectionState = 1; return(Task.CompletedTask); } } }