Android蓝牙开发,如何获得UUID?

时间:2015-12-17 09:58:23

标签: java android bluetooth

我想使用Android连接其他蓝牙设备,这不是Android系统,但 BlueToothDevice的方法需要UUID才能连接。我怎样才能获得UUID?

为了解决这个问题,我找到了一个解决方案,代码如下:

/**
*variable "device" is a BlueToothDevice 
*/

method = device.getClass().getMethod("createRfcommSocket", new Class[{int.class});
tmp = (BluetoothSocket) method.invoke(device, 1);

代码解决问题,但我不知道为什么。请告诉我原理

1 个答案:

答案 0 :(得分:2)

请尝试以下代码。我想这会显示你的设备::

的详细信息

1)AndroidManifest.xml - 添加以下权限

uses-permission android:name="android.permission.BLUETOOTH"

uses-permission android:name="android.permission.BLUETOOTH_ADMIN"

2)MainActivity.java

import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final int REQUEST_ENABLE_BT = 12;
    private TextView out;
    private BluetoothAdapter adapter;

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

        out = (TextView) findViewById(R.id.tvBluetoothInfo);
        setBluetoothData();

        if (Connections.blueTooth()) {
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        out.setText("");
        setBluetoothData();
    }

    private void setBluetoothData() {

        // Getting the Bluetooth adapter
        adapter = BluetoothAdapter.getDefaultAdapter();
        out.append("\nAdapter: " + adapter.toString() + "\n\nName: "
                + adapter.getName() + "\nAddress: " + adapter.getAddress());

        // Check for Bluetooth support in the first place
        // Emulator doesn't support Bluetooth and will return null

        if (adapter == null) {
            Toast.makeText(this, "Bluetooth NOT supported. Aborting.",
                    Toast.LENGTH_LONG).show();
        }

        // Starting the device discovery
        out.append("\n\nStarting discovery...");
        adapter.startDiscovery();
        out.append("\nDone with discovery...\n");

        // Listing paired devices
        out.append("\nDevices Pared:");
        Set<BluetoothDevice> devices = adapter.getBondedDevices();
        for (BluetoothDevice device : devices) {
            out.append("\nFound device: " + device.getName() + " Add: "
                    + device.getAddress());
        }
    }

}

3)Connections.java

import android.bluetooth.BluetoothAdapter;

public class Connections {

    private static boolean state = false;

    public static boolean blueTooth() {

        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
        if (!bluetooth.isEnabled()) {
            System.out.println("Bluetooth is Disable...");
            state = true;
        } else if (bluetooth.isEnabled()) {
            String address = bluetooth.getAddress();
            String name = bluetooth.getName();
            System.out.println(name + " : " + address);
            state = false;
        }
        return state;
    }

}

4)main.xml

<TextView
    android:id="@+id/tvBluetoothInfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />