如何将Java代码转换为C#(mono android)? [英] How to convert Java code to be C# (mono android) ?

查看:76
本文介绍了如何将Java代码转换为C#(mono android)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Java代码转换为C#(单声道android)????







(eclipse):

布局:一个按钮和一个EditText





How to convert Java code to be C# (mono android) ????



(eclipse):
layout: one Button and one EditText


package com.example.bluetooth_client;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.bluetooth.*;
import android.content.Intent;

public class MainActivity extends Activity {
         private static final String TAG = "DEBUG";
         private static final boolean D = true;
         private BluetoothAdapter mBluetoothAdapter = null;
         private BluetoothSocket btSocket = null;
         private OutputStream outStream = null;
         private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
         private static String address = "00:00:00:00:00:00";  // pc bluetooth device address

     @Override
     public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
               Button sendButton = (Button) findViewById(R.id.button);
               sendButton.setOnClickListener(new Button.OnClickListener(){
                    public void onClick(View arg0){
                       TextView enteredText = (TextView)findViewById(R.id.entertext);
                                 String message = enteredText.getText().toString();
                                 byte[] msgBuffer = message.getBytes();
                                 try {
                                      outStream.write(msgBuffer);
                                 } catch (IOException e) {
                                      Log.e(TAG, "ON RESUME: Exception during write.", e);
                             }
                    }
                });
               if (D)
                    Log.e(TAG, "+++ ON CREATE +++");
                       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

               if (mBluetoothAdapter == null) {
                    Toast.makeText(this,"Bluetooth is not available.", Toast.LENGTH_LONG).show();
                    finish();
                    return;
               }
               if (!mBluetoothAdapter.isEnabled()) {
                   Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                   startActivityForResult(enableBtIntent, 1);
               }
               if (!mBluetoothAdapter.isEnabled()) {
                    Toast.makeText(this, "Please enable your BT and re-run this program.", Toast.LENGTH_LONG).show();
                    finish();
                    return;
               }
               Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                    if (pairedDevices.size() > 0) {
                        for (BluetoothDevice device : pairedDevices) {
                                Log.e(TAG,device.getName() + "\n" + device.getAddress());
                        }
                    }
          }
          @Override
          public void onResume() {
               super.onResume();
               BluetoothDevice device = null;
               if (D) {
                    Log.e(TAG, "+ ON RESUME +");
                    Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");
                   }
               if(BluetoothAdapter.checkBluetoothAddress(address))
                       device = mBluetoothAdapter.getRemoteDevice(address);
               else{
                     Log.e(TAG, "+++ address fail +++");
               }

                   try {
                        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                   } catch (IOException e) {
                        Log.e(TAG, "ON RESUME: Socket creation failed.", e);
                   }

                   mBluetoothAdapter.cancelDiscovery();
                   try {
                        btSocket.connect();
                        Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");
                   } catch (IOException e) {
                        try {
                             btSocket.close();
                        } catch (IOException e2) {
                             Log.e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);
                        }
                   }

                   if (D)
                        Log.e(TAG, "+ ABOUT TO SAY SOMETHING TO SERVER +");

                   try {
                        outStream = btSocket.getOutputStream();
                   } catch (IOException e) {
                        Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
                   }


          }

          @Override
          public void onPause() {
               super.onPause();

               if (D)
                    Log.e(TAG, "- ON PAUSE -");

                   if (outStream != null) {
                        try {
                             outStream.flush();
                        } catch (IOException e) {
                             Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e);
                        }
                   }
                   try  {
                        btSocket.close();
                   } catch (IOException e2) {
                Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);
               }
          }
}

推荐答案

阅读本教程... < br $> b $ b

锐化指南 [ ^ ]
Read this tutorial...

Guide to Sharpen[^]


通过手工重写代码。没有可以自动为您执行此操作的转换工具。此外,无论如何,没有人会为你做这件事。
By rewriting the code, by hand. There is no conversion tool that will do this for you automatically. Also, nobody is going to do it for you, for free anyway.


这篇关于如何将Java代码转换为C#(mono android)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