Android连续获得配对设备的蓝牙信号强度
我列出了所有配对设备,它运行良好,但现在希望获得配对设备的蓝牙信号强度…我知道它将通过使用rssi获得,但无法在我的应用程序中持续实现。。 请给我合适的代码作为我的代码…我的代码在这里Android连续获得配对设备的蓝牙信号强度,android,bluetooth,Android,Bluetooth,我列出了所有配对设备,它运行良好,但现在希望获得配对设备的蓝牙信号强度…我知道它将通过使用rssi获得,但无法在我的应用程序中持续实现。。 请给我合适的代码作为我的代码…我的代码在这里 public class Security extends Fragment implements OnClickListener{ private BluetoothAdapter BA; private Set<BluetoothDevice>pairedDevices;
public class Security extends Fragment implements OnClickListener{
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ArrayList<String> mylist = new ArrayList<String>();
//private Object ImageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.security, null);
BA = BluetoothAdapter.getDefaultAdapter();
/* starting the bluetooth*/
on(v);
pairedDevices = BA.getBondedDevices();
//length=4;
// int j=1;
for(BluetoothDevice bt : pairedDevices) {
mylist.add(bt.getName());
length=j;
j++;
bt.getBondState();
}
return v;
}
@Override
public void onResume() {
super.onResume();
// Toast.makeText(getActivity(), "On resume", Toast.LENGTH_LONG).show();
}
/*************************Bluetooth function****************************/
public void on(View view){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getActivity(),"Turned on"
,Toast.LENGTH_LONG).show();
} else{
// Toast.makeText(getActivity(),"Already on",
// Toast.LENGTH_LONG).show();
}
}
public void Discovery(View view) {
if(BA.isDiscovering()) {
BA.cancelDiscovery();
}
}
public void list(View view){
Toast.makeText(getActivity(),"Showing Paired Devices",
Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
for(int j=0;j<length;j++) {
if(v.getId()==j)
Toast.makeText(getActivity(), mylist.get(j), Toast.LENGTH_LONG).show();
//hand.update(run,1000);
}
}
}
公共类安全性扩展了OnClickListener{
私人蓝牙适配器BA;
私有设置对设备;
ArrayList mylist=新的ArrayList();
//私有对象图像视图;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图v=充气机充气(R.layout.security,空);
BA=BluetoothAdapter.getDefaultAdapter();
/*启动蓝牙*/
关于(v);
pairedDevices=BA.getBondedDevices();
//长度=4;
//int j=1;
用于(蓝牙设备bt:pairedDevices){
mylist.add(bt.getName());
长度=j;
j++;
bt.getBondState();
}
返回v;
}
@凌驾
恢复时公开作废(){
super.onResume();
//Toast.makeText(getActivity(),“在简历上”,Toast.LENGTH_LONG.show();
}
/*************************蓝牙功能******************************/
上的公共无效(视图){
如果(!BA.isEnabled()){
意向开启=新意向(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(打开,0);
Toast.makeText(getActivity(),“已打开”
,Toast.LENGTH_LONG)show();
}否则{
//Toast.makeText(getActivity(),“已打开”,
//Toast.LENGTH_LONG).show();
}
}
公共空间发现(视图){
if(BA.isDiscovering()){
BA.cancelDiscovery();
}
}
公共作废列表(视图){
Toast.makeText(getActivity(),“显示配对设备”,
吐司。长度(短)。show();
}
@凌驾
公共void onClick(视图v){
对于(int j=0;j,您可以从以下代码获得信号
为您的活动编写代码
@Override
public void onCreate(Bundle savedInstanceState) {
.....
// Registering Broadcast. this will fire when Bluetoothdevice Found
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
}
private final BroadcastReceiver BroadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String mIntentAction = intent.getAction();
if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(mIntentAction)) {
int RSSI = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
String mDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
}
}
};
当您的设备连接到远程设备时,将执行此广播。
您可以启动此广播的其他几个操作。请查看
检查以下链接以获取RSSI的恒定读数
链接的输出:
如果您想连续运行,您需要在服务或线程中运行它。这样您甚至可以添加时隙或睡眠(等待)测量RSSI。您是否只想要配对设备。?因为您没有发现Bluetooth设备。是的,只有活动配对设备…如果您正在考虑活动设备,那么它将已经与您的设备配对。一切正常,但我无法连续获得活动配对设备的RSSI……。您好,迪曼,您有t的解决方案吗他的。我也面临着同样的问题。请帮助我。我知道这一点,但它在运行应用程序时检索rssi一次…我希望在运行应用程序时在时间片之后继续…然后你需要不断调用它。在时间任务中指代