我正在连接HC05蓝牙模块并与arduino通讯,效果很好。
但是我遇到了一个问题。
有时,断开连接后无法再次连接到蓝牙模块,除非重新启动HC05。
我正在考虑,也许与我如何进行连接和断开连接有关。我正在使用不同的活动来连接和断开与蓝牙模块的连接。
我称其为开始蓝牙连接:
try {
findBT();
openBT();
} catch (IOException e) {
Log.e("Bluetooth", "Couldn't find or connect to bluetooth");
}
这是我找到特定蓝牙模块的代码:
void findBT() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast errore = makeText(Menu2.this, "Error, enable bluetooth", Toast.LENGTH_LONG);
errore.show();
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
.getBondedDevices();
if (pairedDevices.isEmpty()) {
Log.e("bluetooth",
"No devices paired...");
return ;
}
String MY_MAC_ADDR = getResources().getString(R.string.Machine_Bluetooth_MAC);
for (BluetoothDevice device : pairedDevices) {
Log.d("Bluetooth", "Device : address : " + device.getAddress() + " name :"
+ device.getName());
if (MY_MAC_ADDR.equals(device.getAddress())) {
mmDevice = device;
break;
}
}
Log.e("Bluetooth", "Bluetooth device found01");
//Toast found = makeText(MachineReady.this, "Bluetooth device found", Toast.LENGTH_LONG);
//found.show();
}
连接到蓝牙:
void openBT() throws IOException {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
Log.e("Bluetooth", "Almost connected");
mBluetoothAdapter.cancelDiscovery();
mmSocket.connect();
Log.e("Bluetooth", "Connected");
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
if(mmSocket.isConnected()){
bluConnection = true;
beginListenForData();
Log.e("Bluetooth", "Bluetooth connection has been established4");
try {
String msg = "512\n";
mmOutputStream.write(msg.getBytes());
} catch (IOException e) {
Log.e("Send bluetooth", "Couldn't send text");
}
}else if(!mmSocket.isConnected()){
Log.e("Bluetooth", "Bluetooth couldn't connect to the device");
}
}
收听从蓝牙接收的数据:
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
Log.e("Bluetooth", "Bluetooth data received is " + data);
if(data.contains("z")){
sendokcommand();
try {
closeBT();
} catch (IOException e) {
Log.e("Close bluetooth", "Couldn't close the bluetooth connection13");
e.printStackTrace();
}
busyMachineString = data;
MachinesAdapter.setString(busyMachineString);
}
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
当我收到命令时,我会发送一个OK命令以确认我已收到正确的消息:
public void sendokcommand(){
if(bluConnection) {
try {
String msg = "0\n";
mmOutputStream.write(msg.getBytes());
//Toast b = Toast.makeText(this, msg, Toast.LENGTH_LONG);
//b.show();
} catch (IOException e) {
Log.e("Send bluetooth", "Couldn't send text");
}
}
}
我每次离开活动时都会使用CloseBT:
void closeBT() throws IOException
{
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
}
连接和断开连接方法看起来是否正确,还是需要以其他方式执行此操作?当它失败时,我在日志中收到此错误,“找不到或连接到蓝牙”,这是我的第一个代码段。
很抱歉,对于这个冗长的示例,我只是认为展示如何处理不同的蓝牙场景非常重要。希望您能并且会帮助我。