アプリケーションの再起動時に、既に接続されているBluetoothデバイスに自動接続するようにします。以下は私が実行している手順です: -Android:ブロードキャストレシーバは、アプリケーションの再起動時にBluetoothDevice.ACTION_ACL_CONNECTEDを受信しません。
[最初に] Bluetoothデバイスが「ON」の場合:アプリケーションの起動時に起動します。
は、[動作] - > Bluetoothデバイスがペアリングされ、正常に接続された(意図「ACTION_ACL_CONNECTED」が受信される)を取得Bluetoothデバイスが「ON」である:再びアプリを開始し、その後、アプリケーションを閉じました。
- > Bluetooth設定で表示されているように接続されていても、放送受信側は意図 'ACTION_ACL_CONNECTED'を受信しません。
注: - アプリケーションを閉じると、Bluetooth接続が切断されません。 これで、接続に成功するとすぐにホームスクリーンに移動します。それ以外の場合、アプリケーションはボタンを持った画面に移動し、Bluetooth設定(下のコードにあるonClickListener)に移動します。
私はアンドロイドの開発に慣れていないので、どこが間違っているのか分かりません。私は同様の問題の解決策を探して適用しましたが、効果はありません。
@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();
}