コード例 #1
0
        public override void OnConnectionStateChange(BluetoothGatt gatt, [GeneratedEnum] GattStatus status, [GeneratedEnum] ProfileState newState)
        {
            base.OnConnectionStateChange(gatt, status, newState);

            var args = new BLEGATTProfileStateStatusEventArgs(gatt, status, newState);

            _wes.RaiseEvent(nameof(ConnectionStateChanged), this, args);
        }
コード例 #2
0
        private void OnConnectionStateChanged(object sender, BLEGATTProfileStateStatusEventArgs e)
        {
            switch (e.NewState)
            {
            case ProfileState.Connected:
            {
                // Android <= 4.4 时,若连接失败,触发此处
                if (e.Status != GattStatus.Success)
                {
                    CloseGATT();

                    RaiseConnectFailed(e.Status.ToShared());
                }
                else
                {
                    RaiseConnected();
                }

                break;
            }

            case ProfileState.Disconnected:
            {
                // 手动断开后再次连接会非常慢,所以释放掉再次连接
                if (e.Status != GattStatus.Success)
                {
                    CloseGATT();
                }

                switch ((int)e.Status)
                {
                case 0:
                {
                    RaiseDisconnected();
                    break;
                }

                case 8:             // 对方蓝牙关闭
                case 19:            // 对方蓝牙主动断开
                {
                    RaiseConnectionLost(e.Status.ToShared());
                    break;
                }

                case 133:
                {
                    RaiseConnectFailed(e.Status.ToShared());
                    break;
                }

                default:                // Android > 5.0 时,若连接失败,触发此处
                {
                    RaiseConnectFailed(e.Status.ToShared());
                    break;
                }
                }

                break;
            }

            case ProfileState.Connecting:
            case ProfileState.Disconnecting:
            default:
            {
                break;
            }
            }
        }