我希望我的应用在重新启动应用时自动连接到已连接的蓝牙设备。以下是我正在执行的程序: -
[初始]蓝牙设备开启':然后启动应用程序。
[行为] - GT;蓝牙设备已成功配对并成功连接(意图' ACTION_ACL_CONNECTED'已收到)
蓝牙设备已开启':关闭应用,然后重新启动应用。
[行为] - GT;即使它按照蓝牙设置显示连接,并且广播接收器没有收到意图' 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();
}
答案 0 :(得分:0)
由于设备仍处于连接状态,您不会收到BluetoothDevice.ACTION_ACL_CONNECTED
事件。仅在将设备状态从断开连接更改为已连接时才会触发该事件。
您有2个选项。
您可以将BroadcastReceiver
BluetoothDevice.ACTION_ACL_CONNECTED
和BluetoothDevice.ACTION_ACL_DISCONNECTED
过滤器放入Service
,并在后台跟踪设备连接状态。在您的应用启动时,您可以要求服务为您提供设备的当前状态。
您可以检查某些蓝牙配置文件是否包含已连接设备列表中的设备名称。
对于API 18+,您可以使用BluetoothManager#getConnectedDevices()
获取低于18的API,您可以使用以下代码段(针对每个蓝牙配置文件)
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);