Wi-Fi Direct的连接问题

时间:2017-01-28 13:06:41

标签: android wifi wifi-direct wifip2p

我正在开发一款点对点Android游戏。为此,我使用 Wi-Fi Direct 在两台设备之间建立连接。我能够建立连接。我已按照 Google文档设置连接。

问题:用户A可以向用户B发送连接请求。用户B通过接受或拒绝该请求来响应该请求。当用户B拒绝连接请求时,用户A无法获得对连接请求的任何响应。

为了解决这个问题,每当用户A向用户B发送请求时,我就在用户A的终点(发送者)启动了20秒计时器。另外,我想在连接请求到达时在用户B的结束处启动计时器。现在,当请求到达时,我无法弄清楚如何在接收器端启动定时器。

问题:

  1. 有没有办法在接收方端识别到达连接请求?
  2. 当用户B拒绝连接请求时,如何处理用户A端(发送方)的连接请求响应?

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends AppCompatActivity implements FlingCardListener.ActionDownInterface{
    public static MyAppAdapter myAppAdapter;
    public static ViewHolder viewHolder;
    private ArrayList<Data> al;
    private SwipeFlingAdapterView flingContainer;

    public static void removeBackground() {
        viewHolder.background.setVisibility(View.GONE);
        myAppAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);

        al = new ArrayList<>();
        al.add(new Data("http://www.drodd.com/images15/1-7.jpg", "Hello Anuj"));
        al.add(new Data("http://www.drodd.com/images15/2-23.jpg", "Welcome to the final problem"));
        al.add(new Data("http://www.drodd.com/images15/3-12.jpg","\t here goes the name of the person "));
        al.add(new Data("http://www.drodd.com/images15/4-7.jpg", " buhahahahahahahaha"));
        al.add(new Data("http://www.drodd.com/images15/5-18.jpg", "miss me?"));
        myAppAdapter = new MyAppAdapter(al, MainActivity.this);
        flingContainer.setAdapter(myAppAdapter);
        flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {

            }

            @Override
            public void onLeftCardExit(Object dataObject) {
                al.remove(0);
                myAppAdapter.notifyDataSetChanged();
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject

            }

            @Override
            public void onRightCardExit(Object dataObject) {

                al.remove(0);
                myAppAdapter.notifyDataSetChanged();
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {

            }

            @Override
            public void onScroll(float scrollProgressPercent) {

                View view = flingContainer.getSelectedView();
                view.findViewById(R.id.background).setAlpha(0);
                view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
                view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
            }
        });


        // Optionally add an OnItemClickListener
        flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
            @Override
            public void onItemClicked(int itemPosition, Object dataObject) {

                View view = flingContainer.getSelectedView();
                view.findViewById(R.id.background).setAlpha(0);

                myAppAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onActionDownPerform() {
        Log.e("action", "bingo");
    }

    public static class ViewHolder {
        public static FrameLayout background;
        public TextView DataText;
        public ImageView cardImage;


    }

    public class MyAppAdapter extends BaseAdapter {


        public List<Data> parkingList;
        public Context context;

        private MyAppAdapter(List<Data> apps, Context context) {
            this.parkingList = apps;
            this.context = context;
        }

        @Override
        public int getCount() {
            return parkingList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View rowView = convertView;


            if (rowView == null) {

                LayoutInflater inflater = getLayoutInflater();
                rowView = inflater.inflate(R.layout.item, parent, false);
                // configure view holder
                viewHolder = new ViewHolder();
                viewHolder.DataText = (TextView) rowView.findViewById(R.id.bookText);
                viewHolder.background = (FrameLayout) rowView.findViewById(R.id.background);
                viewHolder.cardImage = (ImageView) rowView.findViewById(R.id.cardImage);
                rowView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.DataText.setText(parkingList.get(position).getDescription() + "");

            Glide.with(MainActivity.this).load(parkingList.get(position).getImagePath()).into(viewHolder.cardImage);

            return rowView;
        }
    }
}

如果请求被拒绝或者遇到某些错误而不是onFaliure,则此方法与对等方连接将使用原因码调用。