gpt4 book ai didi

安卓和蓝牙挂起(startActivityForResult)

转载 作者:行者123 更新时间:2023-11-30 03:47:42 26 4
gpt4 key购买 nike

我正在尝试改进一个实际代码,该代码通过 Android 手机与带有 Arduino(电子微 Controller )的 Atmega 建立蓝牙连接。我可以接收数据并将数据发送到微 Controller ,但蓝牙需要在我的应用程序午餐之前打开,否则它会挂起并关闭。我确实检查了蓝牙适配器并请求用户更改蓝牙状态(如果它处于关闭状态)但程序似乎继续并尝试在获得用户选择的结果之前建立连接。我想要一些帮助来找到一个解决方案来阻止我的程序,直到用户输入他们的选择,或者甚至获得更好的解决方案。

我想说我对 Android 编程还是个新手,我确实阅读了 Android Activity 流程图。

我可以提供 logcat,但我检查了它,它清楚地表明我正在尝试使用蓝牙,即使它没有启用......

这是我的代码:

我要感谢任何能给我指出正确方向的人

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnOn = (Button) findViewById(R.id.btnOn); // button LED ON
btnOff = (Button) findViewById(R.id.btnOff); // button LED OFF
txtArduino = (TextView) findViewById(R.id.txtArduino); // for display the received data from the Arduino

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
checkBTState();

h = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECIEVE_MESSAGE: // if receive massage
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
sb.append(strIncom); // append string
int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
if (endOfLineIndex > 0) { // if end-of-line,
sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear
txtArduino.setText("Data from Arduino: " + sbprint);
Log.e(TAG, "Arduino"+sbprint);

//Test string value
if(sbprint.matches("-?\\d+(\\.\\d+)?")) {
try{

Float sensorReading = Float.parseFloat(sbprint);
Log.e(TAG, "Sensor value"+sensorReading);
}catch(NumberFormatException e){
Log.e(TAG, "No int format sorry",e);

}
}
if(sbprint.matches("test")){

Log.e(TAG, "garbage");
}

///////

btnOff.setEnabled(true);
btnOn.setEnabled(true);
}
//Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "...");
break;
}
};
};





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

Log.d(TAG, "...onResume - try connect...");

// Set up a pointer to the remote node using it's address.
BluetoothDevice device = mBluetoothAdapter.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 = createBluetoothSocket(device);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}

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

// Discovery is resource intensive. Make sure it isn't going on
// when you attempt to connect and pass your message.
mBluetoothAdapter.cancelDiscovery();


// Establish the connection. This will block until it connects.
Log.d(TAG, "...Connecting...");
try {
btSocket.connect();
Log.d(TAG, "....Connection ok...");
} 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.
Log.d(TAG, "...Create Socket...");

mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
}

最佳答案

您的所有代码都在 onResume 中。 onResume 将在 Activity 启动时立即被调用,因此它几乎会立即执行。我根本看不到任何应该延迟它的代码。如果您不想在用户选择某些内容之前尝试连接,那么所有连接代码都应该在按钮的点击处理程序或类似的东西中。

关于安卓和蓝牙挂起(startActivityForResult),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14698847/

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