在蓝牙模块上从android读取数据

时间:2013-02-28 13:01:58

标签: java android eclipse bluetooth

我被困在这几天了。我想出了一个代码来将数据字节发送到我的蓝牙模块。我没有收到任何错误,但我不确定数据是否实际发送过。我用示波器来检查模块上的RX和TX引脚,但我什么都没得到。谁知道什么是错的?这是我的代码:

import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Modes extends Activity {
    Button gaming;
    Button graphics;
    Button sound;
    Button text;
    BluetoothDevice mmDevice;
    BluetoothSocket mmSocket;
    OutputStream mmOutputStream;
    BluetoothAdapter mBluetoothAdapter;
    byte val;
    private final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modes);
        setUp();
        gaming.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                System.out.println("gkghfgh");
                val = 0;
                try{
                    connect();
                    senData();
                 }catch(IOException ex){}

            }
        });

        graphics.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View v) {
                    val = 1;
                try{
                      connect();
                    senData();
                }catch(IOException ex){}

            }
        });

        sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                val = 8;
                try{
                    connect();
                    senData();
                }catch(IOException ex){}
            }
        });

         text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                val = 9;
                try{
                    connect();
                    senData();
                }catch(IOException ex){}
                startActivity(new Intent(Modes.this,Text.class));
            }
         });
     }

    private void setUp() {
        gaming = (Button) findViewById(R.id.button1);
        graphics = (Button) findViewById(R.id.button2);
        sound = (Button) findViewById(R.id.button3);
        text = (Button) findViewById(R.id.button4);
        gaming.setText("Gaming");
        graphics.setText("Graphics");
        sound.setText("Sound");
        text.setText("Text");
     }

    void connect() throws IOException{
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0) {
            for(BluetoothDevice device : pairedDevices) {
                if(device.getName().equals("RN42-58C1")) {
                    mmDevice = device;
                    break;
                }
            }
         }
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);      
        mmSocket.connect();

    }

    void senData() throws IOException{
        mmOutputStream.write(val);
        System.out.println("Data Sent");
    }
 }

2 个答案:

答案 0 :(得分:0)

我认为问题在于您的蓝牙设备名称。你需要写一个12位数的蓝牙ID。具有字母数字值。

希望有效。祝你好运。

答案 1 :(得分:0)

尝试调用“mmOutputStream.flush();”在“mmOutputStream.write(val);”之后。