我已完成将蓝牙连接到配对设备的操作。建立连接后,我想转到下一个活动,要发送的数据由用户指定。所以最主要的是,如何将字符串发送到已连接的蓝牙设备(来自先前的活动)。需要帮忙。我搜了很多。找不到。 预先感谢
BluetoothConnectingActivity
私有Runnable serverListener = new Runnable(){ 公共无效run(){ 尝试//打开BT连接 { //对于较旧的手机有问题...帮助:更改服务器/客户端方向... //但解决了:BluetoothAdapter:没有使用BluetoothManagerCallback调用的getBluetoothService()
Log.i("TrackingFlow", "Server socket: new way used...");
socket = (BluetoothSocket) remoteDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(remoteDevice, 1);
socket.connect();
CONNECTION_ENSTABLISHED = true; //protect from failing
} catch (Exception e) //obsolete way how to open BT
{
try {
Log.e("TrackingFlow", "Server socket: old way used...");
BluetoothServerSocket tmpsocket = adapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
socket = tmpsocket.accept();
CONNECTION_ENSTABLISHED = true; //protect from failing
Log.i("TrackingFlow", "Listening...");
} catch (Exception ie) {
Log.e(TAG, "Socket's accept method failed", ie);
ie.printStackTrace();
}
}
Log.i(TAG, "Server is ready for listening...");
runOnUiThread(new Runnable() {
@Override
public void run() { //Show message on UIThread
listItems.clear(); //remove chat history
listItems.add(0, String.format(" Server opened! Waiting for clients..."));
listAdapter.notifyDataSetChanged();
}
});
try //reading part
{
is = socket.getInputStream();
os = socket.getOutputStream();
new Thread(writter).start();
int bufferSize = 1024;
int bytesRead = -1;
byte[] buffer = new byte[bufferSize];
while (CONTINUE_READ_WRITE) //Keep reading the messages while connection is open...
{
final StringBuilder sb = new StringBuilder();
bytesRead = is.read(buffer);
if (bytesRead != -1) {
String result = "";
while ((bytesRead == bufferSize) && (buffer[bufferSize - 1] != 0)) {
result = result + new String(buffer, 0, bytesRead - 1);
bytesRead = is.read(buffer);
}
result = result + new String(buffer, 0, bytesRead - 1);
sb.append(result);
}
Log.e("TrackingFlow", "Read: " + sb.toString());
runOnUiThread(new Runnable() {
@Override
public void run() { //Show message on UIThread
Toast.makeText(ConnectToBluetoothActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
listItems.add(0, String.format("< %s", sb.toString())); //showing in history
listAdapter.notifyDataSetChanged();
}
});
}
} catch (IOException e) {
Log.e(TAG, "Server not connected...");
e.printStackTrace();
}
}
};
StringSendingActivity
public void sendString(String convertString){//发送转换后的字符串
byte[] b = convertedString.getBytes();
try {
Toast.makeText(this, "Sending" + convertedString, Toast.LENGTH_SHORT).show();
os.write(b);
// list.add(0, "> " + et.getText().toString()); //chat history
list.add(0, et.getText().toString()); //chat history
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.message_textview, R.id.textview, list);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
et.setText(""); //remove text after sending
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Not sent", Toast.LENGTH_SHORT).show(); //usually problem server-client decision
}
}