gpt4 book ai didi

android - 以编程方式连接到蓝牙设备

转载 作者:行者123 更新时间:2023-11-30 03:23:51 24 4
gpt4 key购买 nike

我正在为一款新的车辆安全应用程序编写程序。该应用程序允许用户通过他的手机应用程序控制锁定/解锁操作。假设用户的手机蓝牙最初是关闭的。如果是这样的话,当他打开应用程序时,手机蓝牙适配器应该会自动打开并且应该连接到固定在车辆上的蓝牙模块。根据我完成的代码,手机 BT 适配器的编程启用工作正常。但是与车载蓝牙模块的连接并没有发生。

但如果用户在手机 BT 适配器已打开的情况下打开应用程序,则车辆和手机之间会自动建立连接。

我需要知道为什么当以编程方式打开 BT 适配器时连接没有发生。

注意 - 手机和车辆 BT 模块已配对。蓝牙模块的 mac 地址在编码中是硬编码的。 编码如下。我只粘贴了必要的部分。我希望每个需要理解和解决我的问题的人都在这里。我发布代码的方式非常困惑。对于那个很抱歉。希望很清楚。我是新手。

    private static final UUID MY_UUID =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

// Insert bluetooth devices MAC address
private static String address = "00:19:5D:EF:03:79";


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

setContentView(R.layout.main);
btAdapter = BluetoothAdapter.getDefaultAdapter();
btAdapter.enable();


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

btAdapter.enable();

// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);

// Two things are needed to make a connection:
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.


try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}

// Make sure Discovery isn't going on when you attempt to connect and pass your message.

btAdapter.cancelDiscovery();

// Establish the connection. This will block until it connects.

try {
btSocket.connect();

} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}

// Create a data stream so we can talk to server.

try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
}

最佳答案

可能是计时问题,onCreate 和 onResume 的调用顺序非常短。在未启用 BT 的情况下,onResume 中的代码可能会在 BT 服务上线之前被调用。

我的建议:尝试通过将代码放入 Runnable 来延迟启动几秒钟。

private Handler mHandler = new Handler();

public void onCreate() {

[...]

mHandler.postDelayed(new Runnable() {
@Override
public void run() {
btAdapter.enable();
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
// Two things are needed to make a connection:
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}

// Make sure Discovery isn't going on when you attempt to connect and pass your message.

btAdapter.cancelDiscovery();

// Establish the connection. This will block until it connects.

try {
btSocket.connect();

} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}

// Create a data stream so we can talk to server.

try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
}, 5000); // 5 second delay

[...]

注意事项:如果您在启动后立即退出应用程序,这会非常糟糕。将runnable放在一个成员变量中,在onDestroy()中调用mHandler.removeCallback(Runnable)。

关于android - 以编程方式连接到蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18607192/

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