初始代码包下载:链接:https://pan.baidu.com/s/1nEb5kmk8PabkFmK3bO8Fww 
提取码:r5pu 
解压后用eclipse打开该项目

在src中建立名字为lib的package,

讲这三个.jar文件复制到lib下

导入jar文件方法如下:

大致的目录结构如下:

然后修改brainkeyboard.java代码为:

import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Timer;
import java.util.TimerTask;

import neurosky.ThinkGearSocket;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;

public class brainkeyboard extends PApplet {

/*可读取的参数    int delta, int theta, int low_alpha, int high_alpha,
    int low_beta, int high_beta, int low_gamma, int mid_gamma
*/
ThinkGearSocket neuroSocket;
static int attention = 0;
static int mytheta=0;
static int mydelta=0;
static int mylow_alpha=0;
static int myhigh_alpha=0;
static int mylow_beta=0,myhigh_beta=0;

int blinkSt = 0;
int blink = 0;
int k=0;
/*------------------------------------*/
String[] letter = {"A","B","C","D","E","F","G","H","I","J","K","L"," "," ","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","-","-"};
/*------------------------------------*/
String input = " "; 
String lockinput=" ";
int t=0;
PFont font;
PImage bg;
int i=30,j=110;
/*------------------------------------*/
public void setup() 
{
  size (550, 300);
  ThinkGearSocket neuroSocket = new ThinkGearSocket(this);
  try 
  {
    neuroSocket.start();
  } 
  catch (ConnectException e) {
    e.printStackTrace();
  }
  fill(0);
  font = createFont("Courier",48);
  textFont(font);
  bg=loadImage("1.jpg");

}

/*------------------------------------*/
public void draw() {
  background(120);
  fill(255, 255, 0);
  textSize(12);
  text("Attention: " + attention,100, 10);
  strokeJoin(ROUND);
  stroke(50,100,150);
  strokeWeight(3);
  fill(255);
  rect(30,30,500,50);
// ----------------------------------------
  if (blinkSt>0) 
   {      
        if(letter[k]=="-"){
             input = input.substring(0,input.length()-1);
           }
          else{
                input = input+letter[k];
                lockinput = input;
                 fill(0);
                 textSize(18);
                 text(input, 40, 48, 195, 30);
                 textSize(48);
          }
      }
//-----------------------------------------------------
  
  fill(255, 255, 0);
  textSize(12);
  text("Blink: " + blinkSt, 10, 10);
  
  blinkSt=0;
 fill(0);
 textSize(18);
 text(lockinput, 40, 48, 500, 30);
 textSize(48);
//------------------------------------------------------
  image(bg,30,110);
  noFill();
  stroke(255);
  rect(i,j,50,50);
//  自己加的两行

//   print( "i:"+i +" " + "j:"+ j + " " + "k:"+ k + "  " +"Blinkst:" + blinkSt);
//                 ---------------------------------------------
                if(attention > 40)
                 {
                   t=50;
                   println("  Attention: " + attention);
//                    attention=0;
                  }
                 
                 if(t ==50){
                 
                  if(i<450){
                          i=i+50;
                            }
                        else{
                              i=30;
                              if(j<200)
                                {
                                 j=j+50;
                                }
                                else{
                                    j=110;
                                  }              
                        }
//                -------------------------------------
                
                
                         if(k<29)
                         {
                            k =k+1;
                          }
                          else
                          {k=0;}
                          t=0;
                         
                          
                 }
                 
                 t++;
}
/*------------------------------------*/
public void blinkEvent(int blinkStrength) 
{
  blinkSt = blinkStrength;
 // blink = 1;
}
/*------------------------------------*/ 
public void attentionEvent(int attentionLevel) 
{

attention = attentionLevel;
  
}
public void eegEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma)
{
    mytheta=theta;
    mydelta=delta;
    myhigh_alpha=high_alpha;
    mylow_alpha=low_alpha;
    mylow_beta=low_beta;
    myhigh_beta=high_beta;
    
}
/*------------------------------------*/ 
public void stop() {
  neuroSocket.stop();
  super.stop();
}
//定时
static Timer timer =new Timer(true);   
// 注意,javax.swing包中也有一个Timer类,如果import中用到swing包,要注意名字的冲突。   
  
