gpt4 book ai didi

android - 如何在 Android 中检查配对设备的蓝牙连接状态

转载 作者:行者123 更新时间:2023-11-28 20:23:23 25 4
gpt4 key购买 nike

我开发了一个蓝牙应用程序,它将连接到配对设备并发送消息,但我必须先测试连接。我尝试了很多选择,但没有一个效果很好。那么你能给我发送任何可以做到这一点的代码示例吗?我创建了一个线程,但无法获得良好的连接状态来构建“if”函数。这是代码:

package com.example.szukacz;

import java.lang.reflect.Method;
import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
LinearLayout sparowaneUrzadzenia;

public void lokalizowanie() {
Intent intencja = new Intent(this, Lokalizator.class);
startActivity(intencja);

}

public void parowanie(View v) {
Intent intencja = new Intent(this, Parowanie.class);
startActivity(intencja);

}

boolean isRunning;

Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String status = (String)msg.obj;
if(status == "polaczony") {
alarm();
showToast("prawda, zwraca" + status);
} else {
showToast("wykonanie x, zwraca: " + status);
};
}
};

public void alarm() {
showToast("Alarm!!!");
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sparowaneUrzadzenia = (LinearLayout) findViewById(R.id.listaUrzadzenGlowna);

pokazSparowane();



}

public void onStop() {
super.onStop();
isRunning = false;
}

public void onStart() {
super.onStart();
Thread testPolaczen = new Thread(new Runnable() {
public void run() {
try {
for(int i = 0; i < 1000000; i++) {
Thread.sleep(5000);
testujPolaczenia();
int stan = 0;
String status = Integer.toString(stan);

Message msg = handler.obtainMessage(1, (String)status);

if(isRunning == true) {
handler.sendMessage(msg);
}
}
} catch (Throwable t) {
// watek stop
}
}
});
isRunning = true;
testPolaczen.start();
}

private void testujPolaczenia() {

}

public void pokazSparowane(){
/*
* Wyświetlanie listy sparowanych urządzeń .
* */
Log.d("INFO","Sparowane dla tego urzÄ…dzenia");
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
Log.d("INFO",device.getName()+" - "+device.getAddress());

// dodawanie urzadzen do listy
Button urzadzenie = new Button(getApplicationContext());
urzadzenie.setText(device.getName());
// urzadzenie.setTextColor(0xffffff); //jak ustawic na czarny kolor napsisów ?

urzadzenie.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)
{
showToast("klik");
lokalizowanie();

}
});

sparowaneUrzadzenia.addView(urzadzenie);
}
} else {
showToast("brak sparowanych urzadzen");
}
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

@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) {
return true;
}
return super.onOptionsItemSelected(item);
}


}

谢谢!

最佳答案

我遇到了同样的问题,因为我正在开发一个可能在运行时使用 TTS 的应用程序。我认为除了创建广播接收器并监视蓝牙状态的变化之外,没有办法立即检查是否有任何蓝牙设备通过 BluetoothAdapter 类连接。

摸索了几个小时后,我找到了一个相当巧妙的方法来解决这个问题。我试过了,它对我来说效果很好。

AudioManager audioManager = (AudioManager) getApplicationContext.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.isBluetoothA2dpOn()) {
//audio is currently being routed to bluetooth -> bluetooth is connected
}

来源:http://developer.android.com/training/managing-audio/audio-output.html

关于android - 如何在 Android 中检查配对设备的蓝牙连接状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com