Bluetooth通信(Processing:Android編)

<Android端末からNXTのモーターをリモート・コントロール>

動画:https://www.youtube.com/watch?v=eZG0_1j6ayg

動画(ARマーカー):https://www.youtube.com/watch?v=xzeaVdvv-2c

Processingの開発環境にKetaiライブラリを入れておく必要があります

https://code.google.com/p/ketai/

Drive Aボタンを押すと、Aポートにつないだモーターが回転、Stop Aボタンで停止

Drive Bボタンを押すと、Bポートにつないだモータが回転、Stop Bボタンで停止

(NXT側でプログラムを走らせておく必要はありません)

import ketai.camera.*;
import ketai.net.nfc.record.*;
import ketai.net.*;
import ketai.ui.*;
import ketai.cv.facedetector.*;
import ketai.sensors.*;
import ketai.net.nfc.*;
import ketai.net.wifidirect.*;
import ketai.data.*;
import ketai.net.bluetooth.*;


//required for BT enabling on startup

import android.content.Intent;
import android.os.Bundle;
import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;

PFont fontMy;
boolean bReleased = true; //no permament sending when finger is tap
KetaiBluetooth bt;
boolean isConfiguring = true;
String info = "";
KetaiList klist;
ArrayList devicesDiscovered = new ArrayList();

//********************************************************************
// The following code is required to enable bluetooth at startup.
//********************************************************************

void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 bt = new KetaiBluetooth(this);
}

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

void setup() {
 size(displayWidth, displayHeight);
 frameRate(10);
 orientation(PORTRAIT);
 background(0);
 
 //start listening for BT connections
 bt.start();
 //at app start select device…
 isConfiguring = true;
 //font size
 fontMy = createFont("SansSerif", 40);
 textFont(fontMy);
}

void drive(int pnum, int power)
{
  byte[] tele = new byte[15];

  tele[0]=13; // Length of LSB
  tele[1]=0x00; // Length of MSB
  tele[2]=0x00; // Direct Command,with response
  tele[3]=0x04; // Command
  tele[4]=(byte)pnum; // Output Port
  tele[5]=(byte)power; // Power set point

  tele[6]=0x01; // Mode byte:MotorON=0x01,Break=0x02,Reguratied=0x04
  tele[7]=0x00; // Regulation Mode: IDLE=0x00,Speed=0x01,Sync=0x02
  tele[8]=100; // Turn Ratio -100-100
  tele[9]=0x20; // Run Stat  //Iedle=0x00,RampUp=0x10,Running=0x20,RampDown=0x40
  tele[10]=0x00;// Tacho Limit 0:run forever
  tele[11]=0x00;// Tacho Limit 0:run forever
  tele[12]=0x00;// Tacho Limit 0:run forever
  tele[13]=0x00;// Tacho Limit 0:run forever
  tele[14]=0x00;// Tacho Limit 0:run forever

  bt.broadcast(tele);
}

void draw() {
 //at app start select device
 if (isConfiguring)
 {
  ArrayList names;
  background(78, 93, 75);
  klist = new KetaiList(this, bt.getPairedDeviceNames());
  isConfiguring = false;
 }
 else
 {
  background(0,50,0);
  
  
  rect(100, 100, 200, 200);
  rect(100, 500, 200, 200);
  
  rect(500, 100, 200, 200);
  rect(500, 500, 200, 200);
  
  fill(0);
  text("Drive A", 120, 150);
  text("Drive B", 520, 150);
  text("Stop A", 120, 550);
  text("Stop B", 520, 550);
  fill(255);
  
  if((mousePressed) && (bReleased == true))
  {
 //send with BT
 
 if (mouseX < 400) {
   if (mouseY > 400)  drive(0, 0);
   else drive(0, 100);
 } else {
   if (mouseY > 400) drive(1, 0); 
   else drive(1, 100);
 }
 

 
 //first tap off to send next message
  bReleased = false;
  }
  if(mousePressed == false)
  {
  bReleased = true; //finger is up
  }
 //print received data
  fill(255);
  noStroke();
  textAlign(LEFT);
  text(info, 20, 104);
 }
}

void onKetaiListSelection(KetaiList klist) {
 String selection = klist.getSelection();
 bt.connectToDeviceByName(selection);
 //dispose of list for now
 klist = null;
}

//Call back method to manage data received
void onBluetoothDataEvent(String who, byte[] data) {
 if (isConfiguring)
 return;
 //received
 info += new String(data);
 //clean if string to long
 if(info.length() > 150)
 info = "";
}


Last modified: 2014-09-01 Attached files total: 33MB