static TimerTask task = new TimerTask() {   
    public void run() {   
        //每次需要执行的代码放到这里面。 
         
          System.out.println("专注度:"+attention+"  theta:"+mytheta+" mydelta:"+mydelta
            +" mylow_alpha:"+mylow_alpha+" myhigh_alpha:"+myhigh_alpha+" mylow_beta:"+mylow_beta+" myhigh_beta:"+myhigh_beta);
         System.out.println( System.currentTimeMillis());
          try {
            PreparedStatement ps=conn.prepareStatement("insert into biao values(?,?,?,?,?,?,?)");
          ps.setInt(1, attention );
          ps.setInt(2, mytheta);
          ps.setInt(3, mydelta);
          ps.setInt(4, mylow_alpha);
          ps.setInt(5, myhigh_alpha);
          ps.setInt(6,mylow_beta );
          ps.setInt(7, myhigh_beta);
          ps.execute();
          System.out.println("zhou");
          } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }   
};

// JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
//"jdbc:mysql://localhost:3306/RUNOOB";
static final String DB_URL = 
"jdbc:mysql://localhost:3306/zhou?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true";
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "123456";
static Connection conn = null;

static public void main(String args[]) {
      try{
          // 注册 JDBC 驱动
          Class.forName(JDBC_DRIVER);
      
          // 打开链接
          System.out.println("连接数据库...");
          conn = DriverManager.getConnection(DB_URL,USER,PASS);
      }catch(SQLException se){
          // 处理 JDBC 错误
          se.printStackTrace();
      }catch(Exception e){
          // 处理 Class.forName 错误
          e.printStackTrace();
      }
            timer.schedule(task,10000,1000);
  PApplet.main(new String[] { "--bgcolor=#ECE9D8", "brainkeyboard" });

}

}

功能:读取脑波耳机中的α、β、attention等数据值存放到mysql数据库,1秒钟更新一次

数据如下:

ThinkGear.jar源代码如下:

