我在Android应用程序中创建了一个蓝牙活动类,它适用于所有蓝牙功能,如:扫描,配对,连接,发送和接收数据.
真正的问题是当Activity被销毁时.蓝牙正在断开连接.
如何在整个应用程序中建立蓝牙连接.我想从其他活动向蓝牙发送数据.
帮助我以简单的方式实现这一点?
public class BTActivity extends AppCompatActivity {
ArrayList<BluetoothDevice> devices = new ArrayList<>();
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mBluetoothDevice;
ArrayAdapter<BluetoothDevice> arrayAdapter;
ConnectThread c;
private static final String TAG = "MY_BT";
ListView lvPaired;
Button BluetoothOnOff, ScanBt, pairedlist, sendButton, btDisconnect, incrementBtn, decrementBtn;
EditText input_text;
TextView ConnectedTo;
FragmentManager fm = getSupportFragmentManager();
UUID BHANU_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bt);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
lvPaired = findViewById(R.id.btPairedLV);
pairedlist = findViewById(R.id.btPaired);
input_text = findViewById(R.id.user_input);
sendButton = findViewById(R.id.btnSend);
BluetoothOnOff = findViewById(R.id.offOn_bt);
ScanBt = …Run Code Online (Sandbox Code Playgroud) service android bluetooth background-service android-bluetooth