通过蓝牙接收数据并在应用程序屏幕上查看

时间:2016-12-07 07:05:09

标签: android bluetooth

我目前正在构建一个应用程序,它将通过蓝牙连接到设备并从中接收数据。目前我能够连接配对设备,但似乎无法找到有关如何从该设备接收数据的任何好资源。我附上了应用程序的2张图片,第一张;是主菜单。在那里,你会看到一个显示“连接到TANGO”的按钮,其中我已经输入了下面的所有代码。这允许我连接到任何配对设备。同样在主菜单中,有一个“翻译”按钮,那是我试图接收数据,并查看它们,但到目前为止,我还没有能够这样做。

这是“连接到TANGO”connection.java

的代码
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;

    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;

    import android.widget.Toast;
    import java.util.ArrayList;
    import java.util.Set;

    public class connection extends AppCompatActivity  {
    Button b1,b2,b3,b4;
    private BluetoothAdapter BA;
    private Set<BluetoothDevice>pairedDevices;
    ListView lv;

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

         b1 = (Button)findViewById(R.id.button);
         b2=(Button)findViewById(R.id.button2);
         b3=(Button)findViewById(R.id.button3);
         b4=(Button)findViewById(R.id.button4);

         BA = BluetoothAdapter.getDefaultAdapter();
         lv = (ListView)findViewById(R.id.listView);
}

public void on(View v){
    if (!BA.isEnabled()) {
        Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOn, 0);
        Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
    }
}

public void off(View v){
if (BA.disable());
    Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
}


public  void visible(View v){
    Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(getVisible, 0);
}


public void list(View v){
    pairedDevices = BA.getBondedDevices();

    ArrayList list = new ArrayList();

    for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
    Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();

    final ArrayAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_list_item_1, list);

    lv.setAdapter(adapter);
   }
}

main_menu

bluetooth_discovery

2 个答案:

答案 0 :(得分:0)

当您引用配对的BluetoothDevice时,您需要创建一个套接字并收听它。简化示例如下:

创建BluetoothSocket并连接到它:

[2, -0.5]

收听套接字(从设备获取数据)

BluetoothSocket socket = device.createRfcommSocketToServiceRecord(<your-device>.getUuids()[0].getUuid());
socket.connect();

写入套接字(将数据发送到设备)

InputStream inStream = socket.getInputStream();
while (inStream.available() > 0) {
    inStream.read(); // <-- data from device
}

有关更多详细信息,请参阅the docs

答案 1 :(得分:0)

@krekle这段代码会起作用。

    import android.bluetooth.BluetoothSocket;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    public class display extends AppCompatActivity {

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

    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    private android.os.Handler mHandler;

    public display (BluetoothSocket socket, Handler mHandler) {
        mmSocket = socket;
        this.mHandler = mHandler;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                int MESSAGE_READ = 0;
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

我只需要改变&#34; ConnectThread&#34;到&#34;显示&#34;和&#34;线程&#34;到&#34; AppCompatActivity&#34;只是为了配合我以前做过的活动。