/*
  *
  * This provides a simple socket connector to the NeuroSky MindWave ThinkGear connector.
  * For more info visit http://crea.tion.to/processing/thinkgear-java-socket
  *
  * No warranty or any stuffs like that.
  *
  * Have fun!
  * Andreas Borg
  * borg@elevated.to
  *
  *
  * (c) 2010
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
  * Public License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  * Boston, MA 02111-1307 USA
  *
  * @author Andreas Borg, borg@elevated.to
  * @modified June, 2011
  * @version 1.0
  *
  *
  * This library is following the same design as the one developed by Jorge C. S. Cardoso for the MindSet device.
  * The MindWave device can communicate to a socket over JSON instead of the serial port. That makes it easier and tidier
  * to talk between the device and Java. For instructions on how to use the callback listeners please refer to
  *
  * http://jorgecardoso.eu/processing/MindSetProcessing/
  *
  *
  * Data is passed back to the application via the following callback methods:
  *
  *
  * public void attentionEvent(int attentionLevel)
  * Returns the current attention level [0, 100].
  * Values in [1, 20] are considered strongly lowered.
  * Values in [20, 40] are considered reduced levels.
  * Values in [40, 60] are considered neutral.
  * Values in [60, 80] are considered slightly elevated.
  * Values in [80, 100] are considered elevated.
  *
  * public void meditationEvent(int meditationLevel)
  * Returns the current meditation level [0, 100].
  * The interpretation of the values is the same as for the attentionLevel.
  *
  *
  * public void poorSignalEvent(int signalLevel)
  * Returns the signal level [0, 200]. The greater the value, the more noise is detected in the signal.
  * 200 is a special value that means that the ThinkGear contacts are not touching the skin.
  *
  *
  * public void eegEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma) </code><br>
  * Returns the EEG data. The values have no units.
  *
  *
  *
  * public void rawEvent(int [])
  * Returns the the current 512 raw signal samples [-32768, 32767].
  *
  *
  */
  package neurosky;
   
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.lang.reflect.Method;
  import java.net.ConnectException;
  import java.net.Socket;
  import java.net.SocketException;
  import java.net.UnknownHostException;
  import java.util.Iterator;
   
  import org.json.JSONException;
  import org.json.JSONObject;
   
  import processing.core.PApplet;
   
  public class ThinkGearSocket implements Runnable{
  public PApplet parent;
  public Socket neuroSocket;
  public OutputStream outStream;
  public InputStream inStream;
  public BufferedReader stdIn;
  private Method attentionEventMethod = null;
  private Method meditationEventMethod = null;
  private Method poorSignalEventMethod = null;
  private Method blinkEventMethod = null;
  private Method eegEventMethod = null;
  private Method rawEventMethod = null;
  public String appName="";
  public String appKey="";
  private Thread t;
   
  private int raw[] = new int[512];
  private int index = 0;
   
  public final static String VERSION = "1.0";
   
   
   
  private boolean running = true;
  public ThinkGearSocket(PApplet _parent, String _appName,String _appKey){
  this(_parent);
  appName = _appName;//these were mentioned in the documentation as required, but test prove they are not.
  appKey = _appKey;
   
  }
   
  public ThinkGearSocket(PApplet _parent){
  parent = _parent;
  try {
  attentionEventMethod =
  parent.getClass().getMethod("attentionEvent", new Class[] {
  int.class
  }
  );
   
  }
  catch (Exception e) {
  System.err.println("attentionEvent() method not defined. ");
  }
   
  try {
  meditationEventMethod =
  parent.getClass().getMethod("meditationEvent", new Class[] {
  int.class
  }
  );
  }
  catch (Exception e) {
  System.err.println("meditationEvent() method not defined. ");
  }
  try {
  poorSignalEventMethod =
  parent.getClass().getMethod("poorSignalEvent", new Class[] {
  int.class
  }
  );
  }
  catch (Exception e) {
  System.err.println("poorSignalEvent() method not defined. ");
  }
   
  try {
  blinkEventMethod =
  parent.getClass().getMethod("blinkEvent", new Class[] {
  int.class
  }
  );
  }
  catch (Exception e) {
  System.err.println("blinkEvent() method not defined. ");
  }
  try {
  eegEventMethod =
  parent.getClass().getMethod("eegEvent", new Class[] {
  int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class
  }
  );
  }
  catch (Exception e) {
  System.err.println("eegEvent() method not defined. ");
  }
   
  try {
  rawEventMethod =
  parent.getClass().getMethod("rawEvent", new Class[] {
  int[].class
  }
  );
  }
  catch (Exception e) {
  System.err.println("rawEvent() method not defined. ");
  }
   
   
   
   
  }
   
   
  public boolean isRunning(){
  return running;
  }
   
  /**
  * return the version of the library.
  *
  * @return String
  */
  public static String version() {
  return VERSION;
  }
   
   
  public void start() throws ConnectException{
   
  try {
  neuroSocket = new Socket("127.0.0.1",13854);
  } catch (ConnectException e) {
  //e.printStackTrace();
  System.out.println("Oi plonker! Is ThinkkGear running?");
  running = false;
  throw e;
  } catch (UnknownHostException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }
   
  try {
  inStream = neuroSocket.getInputStream();
  outStream = neuroSocket.getOutputStream();
  stdIn = new BufferedReader(new InputStreamReader(neuroSocket.getInputStream()));
  running = true;
  } catch (IOException e) {
  e.printStackTrace();
  }
   
   
   
   
   
  if(appName !="" && appKey !=""){
  JSONObject appAuth = new JSONObject();
  try {
  appAuth.put("appName", appName);
  } catch (JSONException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
  }
  try {
  appAuth.put("appKey", appKey);
  } catch (JSONException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
  }
  //throws some error
  sendMessage(appAuth.toString());
  System.out.println("appAuth"+appAuth);
  }
   
   
  JSONObject format = new JSONObject();
  try {
  format.put("enableRawOutput", true);
  } catch (JSONException e) {
  // TODO Auto-generated catch block
  System.out.println("raw error");
  e.printStackTrace();
  }
  try {
  format.put("format", "Json");
  } catch (JSONException e) {
  // TODO Auto-generated catch block
  System.out.println("Json error");
  e.printStackTrace();
  }
  //System.out.println("format "+format);
  sendMessage(format.toString());
  t = new Thread(this);
  t.start();
   
  }
   
   
  @SuppressWarnings("deprecation")
  public void stop(){
   
  if(running){
  t.interrupt();
  try {
   
  neuroSocket.close();
   
  inStream.close();
  outStream.close();
  stdIn.close();
  stdIn = null;
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  //System.out.println("Socket close issue");
  }
   
   
  }
  running = false;
  }
  public void sendMessage(String msg){
  PrintWriter out = new PrintWriter(outStream, true);
  //System.out.println("sendmsg");
  out.println(msg);
  }
  @Override
  public void run() {
  if(running && neuroSocket.isConnected()){
  String userInput;
   
  try {
  while ((userInput = stdIn.readLine()) != null) {
   
  String[] packets = userInput.split("/\r/");
  for(int s=0;s<packets.length;s++){
  if(((String) packets[s]).indexOf("{")>-1){
  JSONObject obj = new JSONObject((String) packets[s]);
  parsePacket(obj);
  }
   
  //String name = obj.get("name").toString();
  }
   
   
  }
  }
  catch(SocketException e){
  //System.out.println("For some reason stdIn throws error even if closed");
  //maybe it takes a cycle to close properly?
  //e.printStackTrace();
  }
  catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } catch (JSONException e) {
  // TODO Auto-generated catch block
  //e.printStackTrace();
  }
  parent.delay(50);
  }else{
  running = false;
  }
  }
   
   
   
  private void triggerAttentionEvent(int attentionLevel) {
  if (attentionEventMethod != null) {
  try {
  attentionEventMethod.invoke(parent, new Object[] {
  attentionLevel
  }
  );
  }
  catch (Exception e) {
  System.err.println("Disabling attentionEvent() because of an error.");
  e.printStackTrace();
  attentionEventMethod = null;
  }
  }
  }
   
  private void triggerMeditationEvent(int meditationLevel) {
  if (meditationEventMethod != null) {
  try {
  meditationEventMethod.invoke(parent, new Object[] {
  meditationLevel
  }
  );
  //println("Attention: " + attention);
  }
  catch (Exception e) {
  System.err.println("Disabling meditationEvent() because of an error.");
  e.printStackTrace();
  meditationEventMethod = null;
  }
  }
  }
   
  private void triggerPoorSignalEvent(int poorSignalLevel) {
  if (poorSignalEventMethod != null) {
  try {
  poorSignalEventMethod.invoke(parent, new Object[] {
  poorSignalLevel
  }
  );
  //println("Attention: " + attention);
  }
  catch (Exception e) {
  System.err.println("Disabling meditationEvent() because of an error.");
  e.printStackTrace();
  poorSignalEventMethod = null;
  }
  }
  }
   
  private void triggerBlinkEvent(int blinkStrength) {
  if (blinkEventMethod != null) {
  try {
  blinkEventMethod.invoke(parent, new Object[] {
  blinkStrength
  }
  );
  }
  catch (Exception e) {
  System.err.println("Disabling blinkEvent() because of an error.");
  e.printStackTrace();
  blinkEventMethod = null;
  }
  }
  }
   
  private void triggerEEGEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma) {
  if (eegEventMethod != null) {
  try {
  eegEventMethod.invoke(parent, new Object[] {
  delta, theta, low_alpha, high_alpha, low_beta, high_beta, low_gamma, mid_gamma
  }
  );
  }
  catch (Exception e) {
  System.err.println("Disabling eegEvent() because of an error.");
  e.printStackTrace();
  eegEventMethod = null;
  }
  }
  }
   
   
  private void triggerRawEvent(int []values) {
  if (rawEventMethod != null) {
  try {
  rawEventMethod.invoke(parent, new Object[] {
  values
  }
  );
  }
  catch (Exception e) {
  System.err.println("Disabling rawEvent() because of an error.");
  e.printStackTrace();
  rawEventMethod = null;
  }
  }
  }
   
   
  private void parsePacket(JSONObject data){
  Iterator itr = data.keys();
  while(itr.hasNext()) {
   
  Object e = itr.next();
  String key = e.toString();
   
  try{
   
  if(key.matches("poorSignalLevel")){
  triggerPoorSignalEvent(data.getInt(e.toString()));
   
  }
  if(key.matches("rawEeg")){
  int rawValue = (Integer) data.get("rawEeg");
  raw[index] = rawValue;
  index++;
  if (index == 512) {
  index = 0;
  int rawCopy[] = new int[512];
  parent.arrayCopy(raw, rawCopy);
  triggerRawEvent(rawCopy);
  }
  }
  if(key.matches("blinkStrength")){
  triggerBlinkEvent(data.getInt(e.toString()));
   
  }
   
  if(key.matches("eSense")){
  JSONObject esense = data.getJSONObject("eSense");
  triggerAttentionEvent(esense.getInt("attention"));
  triggerMeditationEvent(esense.getInt("meditation"));
   
  }
  if(key.matches("eegPower")){
  JSONObject eegPower = data.getJSONObject("eegPower");
  triggerEEGEvent(eegPower.getInt("delta"), eegPower.getInt("theta"), eegPower.getInt("lowAlpha"), eegPower.getInt("highAlpha"),eegPower.getInt("lowBeta"), eegPower.getInt("highBeta"),eegPower.getInt("lowGamma"), eegPower.getInt("highGamma"));
   
  //System.out.println(key);
  }
   
   
  }
  catch(Exception ex){
   
  ex.printStackTrace();
  }
  }
   
  //
  }
  }

