Java 蓝牙发送数据

Java 蓝牙发送数据,java,android,bluetooth,Java,Android,Bluetooth,我是android开发的新手。我正在开发一个应用程序,首先,我有一个主要活动,我必须打开/关闭蓝牙,使其可发现,搜索设备并连接到它们 在这之后,我转到另一个活动,在那里我有一个文本视图,有一个edittext和一个send按钮,在那里我可以写一些东西并发送它 我已经完成了所有启用/禁用蓝牙的工作,但现在我需要连接到新找到的设备,然后进入聊天类型活动并发送 我现在觉得这是一个相当大的工作,但如果你能给我一个例子,说明我如何做到这一点,那就太好了。这是我的代码: public class BTAct

我是android开发的新手。我正在开发一个应用程序,首先,我有一个主要活动,我必须打开/关闭蓝牙,使其可发现,搜索设备并连接到它们

在这之后,我转到另一个活动,在那里我有一个文本视图,有一个edittext和一个send按钮,在那里我可以写一些东西并发送它

我已经完成了所有启用/禁用蓝牙的工作,但现在我需要连接到新找到的设备,然后进入聊天类型活动并发送

我现在觉得这是一个相当大的工作,但如果你能给我一个例子,说明我如何做到这一点,那就太好了。这是我的代码:

public class BTActivity extends Activity {

// Intent request codes
private static final int REQUEST_DISCOVERABLE_BT = 0;
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;

// Debugging
private static final String TAG = "BluetoothChat";
private static final boolean D = true;

// Name of the connected device
public static String mConnectedDeviceName = null;

// Array adapter for device list
private ArrayAdapter<String> mArrayAdapter;

// Local Bluetooth adapter
private BluetoothAdapter mBluetoothAdapter = null;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.main);


    final Button button1 = (Button) findViewById(R.id.boton1);
    final Button button2 = (Button) findViewById(R.id.boton2);
    final Button button3 = (Button) findViewById(R.id.boton3);
    final Button button4 = (Button) findViewById(R.id.boton4);
    final Button button5 = (Button) findViewById(R.id.boton5);


    button5.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            lanzarComunicacion (null);
        }
    });



    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter == null) {          
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;   
    }   




    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) { 

            if (!mBluetoothAdapter.isEnabled()) {


                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);           
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);   
            }

        }
    });


    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //LLAMA A LA DIALOG ACTIVITY PARA VISUALIZAR LOS DISPOSITIVOS
            LanzarBusqueda(null);

        }
    });





    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) { 


            if (!mBluetoothAdapter.isDiscovering()) {                         
                Context context = getApplicationContext();                            
                CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";                        
                int duration = Toast.LENGTH_SHORT;            

                Toast toast = Toast.makeText(context, text, duration);                        
                toast.show(); 

                Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);         
                discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);

                startActivity(discoverableIntent);
            }                   
        }
    });


    button4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            mBluetoothAdapter.disable();

            Context context = getApplicationContext();                        
            CharSequence text = "TURNING_OFF BLUETOOTH";                          
            int duration = Toast.LENGTH_LONG;               

            Toast toast = Toast.makeText(context, text, 15);                              
            toast.show();

        }
    }); 
}



public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_CONNECT_DEVICE:
        if (resultCode == Activity.RESULT_OK) {
            connectDevice(data);
        }
    }
}



private void connectDevice(Intent data) {
    String address = data.getExtras().getString(DeviceListDialog.EXTRA_DEVICE_ADDRESS);
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    BTCommunication.mChatService.connect(device);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.bt, menu);
    return true;
}


public void lanzarComunicacion (View view) {
    Intent i = new Intent(this, BTCommunication.class);
    startActivity(i);
}

public void LanzarBusqueda (View view) {
    Intent i = new Intent(this, DeviceListDialog.class);
    startActivity(i);
}
公共类BTActivity扩展活动{
//意图请求代码
私有静态最终整数请求\u可发现\u BT=0;
专用静态最终int请求\u连接\u设备=1;
私有静态最终整数请求_ENABLE_BT=2;
//调试
私有静态最终字符串TAG=“BluetoothChat”;
私有静态最终布尔值D=true;
//连接的设备的名称
公共静态字符串mConnectedDeviceName=null;
//设备列表的阵列适配器
私人ArrayaAdapter MarrayaAdapter;
//本地蓝牙适配器
私有BluetoothAdapter mBluetoothAdapter=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
最终按钮button1=(按钮)findViewById(R.id.boton1);
最终按钮按钮2=(按钮)findViewById(R.id.boton2);
最终按钮button3=(按钮)findViewById(R.id.boton3);
最终按钮button4=(按钮)findViewById(R.id.boton4);
最终按钮button5=(按钮)findViewById(R.id.boton5);
button5.setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
Lanzarcomunication(空);
}
});
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
如果(mBluetoothAdapter==null){
Toast.makeText(此“蓝牙不可用”,Toast.LENGTH_LONG.show();
完成();
返回;
}   
button3.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
如果(!mBluetoothAdapter.isEnabled()){
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
}
});
button2.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//美洲驼对话活动
LanzarBusqueda(空);
}
});
button1.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
如果(!mBluetoothAdapter.isDiscovering()){
Context=getApplicationContext();
CharSequence text=“使您的设备可发现”;
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show();
Intent discoverableIntent=新意图(BluetoothAdapter.ACTION\u REQUEST\u DISCOVERABLE);
可发现意图.putExtra(BluetoothAdapter.EXTRA可发现持续时间,300);
星触觉(可发现的意图);
}                   
}
});
button4.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
mBluetoothAdapter.disable();
Context=getApplicationContext();
CharSequence text=“关闭蓝牙”;
int duration=Toast.LENGTH\u LONG;
Toast Toast=Toast.makeText(上下文,文本,15);
toast.show();
}
}); 
}
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
开关(请求代码){
案例请求\u连接\u设备:
if(resultCode==Activity.RESULT\u确定){
连接设备(数据);
}
}
}
专用设备(意图数据){
字符串地址=data.getExtras().getString(DeviceListDialog.EXTRA\u设备\u地址);
BluetoothDevice=mBluetoothAdapter.getRemoteDevice(地址);
BTCommunication.mChatService.connect(设备);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.bt,menu);
返回true;
}
公共无效Lanzarcomunication(视图){
意图i=新意图(此,BTCommunication.class);
星触觉(i);
}
公共无效LanzarBusqueda(视图){
Intent i=新Intent(这是DeviceListDialog.class);
星触觉(i);
}
}