我正在开发一个使用蓝牙连接的应用程序 - 到目前为止 - 从Arduino发送和接收数据。 我正在使用一个Activity,现在我正在尝试切换到Fragments,所以我可以在那之间共享数据。 问题是:代码的蓝牙部分无法复制到所有片段中,也不能保留在旧活动中。这意味着我需要找到一种方法来保持相同的蓝牙连接在任何打开的片段中保持活动状态,以便它们可以在它们之间进行通信和共享数据。 我想知道:最好的办法是什么? IntentService类?仅为蓝牙参数创建整个界面?
package br.com.fespy.fespybluetooth;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
public class NavDrawer extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//STATEMENTS DECLARATIONS
Switch switch1, switch2, switch3;
boolean btState = false;
boolean test = true;
private static String MAC = null;
private static final int REQUEST_ENABLE_BT = 1;
private static final int REQUEST_CONNECTION_BT = 2;
BluetoothAdapter myBluetoothAdapter = null;
BluetoothDevice myBluetoothDevice = null;
BluetoothSocket myBluetoothSocket = null;
UUID myUUID = UUID.fromString("00000003-0000-1000-8000-00805f9b34fb");
NavDrawer.ConnectedThread connectedThread;
public void startService (View view)
{
Intent intent = new Intent(this, Bluetooth.class);
startService(intent);
Toast.makeText(this, "Back up to their brains.",Toast.LENGTH_LONG).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//HOME TITLE
setTitle("");
//DISPLAYS THE FESPY LOGO
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.fespy2);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//SWITCH BUTTONS
switch1 = (Switch) findViewById(R.id.switch1);
switch2 = (Switch) findViewById(R.id.switch2);
switch3 = (Switch) findViewById(R.id.switch3);
//LOADS OF BLUETOOTH SETTINGS
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (myBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "Device disconnected.", Toast.LENGTH_LONG).show();
} else if (!myBluetoothAdapter.isEnabled()) //CHECK FOR BLUETOOTH CONNECTION
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (btState) {
connectedThread.write("1");
} else {
Toast.makeText(getApplicationContext(), "Bluetooth is not connected.", Toast.LENGTH_LONG).show();
switch1.setChecked(false);
}
} else {
if (btState) {
connectedThread.write("0");
} else {
Toast.makeText(getApplicationContext(), "Bluetooth is not connected.", Toast.LENGTH_LONG).show();
switch1.setChecked(false);
}
}
}
});
switch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (btState) {
connectedThread.write("3");
} else {
Toast.makeText(getApplicationContext(), "Bluetooth is not connected.", Toast.LENGTH_LONG).show();
switch2.setChecked(false);
}
} else {
if (btState) {
connectedThread.write("2");
} else {
Toast.makeText(getApplicationContext(), "Bluetooth is not connected.", Toast.LENGTH_LONG).show();
switch2.setChecked(false);
}
}
}
});
switch3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (btState) {
connectedThread.write("5");
} else {
Toast.makeText(getApplicationContext(), "Bluetooth is not connected.", Toast.LENGTH_LONG).show();
switch3.setChecked(false);
}
} else {
if (btState) {
connectedThread.write("4");
} else {
Toast.makeText(getApplicationContext(), "Bluetooth is not connected.", Toast.LENGTH_LONG).show();
switch3.setChecked(false);
}
}
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.closeDrawer(GravityCompat.START);
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.header_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) //SETTINGS FROM NAV BAR
{
startActivity(new Intent(this, Settings.class));
}
//BLUETOOTH CONNECTION BUTTON
switch (item.getItemId()) {
case R.id.btConnect: {
if (btState) {
try {
myBluetoothSocket.close();
btState = false;
Toast.makeText(getApplicationContext(), "Bluetooth disconnected.", Toast.LENGTH_LONG).show();
} catch (IOException error) {
Toast.makeText(getApplicationContext(), "Error" + error, Toast.LENGTH_LONG).show();
}
} else {
Intent openList = new Intent(NavDrawer.this, AvailableDevices.class);
startActivityForResult(openList, REQUEST_CONNECTION_BT);
}
}
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
android.app.FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_home) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new FirstFragment()).commit();
} else if (id == R.id.nav_share) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new SecondFragment()).commit();
} else if (id == R.id.nav_about) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new ThirdFragment()).commit();
}
if (id == R.id.nav_settings) {
startActivity(new Intent(this, Settings.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(getApplicationContext(), "Bluetooth activated.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "The app needs a bluetooth connection.", Toast.LENGTH_LONG).show();
finish();
}
break;
case REQUEST_CONNECTION_BT:
if (resultCode == Activity.RESULT_OK) {
MAC = data.getExtras().getString(AvailableDevices.MAC_ADDRESS);
myBluetoothDevice = myBluetoothAdapter.getRemoteDevice(MAC);
try {
myBluetoothSocket = myBluetoothDevice.createRfcommSocketToServiceRecord(myUUID);
myBluetoothSocket = (BluetoothSocket) myBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(myBluetoothDevice, 1);
Toast.makeText(getApplicationContext(), "Connecting...", Toast.LENGTH_LONG).show();
myBluetoothSocket.connect();
Toast.makeText(getApplicationContext(), "Connected.", Toast.LENGTH_LONG).show();
btState = true;
connectedThread = new NavDrawer.ConnectedThread(myBluetoothSocket);
connectedThread.start();
} catch (Exception e2) {
Toast.makeText(getApplicationContext(), "Error. Try again.", Toast.LENGTH_LONG).show();
//connect.setChecked(false); <<removed from the code
btState = false;
}
} else //missclick
{
Toast.makeText(getApplicationContext(), "Try again.", Toast.LENGTH_LONG).show();
//connect.setChecked(false); <<removed from the code
}
}
}
class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
// mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(String sentData) {
byte[] msgBuffer = sentData.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
}
}
}
}
答案 0 :(得分:0)
可以这样做: 在您的主要活动中,您将创建片段。
您可以使用SupportFragmentManager根据您的需要显示/隐藏片段来管理片段。
MainActivity可以调用片段上的方法,因此通信的下方是通过每个片段的公共方法完成的。
对于通信的上行部分,从片段到活动,每个片段将定义MainActivity必须实现的接口。
接口定义了MainActivity将在每个片段上侦听的方法(回调)。
创建片段后,在创建活动后,片段将MainActivity设置为其侦听器。
当片段收到需要转发到Main Activity的事件时,它会调用它的侦听器接口中定义的相应方法,MainActivity会通过做某事或将evenet转发给另一个片段来对此做出反应定义
在这个简化的例子中,我只使用一个片段来显示它的工作方式。您将根据需要添加片段,每个片段都有自己的回调接口。
更改片段中的开关时,主要活动将显示具有当前值的Toast。
对于你的问题,你在主活动中获得了布尔值,所以你可以像今天一样从那里开始它。
主要活动布局 - so_main_activity_43709698.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragments_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
片段布局so_fragment_43709698_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch 1"/>
</LinearLayout>
主要活动SO_43709698.java
package com.somepackage.so_43709698;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.Toast;
public class SO_43709698 extends AppCompatActivity implements Fragment_43709698_1.Fragment_43709698_1_Listener {
Fragment_43709698_1 fragment1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.so_main_activity_43709698);
//FrameLayout fragmentContainer = (FrameLayout) findViewById(R.id.fragment_container);
fragment1 = new Fragment_43709698_1();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragments_container,fragment1,"Fragment1")
.show(fragment1)
.commit();
}
//This is a callback from fragent1 used when fragment one wants to send a "message" to MainActivity
//You can implement as many as you need.
@Override
public void onFragment_43709698_1_Switch1Change(boolean b) {
Toast.makeText(this,"Switch in Fragment 1 changed to: " + b, Toast.LENGTH_LONG ).show();
}
}
片段1 - Fragment_43709698_1.java
package com.somepackage.so_43709698;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
public class Fragment_43709698_1 extends Fragment {
private Switch switch1;
Fragment_43709698_1_Listener listener; //This will be the activity;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.so_fragment_43709698_1,container, false);
switch1 = (Switch) v.findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
listener.onFragment_43709698_1_Switch1Change(b);
}
});
return v;
}
public int aMethodMainActivityCanCAll(String withSomeParameter){
int aResultToMainActivity = 0;
//MainActivity can call methods on the fragment to communicate things like
//requesting some status, or pass throguh requests from other fragment.
return aResultToMainActivity;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listener = (Fragment_43709698_1_Listener) getActivity();
}
// In this interface you define what things the fragment can call on MainActivity to send it
// "messages"
public static interface Fragment_43709698_1_Listener{
public void onFragment_43709698_1_Switch1Change(boolean b);
}
}