java程序获取MindWave Mobile 2脑波耳机数据+二次开发相关推荐

  1. JAVA程序获取Tomcat的运行状态

    2019独角兽企业重金招聘Python工程师标准>>> 用浏览器来查看Tomcat的运行状态: 配置Tomcat的管理用户和权限. 打开%Tomcat_Home%/conf/目录下的 ...

  2. Java 程序获取本机 ip 地址

    Java程序获取本机ip地址: host_ip_list = new ArrayList<String>(); try {for (NetworkInterface networkInte ...

  3. java程序获取时区不对,linux时区不正确解决整理

    背景: 运行在linux系统的java程序的时区不对,操作系统时区改正了java程序还是不对. 参考: 1.linux 中 时区不对 及 date 时间 和 hwclock 时间不一致解决方案 2.l ...

  4. linux命令获取本机外网公网ip地址,java程序获取本机外网公网ip地址 代码

    linux命令获取本机外网ip地址 1 直接返回ip 最快 稳定 curl ns1.dnspod.net:6666 curl whatismyip.akamai.com curl icanhazip. ...

  5. Java程序员必备!Dagger2源码分析(二(1)

    提起hook概念,相信大家都听说过,然后hook就是将反射和动态代理运用于Android平台源码之上的.可是,反射的应用场景缺非常广泛,注解的内容也同样需要使用反射去获取,所以反射是我们Android ...

  6. java实现这个无表头空表格,《java程序导出excel表格是空白的没有数据?》 java怎么导入数据库...

    java程序导出excel表格是空白的没有数据? 看一下是否可以链接上数据库,或者查看一下链接的数据库是否有数据 java从数据库中导出excel poi 建议你使用pageoffice. java ...

  7. Java程序获取和修改.wav音频文件的内部结构

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/79498075冷血之心的博客) wav音频文件是一种无损的音频文件, ...

  8. Java程序员从笨鸟到菜鸟之(二十一)java过滤器和监听器详解

     过滤器 1.Filter工作原理(执行流程) 当客户端发出Web资源的请求时,Web服务器根据应用程序配置文件设置的过滤规则进行检查,若客户请求满足过滤规则,则对客户请求/响应进行拦截,对请求头和请 ...

  9. Java程序员从笨鸟到菜鸟之(十二)java异常处理机制

    异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C开始,你也许已经知道如何用if...else...来控制异常了,也许是自发的,然而这种控制异常痛苦,同一个异常或者错误如果多个地方出 ...

最新文章

  1. ATS 5.3.0日志字段分析(续)
  2. 英语作文题目计算机,跪求一篇英语作文 题目:论计算机的优缺点
  3. 发力企业云,用友是要掉队,还是进击?
  4. 斯坦福cs161算法考试的cheat sheet!!!十分重要!!!
  5. GNU make manual 翻译(七十三)
  6. hdu 4091 线性规划
  7. 新服务器安装和配置zabbix的playbook
  8. lfsr算法c语言,求助:如何用C语言实现LFSR加密
  9. 基建管控系统_催生智慧园区三维管控系统解决方案,新基建背景下构建可视化监控平台...
  10. ztree 指定节点清空_zTree节点文字过多的处理方法
  11. 找出数组的最大公约数
  12. erlang web服务器性能,100万并发连接服务器笔记之Erlang完成1M并发连接目标
  13. RTX3090ti和rtx2080ti参数对比
  14. 浅谈getch(),getche(),getchar(),gets(),scanf()的区别
  15. [2008北京奥运会]08奥运开幕式NBC版的字幕/解说词
  16. 阿里云FinalShell连接
  17. 写了个移动端可滑动(惯性滑动回弹)Vue导航栏组件 ly-tab
  18. 408计算机组成原理学习笔记——计算机系统概述
  19. 中国ABS市场发展前景与投资策略建议报告2022-2028年
  20. JAVA第四次作业——使用枚举类对扑克牌建模

热门文章

  1. drx功能开启后_DRX功能开启指导书(中国移动推荐参数)
  2. 仙人掌之歌——系统设计(1)
  3. Android Studio NDK 开发
  4. Java web图书管理系统——数据库课程设计
  5. 用python画函数的梯度图_只需45秒,用Python给故宫画一组雪景手绘图
  6. tp5 实现文章上一篇和下一篇翻页
  7. matlab如何转换哑变量,SPSS教程:手把手教你设置哑变量及解读结果!
  8. Marp—一款让你快速做ppt的神奇小众软件
  9. 百度:地震姜常宏,求传播?!!
  10. “拷贝中国”已成硅谷创业趋势