Java 将字符串从AndroidLauncher发送到MyGdxGame
我正在尝试创建一个国际象棋应用程序,我从蓝牙获取移动信息,然后我需要将其发送到位于核心类中的方法。另外,我需要在单击核心类中的按钮时启动Java 将字符串从AndroidLauncher发送到MyGdxGame,java,android,libgdx,native,Java,Android,Libgdx,Native,我正在尝试创建一个国际象棋应用程序,我从蓝牙获取移动信息,然后我需要将其发送到位于核心类中的方法。另外,我需要在单击核心类中的按钮时启动AndroidLauncher中的函数 AndroidLauncher: public class AndroidLauncher extends AndroidApplication { public static final int CONNECTION_TIMEOUT=10000; public static final int REA
AndroidLauncher
中的函数
AndroidLauncher:
public class AndroidLauncher extends AndroidApplication {
public static final int CONNECTION_TIMEOUT=10000;
public static final int READ_TIMEOUT=15000;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice = null;
final byte delimiter = 33;
int readBufferPosition = 0;
public void sendBtMsg(String msg2send){
//UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
UUID uuid = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee"); //Standard SerialPortService ID
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
if (!mmSocket.isConnected()){
mmSocket.connect();
}
String msg = msg2send;
//msg += "\n";
OutputStream mmOutputStream = mmSocket.getOutputStream();
mmOutputStream.write(msg.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
MyGdxGame game = new MyGdxGame();
View gameView = initializeForView(game, config);
initialize(new MyGdxGame(), config);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
final class workerThread implements Runnable {
private String btMsg;
public workerThread(String msg) {
btMsg = msg;
}
public void run()
{
sendBtMsg(btMsg);
while(1 == 1) {
while (!Thread.currentThread().isInterrupted()) {
int bytesAvailable;
boolean workDone = false;
try {
final InputStream mmInputStream;
mmInputStream = mmSocket.getInputStream();
bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
byte[] readBuffer = new byte[1024];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
//The variable data now contains our full command
handler.post(new Runnable() {
public void run() {
char first = data.charAt(0);
if( first == 'm'){
new AsyncLogin().execute(data.substring(1),"-", "0");
} else {
new AsyncLogin().execute("-",data, "0");
}
//myLabel.setText(data);
}
});
workDone = true;
break;
} else {
readBuffer[readBufferPosition++] = b;
}
}
if (workDone == true){
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
// end light off button handler
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("raspberrypi")) //Note, you will need to change this to match the name of your device
{
Log.e("Aquarium",device.getName());
mmDevice = device;
break;
}
}
}
}
公共类AndroidLauncher扩展了AndroidApplication{
公共静态最终int连接\u超时=10000;
公共静态最终整型读取超时=15000;
蓝牙插座;
蓝牙设备mmDevice=null;
最后一个字节分隔符=33;
int readBufferPosition=0;
public void sendBtMsg(字符串msg2send){
//UUID UUID=UUID.fromString(“00001101-0000-1000-8000-00805f9b34fb”);//标准SerialPortService ID
UUID UUID=UUID.fromString(“94f39d29-7d6d-437d-973b-fba39e49d4ee”);//标准串行端口服务ID
试一试{
mmSocket=mmDevice.createRfcommSocketToServiceRecord(uuid);
如果(!mmSocket.isConnected()){
mmSocket.connect();
}
字符串msg=msg2send;
//msg+=“\n”;
OutputStream mmOutputStream=mmSocket.getOutputStream();
mmOutputStream.write(msg.getBytes());
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration配置=新的AndroidApplicationConfiguration();
MyGdxGame=new MyGdxGame();
查看游戏视图=初始化ForView(游戏,配置);
初始化(新建MyGdxGame(),配置);
BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
最后一个类workerThread实现Runnable{
私有字符串btMsg;
公共workerThread(字符串消息){
btMsg=msg;
}
公开募捐
{
sendBtMsg(btMsg);
而(1==1){
而(!Thread.currentThread().isInterrupted()){
int字节可用;
布尔值1=假;
试一试{
最终输入流mmInputStream;
mmInputStream=mmSocket.getInputStream();
bytesAvailable=mmInputStream.available();
如果(字节可用>0){
byte[]packetBytes=新字节[bytesAvailable];
字节[]读缓冲区=新字节[1024];
mmInputStream.read(packetBytes);
for(int i=0;i0)
{
用于(蓝牙设备:pairedDevices)
{
如果(device.getName().equals(“raspberrypi”)//注意,您需要更改此选项以匹配设备的名称
{
Log.e(“水族馆”,device.getName());
mmDevice=设备;
打破
}
}
}
}
MyGdxGame
public class MyGdxGame extends ApplicationAdapter {
public static int V_WIDTH = 720;//720
public static int V_HEIGHT = 1280;//1280
private String slogin = "Prisijungimas",sreg= "Registracija",sai =
"Kompiuteris",shuman = "Ieškoti oponento",sboard;
SpriteBatch batch;
Texture bdt,blt,kdt,klt,ndt,nlt,pdt,plt,qdt,qlt,rdt,rlt;
OrthographicCamera camera;
public Viewport viewport;
private ShowScreen screen = ShowScreen.login;
BitmapFont font,font2;
float rh, rw;
float startpos[][][] = new float[9][9][2];//cia grido pozicijos [eile]
[stulpelis][x/y]
float fig_pos[][]= new float[32][6];// [figuros id]
[x/y/newx/newy/movingx/movingy]
int fig_laukelis[][] = new int[32][2];//[figuros id][eile/stulpelis]
//figuru movinimui
int oldRow;
int newRow;
int oldCol;
int newCol;
ShapeRenderer shape;//renderina shapus as buttons
GlyphLayout layout;
//test string
String test = "move example 1";
@Override
public void create() {
//kai kuriuos var'us reikia initialaizint dar karta kad sukurti ale tuscia array
batch = new SpriteBatch();
shape = new ShapeRenderer();
font = new BitmapFont();//cia kad spausdinti texta
font2 = new BitmapFont();//cia kad spausdinti texta lentoj
camera = new OrthographicCamera(); //nustatom cameros dydi
viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),camera);//fitinam i screena
viewport.apply();//reikia apply kitaip viewporto dydis nebus issaugotas
//gaunam ratio
rw = (float) Gdx.graphics.getWidth() / (float) V_WIDTH;
rh = (float) Gdx.graphics.getHeight() / (float) V_HEIGHT;
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);//nustatom cameros pozicija per viduti cameros view
//uzrkaunam img
//prima uzkraunam i manageri
bdt = new Texture("chess_fig/chess_bdt60.png");
blt = new Texture("chess_fig/chess_blt60.png");
kdt = new Texture("chess_fig/chess_kdt60.png");
klt = new Texture("chess_fig/chess_klt60.png");
ndt = new Texture("chess_fig/chess_ndt60.png");
nlt = new Texture("chess_fig/chess_nlt60.png");
pdt = new Texture("chess_fig/chess_pdt60.png");
plt = new Texture("chess_fig/chess_plt60.png");
qdt = new Texture("chess_fig/chess_qdt60.png");
qlt = new Texture("chess_fig/chess_qlt60.png");
rdt = new Texture("chess_fig/chess_rdt60.png");
rlt = new Texture("chess_fig/chess_rlt60.png");
//font
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("comics_bold.ttf"));//gali pasikeisti i bet koki kita fonta jei nori
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = (int) (50*(rw/rh));//dydis
font = generator.generateFont(parameter);///issaugom sugeneratinta fonta
font.setColor(255 / 255f, 255 / 255f, 255 / 255f, 1);
parameter.size = (int)(150*(rw/rh));
font2 = generator.generateFont(parameter);
font2.setColor(Color.BLACK);
//gaunam grida
for(int stulpeis = 0;stulpeis<9;stulpeis++){
for(int eile = 0;eile<9;eile++){
startpos[eile][stulpeis][0]=0+(camera.viewportWidth/9*stulpeis);//x
startpos[eile][stulpeis][1]=0+(camera.viewportWidth/9*eile);//y
}
}
private void movefig(String coord) {
//nuskaitom stringa ir priskiriam teisingas reiksmes
for (int index = 0; index < 4;
index++) {
char aChar = coord.charAt(index);
if(index==0){
if(aChar=='A'){
oldCol=1;
}else if(aChar=='B'){
oldCol=2;
}else if(aChar=='C'){
oldCol=3;
}else if(aChar=='D'){
oldCol=4;
}else if(aChar=='E'){
oldCol=5;
}else if(aChar=='F'){
oldCol=6;
}else if(aChar== 'G'){
oldCol=7;
}else if(aChar == 'H'){
oldCol=8;
}
}else if(index==1){
if(aChar=='1'){
oldRow=1;
}else if(aChar=='2'){
oldRow=2;
}else if(aChar=='3'){
oldRow=3;
}else if(aChar=='4'){
oldRow=4;
}else if(aChar=='5'){
oldRow=5;
}else if(aChar=='6'){
oldRow=6;
}else if(aChar== '7'){
oldRow=7;
}else if(aChar == '8'){
oldRow=8;
}
}else if(index==2){
if(aChar=='A'){
newCol=1;
}else if(aChar=='B'){
newCol=2;
}else if(aChar=='C'){
newCol=3;
}else if(aChar=='D'){
newCol=4;
}else if(aChar=='E'){
newCol=5;
}else if(aChar=='F'){
newCol=6;
}else if(aChar== 'G'){
newCol=7;
}else if(aChar == 'H'){
newCol=8;
}
}else if(index==3){
if(aChar=='1'){
newRow=1;
}else if(aChar=='2'){
newRow=2;
}else if(aChar=='3'){
newRow=3;
}else if(aChar=='4'){
newRow=4;
}else if(aChar=='5'){
newRow=5;
}else if(aChar=='6'){
newRow=6;
}else if(aChar== '7'){
newRow=7;
}else if(aChar == '8'){
newRow=8;
}
}
}
//ieskom figureles kuri butu senoj pozicioj
for(int f = 0;f<32;f++){
if(fig_laukelis[f][0]==oldRow && fig_laukelis[f][1]==oldCol){
//nzn kas cia nutiko bet cia susimaiso row ir collumai ;/
fig_pos[f][2]=(camera.viewportWidth/9)*(newCol);
fig_pos[f][3]=(camera.viewportWidth/9)*(newRow);
//atnaujinam figuros laukeli
fig_laukelis[f][0]=newRow;
fig_laukelis[f][1]=newCol;
//radom tad baigiam cikla
f=33;
}
}
}
}
公共类MyGdxGame扩展应用程序适配器{
公共静态int V_WIDTH=720;//720
公共静态int V_HEIGHT=1280;//1280
私有字符串slogin=“Prisijungimas”,sreg=“registicija”,sai=
“Kompiuteris”,shuman=“Ieškoti oponento”,sboard;
喷雾批;
纹理bdt、blt、kdt、klt、ndt、nlt、pdt、plt、qdt、qlt、rdt、rlt;
正交摄影机;
公共视口;
私有ShowScreen=ShowScreen.login;
BitmapFont字体,font2;
浮子rh,rw;
float startpos[][]=新的float[9][9][2];//cia grido pozicijos[eile]
[stulpelis][x
MyGdxGame gdxGame;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
gdxGame = new MyGdxGame();
View gameView = initializeForView(gdxGame, config);
setContentView(gameView);
}
public interface Service {
void doSomeThing();
}
public class AndroidLauncher extends AndroidApplication implements Service {
MyGdxGame gdxGame;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
gdxGame = new MyGdxGame(this);
View gameView = initializeForView(gdxGame, config);
setContentView(gameView);
}
@Override
public void doSomething() {
//do what you want
}
...
}
public class MyGdxGame extends ApplicationAdapter {
Service service;
public MyGdxGame(Service service){
this.service=service;
}
...
}