我正在尝试创建一个国际象棋应用程序,我从蓝牙获取移动信息,然后我需要将它发送到位于核心类的方法。此外,我需要在单击核心类的按钮时启动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;
}
}
}
}
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;
}
}
}
}
我需要做的是在AndroidLauncher handler.post(new Runnable() {
部分代码中从蓝牙获取数据时调用movefig方法。
答案 0 :(得分:0)
将movefig()
公开MyGdxGame
公开,以便您可以从任何套餐中进行访问。
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);
}
现在您已引用MyGdxGame
,您可以在movefig()
课程内拨打AndroidLauncher
。
您需要interfacing通过core module
致电android module
在core
模块
public interface Service {
void doSomeThing();
}
在AndroidLauncher
class
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
}
...
}
保留对来自平台的Service
的引用,以便创建MyGdxGame
的参数化构造函数
public class MyGdxGame extends ApplicationAdapter {
Service service;
public MyGdxGame(Service service){
this.service=service;
}
...
}