Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

Ketai Library Can Not Discover Devices

$
0
0

Hey guys recently i decided to make a multiplayer pong game every player will play on their phones over bluetooth so to accomplish that i downloaded ketai library and started using bluetooth then i noticed that library was not finding any devies (i double checked that all bluetooth devices were visible) then i made some research and people says that you need to add coarse and fine location permissions to your sketch so i did that and it worked for once but then when i restarted the sketch it didnt i checked everything i didnt made any diffrence so can you help me get it working again?

Here is my code:

Pong.pde:

    import oscP5.*;
    import android.os.Bundle;
    import android.view.WindowManager;
    import android.content.res.Configuration;
    import android.content.Intent;
    import ketai.net.bluetooth.*;
    import ketai.ui.*;
    import ketai.net.*;
    KetaiBluetooth bt;
    Ball a;
    Paddle p;
    Paddle q;
    KetaiList klist;
    boolean mainScreen = false;
    boolean configured = false;
    float previous;
    float ballPrevious;
    int p1score;
    int state = 0;
    int p2score;
    float p1y;
    float p2y;

public void onConfigurationChanged(Configuration newConfig) {
  super.getActivity().onConfigurationChanged(newConfig);
}

void onCreate(Bundle bundle) {
  super.onCreate(bundle);
  bt = new KetaiBluetooth(this);
  super.onCreate(bundle);
  getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

void onActivityResult(int requestCode, int resultCode, Intent data) {
  bt.onActivityResult(requestCode, resultCode, data);
}

void setup()
{
  bt.start();
  textSize(72);
  textAlign(CENTER);
  previous = millis();
  q = new Paddle(10, 150, 250);
  p = new Paddle(10, 150, width - 250);
  a = new Ball();
  fullScreen();
  orientation(LANDSCAPE);
}

void draw()
{
  if (configured)
  {
    if (!mainScreen)
    {
      mainScreen();
    } else
    {
      if (touches.length == 2)
      {
        a.move();
        if (touches[0].x > width / 2)
        {
          p1y = touches[0].y;
          p2y = touches[1].y;
        } else
        {
          p1y = touches[1].y;
          p2y = touches[0].y;
        }
      }
      gameScreen();
      a.display();
      a.bounce();
      a.collideRightPaddle();
      a.collideLeftPaddle();
      a.offScreen();
      p.display();
      q.display();
      p.y = p1y;
      q.y = p2y;
      checkTime();
    }
  } else
  {
    bluetoothScreen();
  }
}

void checkTime()
{
  if (millis() - previous > 20000 && !(p.h == 200))
  {
    if (a.xs > 0) a.xs = a.xs + 1;
    if (a.xs < 0) a.xs = a.xs - 1;
    previous = millis();
    q.speed +=1.32;
    p.speed +=1.32;
    p.h += 8;
    q.h += 8;
  }
}

void touchStarted()
{
  if (touches[0].x > width / 2 - 250 && touches[0].y > height / 2 - 125 && touches[0].x < width / 2 + 250 && touches[0].y < height / 2 + 125 && configured) mainScreen = true;
  if (touches[0].y < height / 5 && touches[0].x < width / 3) bt.discoverDevices();
  if (touches[0].y < height / 5 && touches[0].x < width / 3 * 2 && touches[0].x > width / 3 && touches[0].x < width) bt.makeDiscoverable();
  if (touches[0].y < height / 5 && touches[0].x < width && touches[0].x > width / 3 * 2) getDevices();
}

void getDevices()
{
  if (bt.getDiscoveredDeviceNames().size() > 0) {
    ArrayList<String> list = bt.getDiscoveredDeviceNames();
    list.add("CANCEL");
    klist = new KetaiList(this, list);
  } else if (bt.getPairedDeviceNames().size() >= 0) {
    ArrayList<String> list = bt.getPairedDeviceNames();
    list.add("CANCEL");
    klist = new KetaiList(this, list);
  }
}

void onKetaiListSelection(KetaiList klist)
{
  String selection = klist.getSelection();
  if (!selection.equals("CANCEL"))bt.connectToDeviceByName(selection);
  klist = null;
}

Ball.pde:

class Ball
{
  float x = width / 2;
  float y = height / 2;
  float xs = -10;
  float angle = 45;
  float ys = 6;
  float diameter = 40;

  void move()
  {
    x += xs;
    y += ys;
  }

  void display()
  {
    fill(0, 255, 0);
    noStroke();
    ellipse(x, y, diameter, diameter);
    stroke(255);
    fill(255);
  }

  void bounce()
  {
    if ((y + diameter / 2) > height)
    {
      if(ys > 0) ys = -ys;
    }
    if ((y - diameter / 2) < 0)
    {
      if(ys < 0) ys = -ys;
    }
  }

  void collideRightPaddle()
  {
    float pbottom = p.y + p.h / 2;
    float ptop = p.y - p.h /2;
    if (y >= ptop && y <= pbottom && x + diameter / 2 > width - 250 - p.w / 2)
    {
      float place = y - p.y;
      angle = map(place, -(p.h / 2), (p.h / 2), -75, 75);
      ys = map(angle, -75, 75, -xs, xs);
      xs = -xs;
    }
  }

  void collideLeftPaddle()
  {
    float pbottom = q.y + q.h / 2;
    float ptop = q.y - q.h /2;
    if (y >= ptop && y <= pbottom && x - diameter / 2 < 250 + q.w / 2)
    {
      float place = y - q.y;
      angle = map(place, -(q.h / 2), (q.h / 2), 75, -75);
      ys = map(angle, -75, 75, -xs, xs);
      xs = -xs;
    }
  }

  void offScreen()
  {
    if ((x + diameter / 2) >= width - 190)
    {
      x = width / 2;
      y = height / 2;
      p1score++;
    }
    if ((x - diameter / 2) <= 190)
    {
      x = width / 2;
      y = height / 2;
      p2score++;
    }
  }
}

Paddle.pde:

class Paddle
{
  float w;
  float h;
  float x;
  float y = height / 2;
  float speed = 10;
  Paddle(float m, float n, float b)
  {
    w = m;
    h = n;
    x = b;
  }

  void display()
  {
    rectMode(CENTER);
    rect(x, y, w, h);
  }
}

Screen.pde: void mainScreen() { background(0); fill(0, 255, 0); rectMode(CENTER); rect(width / 2, height / 2, 500, 250); fill(255); text("PLAY", width / 2, height / 2 + 25); }

void gameScreen()
{
  background(0);
  stroke(255);
  strokeWeight(3);
  line(width / 2, 0, width / 2, height);
  text(p1score, 200, 200);
  text(p2score, width - 200, 200);
}

void bluetoothScreen()
{
  background(0);
  fill(78, 93, 75);
  rectMode(CORNER);
  stroke(255);
  strokeWeight(3);
  rect(0, 0, width / 3, height / 5);
  rect(width / 3, 0, width / 3 * 2, height / 5);
  rect(width / 3 * 2, 0, width, height / 5);
  textAlign(CENTER);
  fill(255);
  text("Discover Devices", width / 3 / 2, height / 5 / 2);
  text("Make Discoverable", width / 2, height / 5 / 2);
  text("Devices", width / 3 + width / 3 * 1.5, height / 5 / 2);
}

Cant give you manifest file it breaks it but i checked it and there is coarse and fine location permissions as well as bluetooth and bluetooth admin


Viewing all articles
Browse latest Browse all 941

Latest Images

Trending Articles



Latest Images