gpt4 book ai didi

android - 蓝牙 : Do i need to know the UUID of the Devices?

转载 作者:行者123 更新时间:2023-11-29 20:53:36 27 4
gpt4 key购买 nike

我想将我的 Nexus 7 设备与我的 Nexus 4 设备连接,稍后我想将我的 Nexus 7 设备与微 Controller 连接。 我是否必须知道我的设备的 UUID 才能使用蓝牙连接它们?

当我配对我的设备时,UUID 是否正在交换?

如果是:为什么在android示例中有定义的UUID?

public class BluetoothService extends Thread {
private static final String TAG = BluetoothService.class.getSimpleName();
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public static final int STATE_NONE = 0;
public static final int STATE_LISTEN = 1;
public static final int STATE_CONNECTING = 2;
public static final int STATE_CONNECTED = 3;
private BluetoothAdapter bluetoothAdapter = null;
private Handler handler = null;
private ConnectThread connectThread = null;
private ConnectedThread connectedThread = null;
private int bluetoothState = STATE_NONE;

public BluetoothService(Handler handler) {
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
this.bluetoothState = STATE_NONE;
this.handler = handler;
}

public synchronized void startConnection() {
Log.d(TAG, "start");

if (this.connectThread != null) {
this.connectThread.cancel();
this.connectThread = null;
}

if (this.connectedThread != null) {
this.connectedThread.cancel();
this.connectedThread = null;
}

this.setBluetoothState(STATE_LISTEN);
}

public synchronized void connect(BluetoothDevice device) {
if (this.bluetoothState == STATE_CONNECTING) {
if (this.connectThread != null) {
this.connectThread.cancel();
this.connectThread = null;
}
}

if (this.connectedThread != null) {
this.connectedThread.cancel();
this.connectedThread = null;
}

this.connectThread = new ConnectThread(device);
this.connectThread.start();

this.setBluetoothState(STATE_CONNECTING);
}

public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
if (this.connectThread != null) {
this.connectThread.cancel();
this.connectThread = null;
}

if (this.connectedThread != null) {
this.connectedThread.cancel();
this.connectedThread = null;
}

this.connectedThread = new ConnectedThread(socket);
this.connectedThread.start();

Message msg = this.handler.obtainMessage(Globals.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();

bundle.putString(Globals.DEVICE_NAME, device.getName());
msg.setData(bundle);

this.handler.sendMessage(msg);
this.setBluetoothState(STATE_CONNECTED);
}

public synchronized void stopConnection() {
if (this.connectThread != null) {
this.connectThread.cancel();
this.connectThread = null;
}

if (this.connectedThread != null) {
this.connectedThread.cancel();
this.connectedThread = null;
}

this.setBluetoothState(STATE_NONE);
}

public void write(byte[] out) {
ConnectedThread connectedThread = null;

synchronized (this) {
if (this.bluetoothState != STATE_CONNECTED) {
return;
}

connectedThread = this.connectedThread;
}

connectedThread.write(out);
}

private void connectionFailed() {
Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);

Bundle bundle = new Bundle();
bundle.putString(Globals.TOAST, "Unable to connect device");

msg.setData(bundle);

this.handler.sendMessage(msg);

BluetoothService.this.startConnection();
}

private void connectionLost() {
Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);
Bundle bundle = new Bundle();

bundle.putString(Globals.TOAST, "Device connection was lost");
msg.setData(bundle);

this.handler.sendMessage(msg);

BluetoothService.this.startConnection();
}

public synchronized int getBluetoothState() {
return this.bluetoothState;
}

private synchronized void setBluetoothState(int bluetoothState) {
this.bluetoothState = bluetoothState;
}

private class ConnectThread extends Thread {
private BluetoothSocket bluetoothSocket = null;
private BluetoothDevice bluetoothDevice = null;

public ConnectThread(BluetoothDevice bluetoothDevice) {
this.bluetoothDevice = bluetoothDevice;

BluetoothSocket tempBluetoothSocket = null;

try {
tempBluetoothSocket = this.bluetoothDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + "create() failed", e);
}

this.bluetoothSocket = tempBluetoothSocket;
}

public void run() {
Log.i(TAG, "BEGIN mConnectThread");

this.setName("ConnectThread");

bluetoothAdapter.cancelDiscovery();

try {
this.bluetoothSocket.connect();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);

connectionFailed();

try {
this.bluetoothSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2);
}

return;
}

synchronized (BluetoothService.this) {
connectThread = null;
}

connected(this.bluetoothSocket, this.bluetoothDevice);
}

public void cancel() {
try {
this.bluetoothSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}

private class ConnectedThread extends Thread {
private BluetoothSocket bluetoothSocket = null;
private InputStream inputStream = null;
private OutputStream outputStream = null;

public ConnectedThread(BluetoothSocket bluetoothSocket) {
Log.d(TAG, "create ConnectedThread");

this.bluetoothSocket = bluetoothSocket;

InputStream tempInputStream = null;
OutputStream tempOutputStream = null;

try {
tempInputStream = this.bluetoothSocket.getInputStream();
tempOutputStream = this.bluetoothSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}

this.inputStream = tempInputStream;
this.outputStream = tempOutputStream;
}

public void run() {
byte[] buffer = new byte[1024];
int bytes = 0;

while (true) {
try {
bytes = this.inputStream.read(buffer);

handler.obtainMessage(Globals.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);

connectionLost();

BluetoothService.this.start();

break;
}
}
}

public void write(byte[] buffer) {
try {
this.outputStream.write(buffer);

handler.obtainMessage(Globals.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}

public void cancel() {
try {
this.bluetoothSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
}

如果不是:如何交换 UUID 以便能够连接设备?

最佳答案

google sample/example 中定义的 UUID 必须为服务器和客户端所知:

  • 服务器创建一个 RFcomm ServerSocket 来监听带有此 UUID 的传入连接
  • 客户端创建一个 RFcomm 蓝牙套接字,它将尝试连接到服务器套接字

如果 uuid 匹配,则建立连接。

配对会保存有关远程设备的信息(名称、地址等),这样当你想再次连接时,你不必搜索设备来获取它们 =)

关于android - 蓝牙 : Do i need to know the UUID of the Devices?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28409651/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com