Android不断获取配对设备的蓝牙信号强度 [英] Android Getting continuously Bluetooth signal strength of paired devices
问题描述
我正在列出所有已配对的设备,并且运行良好,但是现在想获取已配对设备的蓝牙信号强度...我知道它将通过使用rssi获得,但无法在我的应用程序中连续实现. 请给我合适的代码作为我的代码...我的代码在这里...
i am listing all paired devices,and it well work but now want to get Bluetooth signal strength of paired devices...i know it will get by use rssi but cannot implement get it continuously in my app.. plz me by giving suitable code as my code...my code is here...
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);
}
}
}
推荐答案
您可以从以下代码中获取信号.
You can get the signal from following code.
编写活动代码
@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);
}
}
};
当您的设备连接到远程设备时,将执行此广播. 您还可以执行其他几种操作来触发此广播.看看此处(BluetoothDevice)
This broadcast will execute when your device will be connected to the remote device. There are several other action on which you can fire this broadcast. have a look at here(BluetoothDevice)
检查以下链接以不断读取RSSI
Check following link for constant reading of RSSI
用于持续测量已连接Android的蓝牙RSSI的教程设备(Java)
链接的输出:
这篇关于Android不断获取配对设备的蓝牙信号强度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!