我希望我的应用在重启应用时自动连接到已连接的蓝牙设备。以下是我正在执行的程序:
[最初]蓝牙设备为'ON':然后启动应用程序。
[行为]->蓝牙设备已成功配对并连接(收到意图“ ACTION_ACL_CONNECTED”)
蓝牙设备为“ ON”:关闭应用程序,然后再次启动应用程序。
[行为]->即使已按照蓝牙设置上的显示进行连接,并且广播接收器也未收到意图'ACTION_ACL_CONNECTED'。
注意:-在关闭应用程序时,它不会断开蓝牙连接。
因此,成功连接后,应用程序将立即转到HomeScreen。否则,应用程序将转到带有按钮的屏幕,该按钮将其设置为蓝牙设置(下面的代码中显示了onClickListener)
我是android开发的新手,所以我真的不知道我要去哪里错了。我查找了类似问题的解决方案并将其应用,但没有任何效果。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
registerReceiver(mReceiver, filter);
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
m_app = (BtApp) getApplication();
imagebt = (ImageView) this.findViewById(R.id.imagebt);
imagebt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Toast tag = Toast.makeText(getApplicationContext(), "Connect to device", Toast.LENGTH_LONG);
tag.show();
new CountDownTimer(1000, 1000)
{
public void onTick(long millisUntilFinished) {tag.show();}
public void onFinish() {
//tag.show();
}
}.start();
if(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.startDiscovery();
}
Intent intentBluetooth = new Intent();
intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(intentBluetooth);
}
});
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ( BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
m_app.m_main.setupCommPort();
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
m_app.m_device = device;
isconnected = true;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if ( m_app.m_main.m_BtService != null && m_app.m_main.m_BtService.getState() != BluetoothRFCommService.STATE_CONNECTED ) {
m_app.m_main.m_BtService.connect(device, false);
}
}
}, 3500);
} else if ( BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) ) {
isconnected = false;
m_app.m_main.tabHost.setCurrentTab(0);
}
}
};
@Override
protected void onStop()
{
unregisterReceiver(mReceiver);
super.onStop();
}
最佳答案
由于设备仍处于连接状态,因此不会出现BluetoothDevice.ACTION_ACL_CONNECTED
事件。仅在将设备状态从断开连接更改为已连接时才触发该事件。
您有2个选择。
您可以将具有BroadcastReceiver
和BluetoothDevice.ACTION_ACL_CONNECTED
过滤器的BluetoothDevice.ACTION_ACL_DISCONNECTED
放入Service
,并在后台跟踪设备连接状态。在应用启动时,您可以要求该服务为您提供设备的当前状态。
您可以检查某些蓝牙配置文件是否在连接的设备列表中包含您的设备名称。
对于API 18+,您可以对以下的API使用BluetoothManager#getConnectedDevices()
,可以使用以下代码段(针对每个蓝牙配置文件)
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
for (BluetoothDevice device : proxy.getConnectedDevices()) {
if (device.getName().contains("DEVICE_NAME")) {
deviceConnected = true;
}
}
if (!deviceConnected) {
Toast.makeText(getActivity(), "DEVICE NOT CONNECTED", Toast.LENGTH_SHORT).show();
}
mBluetoothAdapter.closeProfileProxy(profile, proxy);
}
public void onServiceDisconnected(int profile) {
// TODO
}
};
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.A2DP);