我编写了以下代码,以在手机和连接到微控制器的hc05之间建立蓝牙连接。我希望此通信的结果显示在三个进度条上(蓝牙模块发送3个字符,代表3个传感器的状态,然后应将其显示在进度条上)。
蓝牙连接已经起作用,进度条的设置也是如此。我唯一不确定的部分是如何获取进度条以显示蓝牙模块发送的字符(这些字符不断变化)。
任何建议将不胜感激!
public class MainActivity extends AppCompatActivity {
private interface MessageConstants {
public static final int MESSAGE_READ = 0;
public static final int MESSAGE_WRITE = 1;
public static final int MESSAGE_TOAST = 2;
// ... (Add other message types here as needed.)
}
private final static int REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
TextView text;
OutputStream mmOutputStream;
InputStream mmInputStream;
private Handler mHandler;
Thread workerThread;
//
private byte[] mmBuffer; // mmBuffer store for the stream
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;
BluetoothSocket tmp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InputStream tmpIn = null;
OutputStream tmpOut = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
ProgressBar progressBar4=(ProgressBar) findViewById(R.id.progressBar4);
ProgressBar progressBar2=(ProgressBar) findViewById(R.id.progressBar2);
progressBar2.setBackgroundColor(2);
if (mBluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
if (!mBluetoothAdapter.isEnabled()) {
// Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
mBluetoothAdapter.enable();
}
if (mBluetoothAdapter.isEnabled()) {
Toast.makeText(getApplicationContext(), "Bluetooth enabled", Toast.LENGTH_SHORT).show();
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
Toast.makeText(getApplicationContext(), "PD found", Toast.LENGTH_SHORT).show();
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
BluetoothDevice hc05 = mBluetoothAdapter.getRemoteDevice(deviceHardwareAddress);
String str = "1234";
UUID uuid = UUID.nameUUIDFromBytes(str.getBytes());
try {
tmp = device.createRfcommSocketToServiceRecord(hc05.getUuids()[0].getUuid());
} catch (IOException e) {
Log.e("Tag", "Socket's create() method failed", e);
}
mmSocket = tmp;
try {
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and return.
try {
mmSocket.close();
} catch (IOException closeException) {
Log.e("shit", "Could not close the client socket", closeException);
}
return;
}
try {
tmpIn = mmSocket.getInputStream();
} catch (IOException e) {
Log.e("tag", "Error occurred when creating input stream", e);
}
mmInputStream = tmpIn;
}
mmBuffer = new byte[1024];
int numBytes; // bytes returned from read()
try {
numBytes = mmInputStream.read(mmBuffer);
String readMessage = new String(mmBuffer, 0, numBytes);
text.setText(readMessage);
}
catch (IOException e) {
Log.d("tag", "Input stream was disconnected", e);
}
}
}
}