如何从MAC地址获取UUID以实现蓝牙连接?

时间:2016-06-25 05:11:42

标签: android bluetooth

我有蓝牙设备,MAC地址为18xxxxxxxx18。我编写以下代码来从此设备获取数据,但收到了无效UUID的错误消息。

公共类MainActivity扩展了AppCompatActivity {

private static final boolean DEBUG = true;
public static final int MESSAGE_READ = 1;
private BluetoothAdapter mBTAdapter;
private AcceptThread mAcceptThread;
TextView textview1;
private static final int REQUEST_ENABLE_BT = 3;
BluetoothAdapter btAdapter;

private static final UUID MY_UUID = UUID.fromString("18xxxxxxxx18");

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textview1 = (TextView) findViewById(R.id.textView);

    // Getting the Bluetooth adapter
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    textview1.append("\nAdapter: " + btAdapter);

   // CheckBluetoothState();
    mBTAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBTAdapter == null) {
        textview1.append( "device does not support Bluetooth");
    }
}

@Override
protected void onStart() {
    super.onStart();

    if (!mBTAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
    else {
        setDiscoveralbe();
    }
}

@Override
protected void onPause() {
    mAcceptThread.cancel();

    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();

    mAcceptThread = new AcceptThread();
    mAcceptThread.start();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_ENABLE_BT) {
        if (resultCode == AppCompatActivity.RESULT_OK) {
            textview1.append("Enabled BT\n");

            setDiscoveralbe();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

private class AcceptThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;
        try {
            tmp = mBTAdapter.listenUsingRfcommWithServiceRecord("BluetoothServer", MY_UUID);
        }
        catch (IOException e) { }
        mmServerSocket = tmp;
    }

    @Override
    public void run() {
        BluetoothSocket socket = null;
        while (true) {
            try {
                socket = mmServerSocket.accept();
            }
            catch (IOException e) {
                break;
            }

            if (socket != null) {
                manageConnectedSocket(socket);
                try {
                    mmServerSocket.close();
                } catch (IOException e) {
                }
                break;
            }
        }
    }

    public void cancel() {
        try {
            mmServerSocket.close();
        }
        catch (IOException e) { }
    }
}

public void manageConnectedSocket(BluetoothSocket socket) {
    if (DEBUG) {textview1.append("Connected");

    ConnectedThread server = new ConnectedThread(mHandler, socket);
    server.start();
    }
    else
    {
    textview1.append("Not Connected");
    }
}

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MESSAGE_READ:
                byte[] readBuf = (byte[])msg.obj;
                String readMessage = new String(readBuf, 0, msg.arg1);
                textview1.append(readMessage);
        }
    }
};

private void setDiscoveralbe() {
    textview1.append("set BT as being discoverable during 2 minutes\n");
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120); // 2 minutes
    startActivity(discoverableIntent);
}

}

我需要此设备连接到我的移动应用以从设备获取数据。 如果我出错了,请建议我这样做吗?

0 个答案:

没有答案